This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 years ago.
A snippet of regression code of a stock price data-
forecast_col='Adj. Close'
forecast_out=int(math.ceil(0.01*len(df)))
df['label']=df[forecast_col].shift(-forecast_out)
X=X[:-forecast_out+1]
What is the meaning of X=X[:-forecast_out+1] ?
This is a case of slice indexing, and also uses negative indices. It means that the last forecast_out + 1 elements of X are discared. For example,
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[:-2]
[1, 2, 3]
Related
This question already has an answer here:
How to know the number of non-zero elements of a Fortran array?
(1 answer)
Closed 1 year ago.
For example if I have a 1d array
indices=(/1, 1, 1, 2 , 2 ,10, 11 /)
I want to know how many times 1 occurs (the answer should be 3).
Number 2 should be 2 times, number 10 should be 1, number 11 should also be 1.
I already tried to find out if there is intrinsic function but the count function works differently.
You can use the count intrinsic with a comparison operator, e.g.
integer :: indices(7)
indices = [1, 1, 1, 2, 2, 10, 11]
write(*,*) count(indices==1)
This question already has answers here:
Setting two arrays equal [duplicate]
(3 answers)
Closed 5 years ago.
I am really shocked, and could not think of the logic why do this happened. This is what I did:
>>> import random
>>> c = a
>>> a
[1, 2, 3, 4, 5]
>>> c
[1, 2, 3, 4, 5]
>>> random.shuffle(a)
>>> a
[5, 1, 3, 2, 4]
>>> c
[5, 1, 3, 2, 4]
>>> random.shuffle(c)
>>> c
[5, 4, 3, 2, 1]
>>> a
[5, 4, 3, 2, 1]
>>>
Expected result is, the array 'a' is not the same as 'c'. Please enlighten me with the light of your knowledge for explaining why was the result the same as expected result as I am going mad.
c and a are the same object. That's why changing either changes the other. If you want to copy the object, here's one way to do it:
a = c[:]
found here
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I have a list of integers larger than zero. I need to identify the one with the highest number of dividers. For this I created two functions: one that gives me all the divisors of all the elements of a list and another that filters which element that has more divisors. The problem is that I can not make the maisDivisores function directly receive a list of only the elements (without dividers), you know? If I call the function 1 (listaDivisores) within the function 2 (maisDivisores) always crashes. However if I call manually, it works good. I've tried all the possibilities and nothing. How do I call the first function in the second for this to work getting the gross list?
def listaDivisores(lista):
if lista == []:
return []
else:
lista=qs(lista)
resultado=[]
resultado.append((lista[0],[y for y in range(1,((lista[0])+1)) if (int(lista[0]))%y==0]))
return resultado+listaDivisores(lista[1:])
return listaDivisores(lista)
def maisDivisores(lista):
if len(lista)==[]:
return "Nenhum nĂºmero."
else:
**lista=listaDivisores(lista)**
if int(len(lista))==1:
return lista[0]
elif int(len(lista[0][1]))<int(len(lista[1][1])):
lista.pop(0)
elif int(len(lista[0][1]))==int(len(lista[1][1])):
if lista[0]<lista[1]:
lista.pop(0)
else:
lista.pop(1)
else:
lista.pop(1)
return maisDivisores(lista)
return lista
functions working separately; error log when working together.
you can easily get the divisor of a number with list comprehension like this
def divisores(n):
if n <= 0:
raise ValueError("n must be positive")
return [x for x in range(1,n+1) if n%x==0 ]
then you can use the build in max with a key function to get the desire result
>>> test=[24, 5, 9, 42]
>>> max( test, key=lambda x:len(divisores(x)))
24
>>>
if you also want to get the divisor in the same step, you can use a generator or list comprehension to build a intermediary result from which get the max
for example
>>> max( ( (x,divisores(x)) for x in test), key=lambda y:len(y[1]))
(24, [1, 2, 3, 4, 6, 8, 12, 24])
here ( (x,divisores(x)) for x in test) is the generator, which create a tuple with the number and the list of its divisors
or
>>> test2 = [ (x,divisores(x)) for x in test ]
>>> test2
[(24, [1, 2, 3, 4, 6, 8, 12, 24]), (5, [1, 5]), (9, [1, 3, 9]), (42, [1, 2, 3, 6, 7, 14, 21, 42])]
>>> max(test2,key=lambda x:len(x[1]))
(24, [1, 2, 3, 4, 6, 8, 12, 24])
>>>
and any of those can be made into a simple function.
Your implementation is just too convoluted for what you want and frankly I have a hard time understanding it
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 7 years ago.
>>> a = [1,2,3]
>>> b = [1,2]
>>> a.append(b)
>>> a
[1, 2, 3, [1, 2]]
>>> b
[1, 2]
>>> b.extend(a)
>>> b
[1, 2, 1, 2, 3, [...]]
>>> b[5]
[1, 2, 1, 2, 3, [...]]
What is the [...] here? This is confusing me. What's wrong with my approach?
Can someone clarify my doubt?
You created a circular reference. a contains the same list b was referencing. By extending b with a, b now contains a reference to itself.
Python displays such a reference by using ... rather than go into an infinite loop. Printing b[5] is printing the same object again, so the output naturally is the same again.
This question already has answers here:
Python list rotation [duplicate]
(4 answers)
Closed 7 years ago.
I'm asked to write a function rotate_left3(nums) that takes a list of ints of length 3 called nums and returns a list with the elements "rotated left" so [1, 2, 3] yields [2, 3, 1].
The questions asks to rotate lists of length 3 only. I can easily do this by the following function (as long as the lists will only be of length 3):
def rotate_left3(nums):
return [nums[1]] + [nums[2]] + [nums[0]]
However, my question is, how do I do the same operation but with lists of unknown lengths?
I have seen some solutions that are complicated to me as a beginner. so I'd appreciate if we can keep the solution as simple as possible.
Let's create a list:
>>> nums = range(5)
Now, let's rotate left by one position:
>>> nums[1:] + nums[:1]
[1, 2, 3, 4, 0]
If we wanted to rotate left by two positions, we would use:
>>> nums[2:] + nums[:2]
[2, 3, 4, 0, 1]