This question already has answers here:
How do I parse a string to a float or int?
(32 answers)
Closed 3 years ago.
I have a string of numbers like this
i = '584,569.2,11515,632'
want to convert it to list of number like this.
[584,569.2,11515,632]
You can do it like this:
i = '584,569.2,11515,632'
numbers = list(map(float, i.split(',')))
print(numbers)
Output:
[584.0, 569.2, 11515.0, 632.0]
Also, as Chris A pointed out, if the distinction between int and float is important, you can use is_integer():
numbers = [int(x) if x.is_integer() else x for x in map(float, i.split(','))]
Output:
[584, 569.2, 11515, 632]
Related
This question already has answers here:
How to remove even numbers from a list in Python? [duplicate]
(5 answers)
How to remove items from a list while iterating?
(25 answers)
Python: remove odd number from a list
(9 answers)
Strange result when removing item from a list while iterating over it
(8 answers)
Closed last year.
I am having some trouble while removing even integers from a list in Python. This is what I am trying to do but I can't figure out what am I doing wrong. Is the array skipping elements due to items being removed? I would really appreciate some help.
def removeEven(l):
for e in l:
if e % 2 == 0:
l.remove(e)
print(l)
Try filter:
def removeEvent(l):
return list(filter(lambda x: x % 2 == 0, l))
print(removeEvent(l))
Or even better:
print(list(filter(lambda x: x % 2 == 0, l)))
This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
How do I add together integers in a list in python?
(4 answers)
Closed 3 years ago.
My Objective: To find and print the sum of all the items in the list via function
My Code:
def list_sum(x):
if type(x)!='list':
print("Invalid List item!")
if type(x)=='list':
list_length = len(x)
total = 0
i = 0
for i in range (list_length):
total +=x[i]
i+=1
print("The sum of all items in the list is: ",total)
samples = [1,3,5,6,8,45,67,89]
list_sum(samples)
My Output:
<class 'list'>
Invalid List item!
Expected:
224
Why am I getting the output I am getting?
if type(x) != list
not
if type(x) != 'list'
This question already has answers here:
Find min, max, and average of a list
(5 answers)
Closed 5 years ago.
Let's say I have the below list:
o =[[-0.90405713, -0.86583093, -0.14048125]]
How do I find out how positive each element of o[0] is?
So,by looking at this I know that -0.14048125 is the most "positive" with respect to 0 on the number line. Is there a way to do this via a python code?
If you want the value closest to 0, you could use min with abs as key:
>>> o =[-0.90405713,-0.86583093,-0.14048125,3]
>>> min(o, key=abs)
-0.14048125
use max()
>>> o =[-0.90405713,-0.86583093,-0.14048125]
>>> max(o)
-0.14048125
This question already has answers here:
How are strings compared?
(7 answers)
Closed 6 years ago.
I have written this code in python 3.5
x="absx"
o="abcdef"
and if i am doing this operation,
x<o
False #it return's False and i think it should return True
So what is '<' doing in case of strings and why is it not returning true.
how is it comparing x and o?
The < or > would result in lexicographic comparison of the two strings:
>>> x="absx"
>>> o="abcdef"
>>> x > o
True
Lexicographic ordering is same as dictionary ordering, and basically, the operators are checking for which string would come earlier (or later) in a dictionary order. The behavior is same for both Python 2 and 3.
The final result does not depend on the size of the string, Example:
>>> "a" < "aaaaa"
True
In example above "a" would come before "aaaaa" when written in dictionary order. To compare by length of strings, use the len() function on the strings.
Lexicographic comparison. In your case o would come after x.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 6 years ago.
feed function using raw_input of sys.argv for sum of two number goes just showing in list
def sum_double(a, b):
sum = a+b
if a == b:
sum = sum*2
print sum
return sum
else :
print sum
return sum
sum_double(a = raw_input("a"),b = raw_input("b"))
if we feed input are 1 and 2 then it will showing 12 instead of 3
raw_input returns a string and not a number. With string inputs, + simply concatenates the two strings together.
'1' + '2'
# '12'
If you want to perform numeric operations (such as addition), you need to first convert the output of raw_input to a number using int (for integers) or float (for floating point numbers).
sum_double(a = int(raw_input("a")),b = int(raw_input("b")))
raw_input returns a string ('1' and '2'). Summing them gives you '12'.
In order to sum numbers, not strings, convert the strings to numbers:
sum_double(a = int(raw_input("a")),b = int(raw_input("b")))