This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Sum a list of numbers in Python
(26 answers)
Apply function to each element of a list
(4 answers)
Closed 6 months ago.
if I had a list
results = ['1','4','73','92']
how do I get that list to call on itself and add all elements together?
the elements have to be strings
map str to int and sum it
results = ['1','4','73','92']
sum(map(int, results))
You can use list comprehension to convert the list of strings to a list of ints, then use the sum function to add all of the integers up.
results = ['1','4','73','92']
sum([int(x) for x in results]) # 170
One way to do this is using a list comprehension + sum operator:
sum([float(num) for num in results])
Note that it's safer to use float() instead of int() since your elements may include decimals.
This can be achieved using a for loop.
First, create a variable for the total:
total = 0
Next, loop through all elements in the list:
for result in results:
Then, check if the string is a number:
if result.isnumeric():
Finally, add the result to the total:
total += int(result)
This results in the final code of:
total = 0
for result in results:
if result.isnumeric():
total += int(result)
This question already has answers here:
Convert all strings in a list to integers
(11 answers)
Closed 3 years ago.
I'm trying to take a single line of intergers and using a for loop to convert the elements into integers, Is there a more pyhtonic way of doing so??
a = input().strip().split()
l = []
for i in a:
l.append(int(i))
You can do map directly on split:
l = list(map(int, input().strip().split()))
This question already has answers here:
Squaring all elements in a list
(9 answers)
How to add an integer to each element in a list?
(12 answers)
How to multiply individual elements of a list with a number?
(4 answers)
Closed 4 years ago.
Given a list x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10] use a for loop to create a new list, y, that contains the value aSin(10a) for each value, a, in list x. Plot the results using plot(x,y).
I have...
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
for a in x:
print sin(a)*(10*a)
The code returns the correct sin values but I'm not sure how to get the values into a new list y..
Any help would be greatly appreciated.
Use list comprehensions here
>>> y = [sin(10*i)*(i) for i in x]
try this
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y=list()
for a in x:
y.append(sin(a)*(10*a))
the following code works
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y = [i*sin(10*i) for i in x]
x=[-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y=[]
for a in x:
y.append(sin(a)*(10*a))
Should work but do try to learn python!!
Try list comprehension:
y = [a * sin(10*a) for a in x]
Hope that helps.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I am trying to take an input from a "raw_input" function and make it into 3 floats and then sum them up.
user_input = "1.23+2.25+3.25"
is it possible to take the 3 numbers and add them to a list of floats that look like this or something similar?
float_lst = [1.23,2.25,3.25]
Yes.
float_lst = [float(i) for i in user_input.split("+")]
If I only go by your requirement, not the list, you can eval. Trivial code example below
a = raw_input()
print eval(a)
You can use the split function and then cast the elements to float.
user_input = "1.23+2.25+3.25"
lst = user_input.split("+")
lst = [float(i) for i in lst]
Now you have a list of float so you can do
result = sum(lst)
And you will have the result
This question already has answers here:
How to use filter, map, and reduce in Python 3
(7 answers)
Closed 5 years ago.
I am learning the concept of filters in Python. I am running a simple code like this.
>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
But instead of getting a list, I am getting some message like this.
<filter object at 0x00FDC550>
What does this mean? Does it means that my filtered object i.e list to come out is stored at that memory location? How do I get the list which I need?
It looks like you're using python 3.x. In python3, filter, map, zip, etc return an object which is iterable, but not a list. In other words,
filter(func,data) #python 2.x
is equivalent to:
list(filter(func,data)) #python 3.x
I think it was changed because you (often) want to do the filtering in a lazy sense -- You don't need to consume all of the memory to create a list up front, as long as the iterator returns the same thing a list would during iteration.
If you're familiar with list comprehensions and generator expressions, the above filter is now (almost) equivalent to the following in python3.x:
( x for x in data if func(x) )
As opposed to:
[ x for x in data if func(x) ]
in python 2.x
It's an iterator returned by the filter function.
If you want a list, just do
list(filter(f, range(2, 25)))
Nonetheless, you can just iterate over this object with a for loop.
for e in filter(f, range(2, 25)):
do_stuff(e)