Filtering even numbers in python [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
just started with python and wanted to filter the even numbers from a numpy array:
>array = np.arange(2,10000)
>>print(array)
I know that the remainder of even no./2 should be 0, so part of the filtering condition should look somehow like this:
>if x%2 == 0
But no matter how, I always get an error of some kind. btw I'm using Python 3.
Thanks and Best

One Liner as pointed by Mikel:
print(np.arange(2,10000,2))
This creates an array starting from 2 ending at 10k with a step size of 2 i.e every second number.
Or if you want to use modulus you can try like this:
ar = np.arange(2,10000)
ar = ar[ar%2==0]
print(ar)
Output:
array([ 2, 4, 6, ..., 9994, 9996, 9998])
ar%2==0 creates a boolean mask to include only even numbers

Related

How can I write a function that adds the number of possitive number in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
I am trying to write a simple function that takes a list of number passed as an argument and prints how many positive number is in the list.
I can't seem to figure out what is wrong with the code here. Can someone please explain this.
You should return add instead of num. And you should initialize add outside the for loop.
lst = [1,2,3,4,-4,-3,-2,-1]
def count_positives(lst):
return sum(i > 0 for i in lst)
print(count_positives(lst))
the program above will print 4

Maximum Collatz length in Python, but output is not correct [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
This is my first time posting to Stackoverflow.
I'm trying to solve this problem here: https://codingbat.com/prob/p270692?parent=/home/konstans#stuy.edu/all
When looking at all hailstone sequences from 1 to z, maxHail(z) will return the starting number that creates the longest sequence. In other words, maxHail(n) looks at hailLen(1), hailLen(2) ... hailLen(n) and returns the number from 1-n that had the largest hailstone sequence. You should look at the hailLen() problem before working on this. You should use your solution from the hailLen() problem. ( http://codingbat.com/author/p264289 ) since hailLen(3) is larger than the hailLen of 4 or 5, maxHail of 3,4,5 all return 3. Since 6 has a longer sequence, maxHail(6) gives us 6. remember: Use the hailLen function you already wrote!
Here's my code and the output:
However, I'm not sure where this goes wrong - I checked line-by-line and couldn't see anything wrong. Could anyone help me fix this? Thank you!
I see what is wrong - hailLen returns lenght of sequence and the question is about index for which the sequence is the longest. Just store it in variable
if (res := hailLen(i)) > counter: # it's python 3.8 syntax
counter = res
index = i
return index

cuda in python issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have written the code below in order to discover the number of threads and blocks and send them to train_kernel function.
rows = df.shape[0]
thread_ct = (gpu.WARP_SIZE, gpu.WARP_SIZE)
block_ct = map(lambda x: int(math.ceil(float(x) / thread_ct[0])),[rows,ndims])
train_kernel[block_ct, thread_ct](Xg, yg, syn0g, syn1g, iterations)
but after execution, I face the error below:
griddim must be a sequence of integers
Although you have not stated it, you are clearly running this code in Python 3.
The semantics of map changed between Python 2 and Python 3. In Python 2 map returns a list. In Python 3 it returns an iterator. See here.
To fix this you need to do something like:
block_ct = list(map(lambda x: int(math.ceil(float(x) / thread_ct[0])),[rows,ndims]))
Alternatively you could just use a list comprehension without the lambda expression and map call:
block_ct = [ int(math.ceil(float(x) / thread_ct[0])) for x in [rows,ndims] ]
Either will yield a list with the necessary elements which should work in the CUDA kernel launch call.

Code to check if a list of numbers is in arithmetic progression or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Following is the code to check if a list of items is in arithmetic progression or not.
def ap():
l=[int(x) for x in list(input("Enter the list: "))]
diff=l[1]-l[0]
for i in range(len(l)-1):
if not ( l[i+1]-l[i]==diff):
return False
return True
When I am executing the above code, it is working fine, but If I am modifying the code and don't use the "not" keyword it is returning true in all the cases.
Following is the code:
def ap():
l=[int(x) for x in list(input("Enter the list: "))]
diff=l[1]-l[0]
for i in range(len(l)-1):
if (l[i+1]-l[i]==diff):
return True
return False
Can someone please help me to figure out where am I going wrong?
Of course it does. You get the difference between the first two elements, and then in your loop, the first step will check if the difference between the first two elements is the same, which it will always be. So, it will always return true in the first iteration of the loop.

Matching two comma seperated strings in Python and correct position counts [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
Hope get assistance on the below problem.
Python having more in-built sequence matcher functions. Whether the following requirement can be done through any built-in function without looping.
x = 'hu1_X','hu2_Y','hu3_H','hu4_H','hu5_H','hu7_H'
y = 'hu1_H','hu2_X','hu3_H','hu4_H','hu5_H','hu7_X'
for comparing the above string the final match count is 3.
Matches are: 'hu3_H','hu4_H','hu5_H'.
Any idea, which in-built function can use? Can we go with ZIP() in Python.
Thanks in advance.
You can use a generator expression and the builtin sum function along with zip, like this
x = 'hu1_X', 'hu2_Y', 'hu3_H', 'hu4_H', 'hu5_H', 'hu7_H'
y = 'hu1_H', 'hu2_X', 'hu3_H', 'hu4_H', 'hu5_H', 'hu7_X'
print(sum(item1 == item2 for item1, item2 in zip(x, y)))
# 3
This works because, in Python, True and False can be treated as 1 and 0 respectively. So, we can simply compare the corresponding elements and the result of that evaluation can be added together to get the total match count.

Categories