简体   繁体   English

我想追加到新列表中,仅添加第三个元素与第一个元素的第三个元素相同的元组

[英]I want to append to a new list , only the tuples in which the third element is the same with the third element of the first tuple

After a sort to a list of tuples, for example: 在对元组列表进行排序之后,例如:

jen =  (34,54,15) , (34,65,15), (34, 78,89) ...

I am trying to append to a new list only the tuples which contain the third element of the first tuple for instance the new list must contain only: 我试图将仅包含第一个元组的第三个元素的元组追加到新列表中,例如,新列表必须仅包含:

(34,54,15) 
(34,65,15)

Here is my pseudocode: 这是我的伪代码:

   Salvation_array = []
    For lines in  jen:
    Num = 0
    If jen[0] [:-2] = Num:
    Salvation_array.append(Num)

I am really confused with this can you help , suggest something? 我对此很困惑,您能帮忙,提些建议吗?

You can do that with filter method easily. 您可以使用filter方法轻松地做到这一点。

In one liner filter call: 在一个衬管filter调用中:

array1 = [(34,54,15) , (34,65,15), (34, 78,89)]
array2 = filter(lambda element: element[2] == array[0][2], array)
print array2
[(34, 54, 15), (34, 65, 15)]

You can check the documentation of filter here . 您可以在此处查看filter的文档。 So, basically what filter( some_function(e) , array) does it it iterates through each element e of array and test if it satisfies some condition. 因此,基本上, filter( some_function(e) , array)执行什么操作,以迭代遍历array的每个元素e并测试其是否满足某些条件。 This check is done using the call some_function(e) . 使用调用some_function(e)完成此检查。 If that function returns True , the element e is kept, otherwise not. 如果该函数返回True ,则保留元素e ,否则不保留。

Also, since you mentioned that you are python novice, I guess you might not know about lambda in python. 另外,由于您提到您是python新手,所以我想您可能不了解python中的lambda So basically can take them as one liner nameless functions. 因此基本上可以将它们作为一种无名函数。 You can think of following to be equivalent: lambda x: print x and def printit(x): print x; 您可以认为以下内容等效: lambda x: print xdef printit(x): print x; You can check about lambda here 您可以在这里查看有关lambda 信息

Hope it helps :) 希望能帮助到你 :)

If I understood the requirements correctly, this should do what you want: 如果我正确理解了要求,那么应该执行您想要的操作:

jen = (34,54,15), (34,65,15), (34, 78,89)

salvation_army = [j for j in jen if j[2] == jen[0][2]]

print(salvation_army)

# Output:
# [(34, 54, 15), (34, 65, 15)]

It's similar to @Harsh's answer but uses list comprehension rather than filter . 它类似于@Harsh的答案,但使用列表理解而不是filter (This is a matter of taste.) (这是一个品味问题。)

Something more along the lines of what you were trying to do: 与您正在尝试做的事情类似的事情:

salvation_army = []
desired_value = jen[0][2]

for line in jen:
    if line[2] == desired_value:
        salvation_army.append(line)

暂无
暂无

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

相关问题 Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 通过添加元组的第二个和第三个元素对元组列表进行排序 - Sorting a list of tuples by the addition of second and third element of the tuple 按元组的第二个和第三个值对三元素元组列表进行排序和计数 - Sort & count list of 3-element tuples by second and third value of a tuple 如何首先相对于元组的第二个元素订购元组列表,然后相对于 python 中的元组的第三个元素订购元组列表? - How to order a list of tuple first with respect to second element of the tuple and then with respenct to third element of the tuple in python? 给定一个元组列表,如果第一个元素相同,我如何在每个相似的元组中添加第二个元素 - Given a list of tuples, if the first element is the same, how do i add the second element inside each similar tuple 查找元组列表中第一项和第三项相同的元组数 - Finding count of tuples with same first and third item in list of tuples 如果第一个元素相同,如何将列表的每三个元素相乘? - How to multiply every third element of a list together if the first elements are the same? 只有从 python 列表打印到 csv 文件的第一个和第三个元素 - Only the first and third element printed from python list to csv file 元组列表中元组的小写第一个元素 - Lowercase first element of tuple in list of tuples 访问元组列表中元组第一个元素的范围 - Accessing a range of the first element of a tuple in a list of tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM