简体   繁体   English

如果子列表的第一个元素在 Python 中是唯一的,则从第一个子列表中获取前两项

[英]Get first two items from the first sublist if first element of the sublist is unique in Python

I have a list:我有一个清单:

df = [['apple', 'red', '0.2'], ['apple', 'green', '8.9'], ['apple', 'brown', '2.9'], 
      ['guava', 'green', '1.9'], ['guava', 'yellow', '4.9'], ['guava', 'light green', '2.3']]

From here I want to only get the first 2 items from the first distinct sublist given the condition that the value of the first sublist is unique.从这里我只想从第一个不同的子列表中获取前 2 个项目,条件是第一个子列表的值是唯一的。

Expected output:预期 output:

df = [['apple', 'red'], ['guava', 'green']]

Code till now:到目前为止的代码:

dummy_list = []

for item in df:
    if item[0] not in dummy_list:        
        dummy_list.append(item[:2])

This is not working and appending all the elements.这不起作用并附加所有元素。 Any help on this please请对此有任何帮助

Or smarter: use a dict and setdefault to add the mapping only for the first或者更聪明:使用 dict 和setdefault只为第一个添加映射

result = {}
for value in df:
    result.setdefault(value[0], value[:2])
result = list(result.values())

print(result)

Or you could keep a count of the added keys to avoid repeating them (in a separate list)或者您可以保留添加键的计数以避免重复它们(在单独的列表中)

keys = set()
result = []
for value in df:
    if value[0] not in keys:
        result.append(value[:2])
        keys.add(value[0])

print(result) # [['apple', 'red'], ['guava', 'green']]

You can use itertools.groupby and for the key use operator.itemgetter :您可以使用itertools.groupby和关键使用operator.itemgetter

from itertools import groupby
from operator import itemgetter

df = [['apple', 'red', '0.2'], ['apple', 'green', '8.9'], ['apple', 'brown', '2.9'], 
      ['guava', 'green', '1.9'], ['guava', 'yellow', '4.9'], ['guava', 'light green', '2.3']]

df1 = [next(g)[:2] for k, g in groupby(df, key=itemgetter(0))]

FYI itemgetter(0) is the same as lambda x: x[0] so you could use that too.仅供参考itemgetter(0)lambda x: x[0]相同,因此您也可以使用它。

When you say unique, do you mean that if you select a value, then you don't want to select it again?当你说唯一的时候,你的意思是如果你 select 一个值,那么你不想再 select 呢?

If that is the case then pop might be useful:如果是这种情况,那么 pop 可能会很有用:

import random as r
df = [['apple', 'red', '0.2'], ['apple', 'green', '8.9'], ['apple', 'brown', '2.9'], 
      ['guava', 'green', '1.9'], ['guava', 'yellow', '4.9'], ['guava', 'light green', '2.3']]

total = len(df)

targetdf = []

for value in range(2):
    position = r.randint(0,total-1)
    targetdf.append(df.pop(position)[:2])
    total-=1

#print(targetdf)

#[['apple', 'green'], ['guava', 'yellow']]

What this code does is that it selects a random position in the original list and then pops it out.这段代码的作用是在原始列表中随机选择一个 position 然后将其弹出。 This value is then saved to a new list.然后将该值保存到新列表中。

you can use defaultdict to store all the values using key-value pairs and then pick only the first value out of that list.您可以使用defaultdict使用键值对存储所有值,然后仅从该列表中选择第一个值。

from collections import defaultdict

df = [
    ["apple", "red", "0.2"],
    ["apple", "green", "8.9"],
    ["apple", "brown", "2.9"],
    ["guava", "green", "1.9"],
    ["guava", "yellow", "4.9"],
    ["guava", "light green", "2.3"],
]
temp = defaultdict(list)
for sub_list in df:
    temp[sub_list[0]].append(sub_list)

df = [value[0][:2] for _, value in temp.items()]

print(df)

Output: Output:

[['apple', 'red'], ['guava', 'green']]

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

相关问题 获取子列表的第一个元素作为 python 中的字典键 - Get first element of sublist as dictionary key in python 获取字符串子列表的第一个元素作为字典键,其中包含 python 中的值列表 - Get first element of a string sublist as a dictionary key with a list of value in python 以有效的方式从子列表列表中删除第一个元素 [python 3] - remove first element from a list of sublist in an efficient way [python 3] 如何将一个元素与 python 中子列表中的每个第一个元素进行比较? - How to compare an element to every first element in a sublist in python? 带有子列表的两个列表之间的差异是每个子列表中的第一个元素 - The difference between the two lists with sublists by the first element in each sublist Python:将子列表中的每个第一,第二,第三元素 - Python: Take Every First, Second, Third Element in Sublist 使用python将子列表中的第一个元素与同一列表中的元素合并 - Merging first element in sublist with elements in the same list using python 如何获取列表的每个子列表的第一个元素(django 模板) - how to get first element of every sublist of a list (django template) 子列表中第一项的列表 - List of the first item in a sublist 如何通过嵌套列表中第一个元素的值提取子列表 - How to extract a sublist by the value of the first element from a nested list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM