问题
I am trying to loop through a list of lists. I want to find the min and max of the columns in the list.
This is the list:
['Afghanistan', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523]
['Albania', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535]
['Algeria', 5.248912334, 9.540244102, 0.806753874, 65.69918823, 0.436670482, -0.194670126, 0]
['Argentina', 6.039330006, 9.843519211, 0.906699121, 67.53870392, 0.831966162, -0.186299905, 0.305430293]
['Armenia', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.246900991]
['Australia', 7.25703764, 10.71182728, 0.949957848, 72.78334045, 0.910550177, 0.301693261, 0.45340696]
I am trying to loop through each column starting from li[1]
to li[-1]
.
I have tried:
j = 1
small = []
while j < len(li)
for i in range(0, len(newlist)):
small.append(newlist[i][j])
print(max(small))
print(min(small))
j = j + 1
Output:
7.25703764
2.66171813
I get the first column but i'm struggling to loop through the next columns.
回答1:
You can use zip
. Given your dataset as my_list
:
my_lists = [['Afghanistan', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523],
['Albania', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535],
['Algeria', 5.248912334, 9.540244102, 0.806753874, 65.69918823, 0.436670482, -0.194670126, 0],
['Argentina', 6.039330006, 9.843519211, 0.906699121, 67.53870392, 0.831966162, -0.186299905, 0.305430293],
['Armenia', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.246900991],
['Australia', 7.25703764, 10.71182728, 0.949957848, 72.78334045, 0.910550177, 0.301693261, 0.45340696]]
for t in list(zip(*my_lists))[1:]:
print(max(t), min(t))
7.25703764 2.66171813
10.71182728 7.460143566
0.949957848 0.490880072
72.78334045 52.33952713
0.910550177 0.427010864
0.301693261 -0.194670126
0.457737535 0
回答2:
columns=len(newlist[0])
rows=len(newlist)
for i in range(1,columns):
for j in range(rows):
small.append(newlist[j][i])
print("{}. columuns min value:{} max value:{}".format(i,min(small),max(small)))
回答3:
The below code creates a list of tuples with min and max values of each list using list comprehension, you may do further formatting to have country name in each tuple as well.
li = [['Afghanistan', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523],
['Albania', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535],
['Algeria', 5.248912334, 9.540244102, 0.806753874, 65.69918823, 0.436670482, -0.194670126, 0],
['Argentina', 6.039330006, 9.843519211, 0.906699121, 67.53870392, 0.831966162, -0.186299905, 0.305430293],
['Armenia', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.246900991],
['Australia', 7.25703764, 10.71182728, 0.949957848, 72.78334045, 0.910550177, 0.301693261, 0.45340696]]
li2 = [(min(y),max(y)) for y in [[x for x in l[1:]] for l in li]]
print (li2)
来源:https://stackoverflow.com/questions/54822608/how-to-loop-through-columns-of-a-list