简体   繁体   English

如何检索列表中第一个元素的唯一值?

[英]How can I retrieve the unique values of the first element of a list?

I have a list which looks like this: 我有一个看起来像这样的清单:

l=[(['amandviw'], ['09.01'], [11, 0, 0, 0, 0]), (['amandviw'], ['09.03'], [6, 0, 5, 0, 0]), (['amandviw'], ['10.01'], [10, 0, 0, 0, 1]), (['amandviw'], ['10.02'], [10, 0, 0, 0, 1]), (['amandviw'], ['10.03'], [10, 0, 1, 0, 0]), (['amandviw'], ['10.05'], [16, 4, 2, 0, 1]), (['amandviw'], ['10.06'], [23, 0, 0, 0, 0]), (['amandviw'], ['10.07'], [8, 0, 0, 0, 3]), (['archerj'], ['09.01'], [0, 0, 0, 0, 0]), (['archerj'], ['09.03'], [0, 0, 0, 0, 0]), (['archerj'], ['10.01'], [0, 0, 0, 0, 0]), (['archerj'], ['10.02'], [0, 0, 0, 0, 0]), (['archerj'], ['10.03'], [0, 0, 0, 0, 0]), (['archerj'], ['10.05'], [0, 1, 0, 0, 0]), (['archerj'], ['10.06'], [1, 0, 0, 0, 0]), (['archerj'], ['10.07'], [0, 0, 0, 0, 0]), (['dwickett'], ['09.01'], [9, 0, 2, 0, 1]), (['dwickett'], ['09.03'], [5, 1, 0, 0, 6]), (['dwickett'], ['10.01'], [12, 0, 0, 0, 0]), (['dwickett'], ['10.02'], [10, 0, 2, 0, 0]), (['dwickett'], ['10.03'], [11, 0, 0, 0, 1]), (['dwickett'], ['10.05'], [13, 4, 2, 0, 6]), (['dwickett'], ['10.06'], [21, 1, 0, 0, 3]), (['dwickett'], ['10.07'], [8, 0, 3, 0, 1]), (['gandhi7'], ['09.01'], [27, 0, 0, 0, 1]), (['gandhi7'], ['09.03'], [28, 0, 0, 0, 0]), (['gandhi7'], ['10.01'], [27, 0, 0, 0, 1]), (['gandhi7'], ['10.02'], [27, 0, 0, 0, 1]), (['gandhi7'], ['10.03'], [26, 0, 1, 0, 1]), (['gandhi7'], ['10.05'], [37, 9, 3, 0, 8]), (['gandhi7'], ['10.06'], [55, 1, 0, 0, 1]), (['gandhi7'], ['10.07'], [15, 0, 10, 0, 3]), (['lammp'], ['09.01'], [12, 0, 0, 0, 3]), (['lammp'], ['09.03'], [13, 0, 0, 0, 2]), (['lammp'], ['10.01'], [12, 0, 0, 0, 3]), (['lammp'], ['10.02'], [12, 0, 0, 0, 3]), (['lammp'], ['10.03'], [12, 0, 0, 0, 3]), (['lammp'], ['10.05'], [17, 3, 2, 0, 9]), (['lammp'], ['10.06'], [26, 0, 1, 0, 4]), (['lammp'], ['10.07'], [5, 0, 4, 0, 6]), (['lee1131'], ['09.01'], [9, 0, 0, 0, 1]), (['lee1131'], ['09.03'], [7, 0, 1, 0, 2]), (['lee1131'], ['10.01'], [9, 0, 0, 0, 1]), (['lee1131'], ['10.02'], [9, 0, 0, 0, 1]), (['lee1131'], ['10.03'], [8, 0, 1, 0, 1]), (['lee1131'], ['10.05'], [16, 1, 2, 0, 2]), (['lee1131'], ['10.06'], [20, 0, 0, 0, 1]), (['lee1131'], ['10.07'], [7, 0, 2, 0, 1]), (['pzibley'], ['09.01'], [7, 2, 0, 0, 1]), (['pzibley'], ['09.03'], [8, 0, 2, 0, 0]), (['pzibley'], ['10.01'], [8, 1, 0, 0, 1]), (['pzibley'], ['10.02'], [9, 0, 0, 0, 1]), (['pzibley'], ['10.03'], [9, 0, 0, 0, 1]), (['pzibley'], ['10.05'], [13, 1, 3, 0, 4]), (['pzibley'], ['10.06'], [16, 0, 1, 0, 4]), (['pzibley'], ['10.07'], [4, 0, 4, 0, 2]), (['qiu58'], ['09.01'], [35, 0, 0, 0, 1]), (['qiu58'], ['09.03'], [35, 0, 0, 0, 1]), (['qiu58'], ['10.01'], [35, 0, 0, 0, 1]), (['qiu58'], ['10.02'], [35, 0, 0, 0, 1]), (['qiu58'], ['10.03'], [27, 0, 7, 0, 2]), (['qiu58'], ['10.05'], [40, 20, 4, 0, 9]), (['qiu58'], ['10.06'], [58, 13, 0, 0, 2]), (['qiu58'], ['10.07'], [23, 0, 11, 0, 2]), (['yeggy'], ['09.01'], [11, 0, 0, 0, 0]), (['yeggy'], ['09.03'], [10, 0, 0, 0, 2]), (['yeggy'], ['10.01'], [11, 0, 0, 0, 0]), (['yeggy'], ['10.02'], [11, 0, 0, 0, 0]), (['yeggy'], ['10.03'], [10, 1, 0, 0, 0]), (['yeggy'], ['10.05'], [17, 3, 3, 0, 2]), (['yeggy'], ['10.06'], [21, 0, 2, 0, 2]), (['yeggy'], ['10.07'], [6, 0, 5, 0, 1])]

