[英]How to convert float list to int list in python
a= [6248.570994, 5282.059503, 5165.000653, 5130.795058, 5099.376451]
one way: 单程:
a=map(int, a)
the other way: 另一种方法:
int_a=[]
for intt in a:
int_a.append(int(intt))
above ways can print right answer,but when I want sorted I met problem: 上述方法可以打印正确的答案,但是当我想排序时遇到问题:
maxx=sorted(int_a,reverse=True)[:1]*1.2
print maxx
TypeError: can't multiply sequence by non-int of type 'float'
The problem seems to be that 问题似乎是
maxx=sorted(int_a,reverse=True)[:1]*1.2
print maxx
... produces a list, not an integer and you cannot multiply a list by a floating point number. ...产生一个列表,而不是整数,并且您不能将列表乘以浮点数。 To obtain 1.2 times the maximum element in the list using this code, the following would work:
要使用此代码获得列表中最大元素的1.2倍,可以执行以下操作:
maxx=sorted(int_a,reverse=True)[0]*1.2
print maxx
... though it would be more efficient to use: ...虽然使用起来会更有效:
maxx=max(int_a)*1.2
print maxx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.