I wanted to see if there was a better way of doing the following:
I have a list of strings which may actually be floats, letters and other characters like "-" and "*":
mylist = ["34.59","0.32","-","39.29","E","0.13","*"]
I was to create a new list which iterates through mylist and checks IF an item is greater than 0.50, if it is, then that item should be rounded to the nearest whole number, if not, then it should be left alone and appended to the new list.
Here is what I have, this works but I wanted to know if there is a better way of doing it:
for item in mylist:
try:
num = float(item)
if num > 0.50:
newlist.append(str(int(round(num))))
else:
newlist.append(item)
except ValueError:
newlist.append(item)
print newlist
Output:
['35', '0.32', '-', '39', 'E', '0.13', '*']
What do you guys thing?