简体   繁体   English

比较python中列表列表中的元素并找到匹配项

[英]Compare elements in a list of lists in python and finding a match

I have a list of lists in Python with each list containing 5 elements. 我在Python中有一个列表列表,每个列表包含5个元素。 For example 例如

lists = [['x1', 'y1', 'z1', '10', ''],
         ['x1', 'y1', 'z1', '', '5'], 
         ['x2', 'y2', 'z2', '10', ''],
         ['x2', 'y2', 'z2', '10', ''],
         ['x1', 'y1', 'z1', '', '5'],
         ['x3', 'y3', 'z3', '', '40'],
         ['x2', 'y2', 'z2', '', '20']]

I wanted to compare the first 3 elements of each lists and if there is a match, then for the matched lists, I wanted to add the 4th column of every rows and compare it with the sum of 5th column of matched lists. 我想比较每个列表的前3个元素,如果有匹配项,那么对于匹配列表,我想添加每行的第4列并将其与匹配列表第5列的总和进行比较。 I will need to output the rows if there is a match in the sum of 4th and 5th columns in the set of matched lists. 如果匹配列表集中第4列和第5列的总和匹配,则需要输出行。

So in the above example, output should be 因此,在上面的示例中,输出应为

output = [['x1', 'y1', 'z1', '10', '10'],
          ['x2', 'y2', 'z2', '20', '20']]           

Can someone provide a solution for this. 有人可以为此提供解决方案吗?

Thanks 谢谢

A one-line solution! 一线解决方案!

output = [l[:4] + [str(sum(int(j[4]) for j in lists if j[:3] == l[:3] and j[4]))] for l in (l for l in lists if l[3])]

which gives output as: output为:

[['x1', 'y1', 'z1', '10', '10'], ['x2', 'y2', 'z2', '20', '20']]

Instead of trying to explain that line, I have expanded the process into a larger for-loop Scroll down for a proper explanation of how it is working. 我没有尝试解释该行,而是将过程扩展为更大的for-loop向下滚动以获取有关其工作方式的正确解释。

output = []
for l in (l for l in lists if l[3]):
    sm = 0
    for j in lists:
         if j[:3] == l[:3] and j[4]:
              sm += int(j[4])
    sm = str(sm)
    if sm == l[3]:
        output.append(l[:4] + [sm])

how? 怎么样?

Well, we are looping through the lists in the generator: l for l in lists if l[3] . 好吧,我们正在遍历生成器l for l in lists if l[3] Essentially, we just want to start looping through the lists that actually have a value in the third index so that we can sum up the other lists which have the same first three elements and compare this to l[3] . 本质上,我们只想开始遍历实际上在第三个索引中具有值的列表,以便我们可以对其他具有相同前三个元素的列表进行汇总,并将其与l[3]进行比较。

We then declare a sum variable ( sm ) as 0 which we will add the other lists elements to. 然后,将sum变量( sm )声明为0 ,并将其他列表元素添加到该变量。

Next we want to begin looping through the other lists in lists which may have the same first three elements. 接下来,我们要开始遍历lists中可能具有相同的前三个元素的其他列表。

We then check if the first three elements are the same ( j[:3] == l[:3] ) and also check if there is actually something to add to the sum in the fourth element (otherwise we will get an error when trying to convert an empty string to an int ). 然后,我们检查前三个元素是否相同( j[:3] == l[:3] ),还检查第四个元素的总和是否确实有东西要累加(否则,当尝试将空字符串转换为int )。 Then, if it passes this test, we add j[4] to sm and continue onto the next list. 然后,如果通过了此测试,我们将j[4]添加到sm并继续进入下一个列表。

Next we convert the sum ( sm ) to a string since that is the data type it needs to be in the end and saves converting l[3] to an integer. 接下来,将sum( sm )转换为字符串,因为这是末尾所需的数据类型,并保存将l[3]转换为整数。

Finally we compare this string to the fourth element in our original list - if it is the same, then we append the original list along with the sum (using list concatenation) to the output list. 最后,我们将此字符串与原始列表中的第四个元素进行比较-如果相同,则将原始列表以及总和(使用列表串联)附加到output列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM