In your original code, you are iterating over the elements of the sublist and you are using them as indexes, since the element of the sublist is a float, when you use them as an index i.e. you do tumor_size[2.6], hence you get the error TypeError: list indices must be integers or slices, not float
Also it should be and instead of & to chain conditionals together
To fix this, you would need to iterate over the sublists of each list, and calculate the max given that the list is non-empty
tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]
#Find max of each sublist given sublist is non-empty
res = [max(li) for li in tumor_size if li]
print(res)
The output will be
[3.65, 2.9]
Or if we go with your original approach, you can get rid of the inner loop, check for maxlen only if the sublist is non-empty, and do your comparison
tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]
T_stage_list=[]
for i in tumor_size:
#Default value is T0
T_stage = "T0"
#If sublist is non-empty
if i:
#Get max and perform comparison
maxlen = max(i)
if (maxlen>2.0 and maxlen<3.0):
T_stage = "T2a"
else:
T_stage = "T2b"
#Add T_stage
T_stage_list.append(T_stage)
print(T_stage_list)
The output will be
['T2b', 'T0', 'T2a']
Answer from Devesh Kumar Singh on Stack OverflowHi, I have calculated some values and now need the program to print which is the largest. The problem is that these numbers are floats and I get an error: TypeError: 'float' object is not iterable when using the max function. And I can't change it to int for example, because I need to include numbers after the comma. I would be really grateful if someone could help me.
Finding max value in a series of floats
python - Get the max value of float numbers on a list on tuples? - Stack Overflow
python - How do I find the largest float in a list array? - Stack Overflow
python - Max value in list with decimals. - Stack Overflow
In your original code, you are iterating over the elements of the sublist and you are using them as indexes, since the element of the sublist is a float, when you use them as an index i.e. you do tumor_size[2.6], hence you get the error TypeError: list indices must be integers or slices, not float
Also it should be and instead of & to chain conditionals together
To fix this, you would need to iterate over the sublists of each list, and calculate the max given that the list is non-empty
tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]
#Find max of each sublist given sublist is non-empty
res = [max(li) for li in tumor_size if li]
print(res)
The output will be
[3.65, 2.9]
Or if we go with your original approach, you can get rid of the inner loop, check for maxlen only if the sublist is non-empty, and do your comparison
tumor_size = [[2.6,3.65],[],[2.0,2.9,1.7,2.5,1.3]]
T_stage_list=[]
for i in tumor_size:
#Default value is T0
T_stage = "T0"
#If sublist is non-empty
if i:
#Get max and perform comparison
maxlen = max(i)
if (maxlen>2.0 and maxlen<3.0):
T_stage = "T2a"
else:
T_stage = "T2b"
#Add T_stage
T_stage_list.append(T_stage)
print(T_stage_list)
The output will be
['T2b', 'T0', 'T2a']
Use @Devesh's answer if you want the maximum for all sublists, but you can also use:
print(list(map(max, filter(None, tumor_size))))
maximum of the whole list:
print(max([x for i in tumor_size for x in i]))
You can achieve this for instance with a combination of groupby and sorting:
from itertools import groupby
srt_list = sorted(erra_eus_repo, key=lambda x: x[1]);
max_list = []
for key, group in groupby(srt_list, lambda x: x[1].split('.')[0]):
max_el = max(list(group), key = lambda y: int(y[1].split('.')[1]))
max_list.append(float(max_el[1]))
First the array is sorted based on second element of each tuple to get sequences of elements with matching non-decimal number for grouping with groupby. groupby groups the elements into just that - each group will represent a sequence X.Z with common X. In each of these sequences - groups the program finds the one with maximum decimal part treated as a stand-along number. The whole number is then appended to the list with max values as a float.
Do not treat the version numbers as floating point, treat as '.' separated strings. Then split each version string (split on '.') and compare. Like this:
def normalize_version(v):
return tuple(map(int, v.split('.')))
Then you can see:
>>> u'5.11' > u'5.9'
False
>>> normalize_version(u'5.11') > normalize_version(u'5.9')
True
You are trying to sort the value of a itself, hence giving a float as argument to the sorted() function. It is the same of typing:
sorted(4.3)
As 4.3 is not a list but a float, it is not iterable.
I suggest to simply create a list and appending the value to that list, then printing the sorted list.
import math, csv
file_csv = open("statepop2.csv")
data_csv = csv.reader(file_csv)
list_csv = list(data_csv)
rows = []
for row in list_csv:
row.append(0)
row[3] = int(row[1])/math.sqrt(int(row[2])*(int(row[2])+1))
rows.append(row[3])
print(sorted(rows))
If you need some other data from your CSV (i.e. the country name?) to be displayed along with the a value, just makes rows a dict:
rows = {}
Then you can add a dict entry, having the other piece of info as key:
countryName = row[x]
rows[countryName] = row[3]
The problem is that row[3] is an integer, not a list. You cannot sort an integer.
I am not sure what you exactly want to do, but try this:
for row in list_csv:
row.append(0)
row[3] = int(row[1])/math.sqrt(int(row[2])*(int(row[2])+1))
print(max([row[3] for row in list_csv]))
Hey guys, I've been trying to figure out how to find the max value in a list using this constant in the math library (math.inf). I am aware that we can use max() as an easier method, but I am required to use math.inf.
say I have:li = ['10','20','30','40']
or
li = ['-10','-20','-30','-40']
and I want to find the largest value