Reputation: 1709
example_list = ['21.48678', '21.46552', '21.45145', '21.43822',
'21.42734', '21.41222', '21.40132', '21.37679']
Im having a bit of trouble converting this list from strings to integers, I would like to also have it as whole numbers. Thanks :)
Upvotes: 1
Views: 8557
Reputation: 143022
[int(round(float(i))) for i in example_list]
will convert items in the list to float
, round them and then convert them to int
.
Upvotes: 0
Reputation: 1
new_list=[]
for each in example_list:
Integer = int(each)
new_list.append(Integer)
print new_list
This is easy for beginner to understand:D
Upvotes: 0
Reputation: 942
foo = ['21.48678', '21.46552', '21.45145', '21.43822', '21.42734', '21.41222', '21.40132', '21.37679']
map(lambda x: int(float(x)), foo)
Upvotes: 2
Reputation: 6819
At first convert to float
>>> lst = ['21.48678', '21.46552', '21.45145', '21.43822', '21.42734', '21.41222', '21.40132', '21.37679']
>>> ints = [int(float(num)) for num in lst]
[21, 21, 21, 21, 21, 21, 21, 21]
Upvotes: 3
Reputation: 304205
Simplest thing is
[int(float(x)) for x in your_list]
This will truncate all the numbers
If you want to round the numbers use this instead
[int(float(x)+.5) for x in your_list]
Upvotes: 7