How to do elif statments more elegantly if appending to array in python
Posted
by
user1741339
on Stack Overflow
See other posts from Stack Overflow
or by user1741339
Published on 2012-10-12T14:04:36Z
Indexed on
2012/10/12
15:37 UTC
Read the original article
Hit count: 175
I am trying to do a more elegant version of this code. This just basically appends a string to categorynumber depending on the number. Would appreciate any help.
number = [100,150,200,500]
categoryNumber = []
for i in range (0,len(number)):
if (number [i] >=1000):
categoryNumber.append('number > 1000')
elif (number [i] >=200):
categoryNumber.append('200 < number < 300')
elif (number [i] >=100):
categoryNumber.append('100 < number < 200')
elif (number [i] >=50):
categoryNumber.append('50 < number < 100')
elif (number [i] < 50):
categoryNumber.append('number < 50')
for i in range(0,len(categoryNumber)):
print i
© Stack Overflow or respective owner