楊宇恩
楊宇恩

Reputation: 119

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'

Upvotes: 2

Views: 9183

Answers (2)

Anuvrat Parashar
Anuvrat Parashar

Reputation: 3100

Any specific reason why you are not using max? It your statement could simply be:

print max(int_a) * 1.2

Upvotes: 1

Simon
Simon

Reputation: 10841

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:

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

Upvotes: 3

Related Questions