example_list = ['21.48678', '21.46552', '21.45145', '21.43822',
'21.42734', '21.41222', '21.40132', '21.37679']
我在将这个列表从字符串转换为整数时遇到了一些麻烦,我想将它作为整数。谢谢:)
答案 0 :(得分:7)
最简单的事情是
[int(float(x)) for x in your_list]
这会截断所有数字
如果要对数字进行舍入,请使用此代替
[int(float(x)+.5) for x in your_list]
答案 1 :(得分:2)
首先转换为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]
答案 2 :(得分:2)
foo = ['21.48678', '21.46552', '21.45145', '21.43822', '21.42734', '21.41222', '21.40132', '21.37679']
map(lambda x: int(float(x)), foo)
答案 3 :(得分:0)
[int(round(float(i))) for i in example_list]
会将列表中的项目转换为float
,围绕,然后将其转换为int
。
答案 4 :(得分:0)
new_list=[]
for each in example_list:
Integer = int(each)
new_list.append(Integer)
print new_list
初学者很容易理解:D