如何在python中比较多个列表的相同索引处的变量

时间:2019-07-09 20:37:35

标签: python-3.x

y 我有多个列表,希望从同一列表中找到最大数量。

i = 0
list = [""]

# open file and read the content in a list
with open('/media/saqib/Study/Angular/weatherfiles/weatherfiles/Murree_weather_2004_Aug.txt', 'r') as filehandle:  

    for line in filehandle:

        # remove linebreak which is the last character of the string
        list.append(line.strip().split(','))

        print list[i]
        i = i+1

      # add item to the list

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解您的问题,但这是一个简单的示例:

df1['addedList']=[testList]*df1.shape[0]
df1['addedList1']=[['aa', 'bb', 'cc'] for x in range(len(df1))]


df1.addedList[0][0]=11111
df1
Out[162]: 
   a  b            addedList    addedList1
0  a  d  [11111, bb, cc, dd]  [aa, bb, cc]
1  b  e  [11111, bb, cc, dd]  [aa, bb, cc]
2  c  f  [11111, bb, cc, dd]  [aa, bb, cc]
df1.addedList1[0][0]=11111
df1
Out[164]: 
   a  b            addedList       addedList1
0  a  d  [11111, bb, cc, dd]  [11111, bb, cc]
1  b  e  [11111, bb, cc, dd]     [aa, bb, cc]
2  c  f  [11111, bb, cc, dd]     [aa, bb, cc]

我的理解是,您想要一个包含每个索引最大数量的列表。如果是这样,您可以运行以下命令:

A = [5, 8, 2, 0, 9]
B = [3, 6, 8, 5, 3]
C = [1, 3, 6, 9, 5]

输出将是:

[max(a, b, c) for a, b, c in zip(A, B, C)]

答案 1 :(得分:0)

如果我了解您正在寻找要在特定索引处找到最大数目的代码,则以下程序应该可以完成您想要的操作。在其中对其进行评论以解释其作用:

join