如何遍历列表的列?

我正在尝试遍历列表列表。我想找到列表中列的最小值和最大值。


这是清单:


['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]

我试图遍历从li[1]到 的每一列li[-1]。


我试过了:


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

输出:


7.25703764

2.66171813

我得到了第一列,但我正在努力遍历下一列。


茅侃侃
浏览 239回答 3
3回答

沧海一幻觉

您可以使用zip. 鉴于您的数据集为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:]:&nbsp; &nbsp; print(max(t), min(t))7.25703764 2.6617181310.71182728 7.4601435660.949957848 0.49088007272.78334045 52.339527130.910550177 0.4270108640.301693261 -0.1946701260.457737535 0

潇湘沐

下面的代码使用列表理解创建了一个包含每个列表最小值和最大值的元组列表,您可以进一步格式化以在每个元组中包含国家/地区名称。li = [['Afghanistan', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523],&nbsp; &nbsp; ['Albania', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535],&nbsp; &nbsp; ['Algeria', 5.248912334, 9.540244102, 0.806753874, 65.69918823, 0.436670482, -0.194670126, 0],&nbsp; &nbsp; ['Argentina', 6.039330006, 9.843519211, 0.906699121, 67.53870392, 0.831966162, -0.186299905, 0.305430293],&nbsp; &nbsp; ['Armenia', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.246900991],&nbsp; &nbsp; ['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)

富国沪深

columns=len(newlist[0])rows=len(newlist)for i in range(1,columns):&nbsp; &nbsp; for j in range(rows):&nbsp; &nbsp; &nbsp; &nbsp; small.append(newlist[j][i])&nbsp; &nbsp; print("{}. columuns min value:{} max value:{}".format(i,min(small),max(small)))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python