2
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'
4
  • which version of python are you using? Commented Jul 10, 2015 at 3:32
  • can you put complete traceback? Commented Jul 10, 2015 at 3:32
  • 1
    I find the reason ....I can't add " *1.2" to the sorted or it will go wrong. Commented Jul 10, 2015 at 3:35
  • 2
    do you want an output like this : [6248, 5282, 5165, 5130, 5099] Commented Jul 10, 2015 at 3:36

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

This fixes the TypeError and works, but to find the maximum element multiplied by 1.2 it would make a lot more sense to do max(int_a) * 1.2
I agree that max(int_a) would be better if the goal is to find the maximum element multiplied by 1.2.
I misunderstand list and number .Thinks a lot .
1

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

print max(int_a) * 1.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.