How can i put this into a list comprehension [closed] - python

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 6 years ago.
Improve this question
write a function super_sum(A), that takes in an array of numbers A, and returns the sum of all the numbers after doubling the odd numbers and halving the even numbers.
heres my solution: how do solve this with a list comprehension
def super_sum(A):
new_sum=[]
total=0
for x in A:
if x%2==0:
x=x/2
elif x%2!=0:
x=x*2
new_sum.append(x)
return sum(new_sum)
print (super_sum([10,3,5]))

sum([x/2 if x%2==0 else x*2 for x in [10,3,5]])

Related

I am a python student, and I have a problem [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def thing():
_list = [1,1,5,6,8,3,4,6,10,23,5,12,67,3,25,2,6,5,4,3,2,1]
_list1 = [str(i) for i<=5 in lista]
return " ".join(_list1)
print(thing()))
I am new to this type of list managment, I am trying to put in _list1 only integers that are less then 5
so like the name of your function, it's created to calculate the sum of integers.
So basically, if your n=0 then your program will return 0 as result.
If not (else), it gonna give you a result of the calculation in return.
And here you want to print the interger sum of 451 which will give you the result of n % 10 + integer_sum(int(n / 10))

Sum in range that meet criteria [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 2 years ago.
Improve this question
Trying to learn a new approach.It's the classical problem of finding the sum of all multiples of 3 and 5 below 'n'.
What I want is:
print ("The sum is: ", sum(range(1, 100)))
But with an if-statement for multiples of 3 / 5.My question:How can I include that if-statement for a one-line solution?
Thanks for your help. =)// Chris S.
This is the approach you may look into:
def thesum(the_numbers):
the_numbers = [i for i in the_numbers if i % 3 == 0 or i % 5 == 0]
return print(sum(the_numbers))
thesum(range(100))

How to make all numbers in a list of lists into just ints [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 7 months ago.
Improve this question
Say I have
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
and I want to cast all of those numbers into just ints to have
A = [[1,2,1],[2,1,3]]
How do we do that in python?
Try list comprehension*2:
print([[int(x) for x in i] for i in A])
Or list comprehension + map:
print([list(map(int,i)) for i in A])
Or map+map:
print(list(map(lambda x: list(map(int,x)),A)))
Simple ways all return:
[[1,2,1],[2,1,3]]
Here's an approach using a list comprehension and map:
A = [[1.0,2.3,1.1],[2.2,1.3,3.2]]
print([list(map(int, i)) for i in A])

How do I return a list of numbers below a certain threshold without using complex list functions? [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 4 years ago.
Improve this question
I can't figure out how to do this without using complex functions, please help. this is the docstring of the code:
'''
finds all numbers in the list below a certain threshold
:param numList: a list of numbers
:threshold: the cutoff (only numbers below this will be included)
:returns: a new list of all numbers from numList below the threshold
'''
One approach
def filterList(numList, threshold):
return list(filter(lambda x: x < threshold, numList))
Another approach:
filteredList = [x for x in numList if x < threshold]

How to print certain elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How do I print certain elements of a list for example:
list1=["0","0","0","0","0","0","Element1","0","0","0","0"]
Is there any simple way to print only Element1 that specifies that you should not print out anything that is equal to 0.
Use a list comprehension or (as in this example) a generator expression to filter out the "0" items, and loop through the filtered list:
for item in (x for x in list1 if x != "0"):
print(item)
This prints all items that are not "0".

Categories