I want to retrieve the unique values odf the first element of each item so I am looking for something like this: 我想检索每个项目的第一个元素的唯一值odf,所以我正在寻找像这样的东西:

l_unique=('dwickett', 'amandviw', 'yeggy', 'pzibley', 'lammp', 'gandhi7', 'archerj')

You can use a set comprehension: 您可以使用集合理解:

l = [ (['dwickett'], ['09.01'], [9, 0, 2, 0, 1]), 
(['dwickett'], ['09.03'], [5, 1, 0, 0, 6]), 
(['amandviw'], ['09.03'], [6, 0, 5, 0, 0]),
(['amandviw'], ['09.03'], [6, 0, 5, 0, 0])  ]
s = {item[0][0] for item in l}

Result: 结果:

>>> s
{'dwickett', 'amandviw'}

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

相关问题 如何将值列表添加到字典中,其中列表中的第一个元素属于第一个键等等......? - How can I add a list of values to a dictionary where the first element in the list belongs to the first key and so on...? 如何从列表中检索最小唯一值? - How to retrieve minimum unique values from list? lxml 如何定位和检索多个元素值? - lxml How Can I Target And Retrieve Multiple Element Values? 如何拆分嵌套列表元素的第一个元素? - How can I split the first element of a nested list element? 如何从21个唯一值列表中获得3个唯一值列表? - How can I get 3 lists of unique values from a list of 21 unique values? 如何替换PYTHON列表中每行的第一个元素? - How can I replace the first element of each line in a list in PYTHON? 我如何取出(或切片)秩2张量中的元素,其第一个元素是唯一的? - How can I take out(or slice) the elements in a rank-2 tensor , whose first element is unique? 如何在dict python 3.4的值列表中找到第一个唯一项 - How to find the first unique item in a list of values in a dict python 3.4 如何使用索引列表从 numpy arrays 列表中检索值? - How can I retrieve values from a list of numpy arrays with a list of indices? 使用循环和条件时如何从列表中获取唯一值 - How can I get unique values from a list when I use loop and condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM