简体   繁体   English

根据列表的第一个元素从主列表创建子列表

[英]Create sublists from a master list based on the first element of the list

I have the below List:我有以下列表:

master_list =[['Apple','Red','Fresh','CA'],['Banana','Yellow','Fresh','NY'],['Mandarin','Orange','Fresh','WA'],['Apple','Red','Fresh','NJ'],['Banana','Yellow','Fresh','TX'],
['Apple','Red','Old','CA'],['Mandarin','Orange','Fresh','CA'],['Pineapple''Yellow','Fresh','MI']]

I need to loop through the above list and create separate lists based on the 0th element of each sublist in the master_list我需要遍历上面的列表并根据 master_list 中每个子列表的第 0 个元素创建单独的列表

The expected output is预期的 output 是

l_apple=[['Apple','Red','Fresh','CA'],['Apple','Red','Fresh','NJ'],['Apple','Red','Old','CA']]
l_banana=[['Banana','Yellow','Fresh','TX'],['Banana','Yellow','Fresh','NY']]
l_mandarin=[['Mandarin','Orange','Fresh','WA'],['Mandarin','Orange','Fresh','CA']]
l_pineapple=[['Pineapple''Yellow','Fresh','MI']]"

i would really appreciate any suggestions or tips to code it in Python in an efficient manner.我非常感谢任何建议或提示以有效的方式在 Python 中对其进行编码。

Please note:We do not have a fixed list of fruits.So the number of lists that need to be generated might change based on the unique values(sourced from the first element of the list in the nested list) of fruits.请注意:我们没有固定的水果列表。因此,需要生成的列表数量可能会根据水果的唯一值(源自嵌套列表中列表的第一个元素)而改变。 Thanks in advance for the help在此先感谢您的帮助

Since you have a dynamic number of fruits, you don't want to give each one a statically named variable.由于您有动态数量的水果,因此您不想给每个水果一个静态命名的变量。 Something like a dict is a better option:像 dict 这样的东西是一个更好的选择:

>>> from collections import defaultdict
>>> fruit_dict = defaultdict(list)
>>> for fruit in master_list:
...     fruit_dict[fruit[0]].append(fruit)
...
>>> dict(fruit_dict)
{
    'Apple': [['Apple', 'Red', 'Fresh', 'CA'], ['Apple', 'Red', 'Fresh', 'NJ'], ['Apple', 'Red', 'Old', 'CA']], 
    'Banana': [['Banana', 'Yellow', 'Fresh', 'NY'], ['Banana', 'Yellow', 'Fresh', 'TX']], 
    'Mandarin': [['Mandarin', 'Orange', 'Fresh', 'WA'], ['Mandarin', 'Orange', 'Fresh', 'CA']], 
    'PineappleYellow': [['PineappleYellow', 'Fresh', 'MI']]
}

As it was well said by Samwise, this is better approached by using a dictionary.正如 Samwise 所说,最好使用字典来解决这个问题。 But in case you really need to create those variables, and if you are at module scope , you can achieve this by doing the following:但是如果你真的需要创建这些变量,并且如果你在模块 scope ,你可以通过执行以下操作来实现:

master_list =[['Apple','Red','Fresh','CA'],['Banana','Yellow','Fresh','NY'],['Mandarin','Orange','Fresh','WA'],['Apple','Red','Fresh','NJ'],['Banana','Yellow','Fresh','TX'], ['Apple','Red','Old','CA'],['Mandarin','Orange','Fresh','CA'],['Pineapple','Yellow','Fresh','MI']]

for list_ in master_list:
    name = f'l_{list_[0].lower()}'
    vars().setdefault(name, []).append(list_)

outputs输出

>>> l_apple
[['Apple', 'Red', 'Fresh', 'CA'], ['Apple', 'Red', 'Fresh', 'NJ'], ['Apple', 'Red', 'Old', 'CA']]
>>> l_pineapple
[['Pineapple', 'Yellow', 'Fresh', 'MI']]
>>> l_mandarin
[['Mandarin', 'Orange', 'Fresh', 'WA'], ['Mandarin', 'Orange', 'Fresh', 'CA']]
>>> l_banana
[['Banana', 'Yellow', 'Fresh', 'NY'], ['Banana', 'Yellow', 'Fresh', 'TX']]

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

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