简体   繁体   English

如何使用python检测列表中的不同元素并从中创建新列表(查找)

[英]How to detect different elements in list and create new lists from them (finding) with python

To send an e-mail element by element I need to first detect different elements in list and then create new lists from them. 要按元素发送电子邮件元素,我需要首先检测列表中的不同元素,然后根据它们创建新列表。

Does anyone can help me about the problem below ? 有谁能帮助我解决以下问题?

The list that I mentioned is also interrelated with another one. 我提到的列表也与另一个列表相互关联。

I have: 我有:

list_I = [123, 453, 444, 555, 567, ...]

list_II = [A , A, B, C, B, ....]

What I hope to get is: 我希望得到的是:

New_list_I = [123, 453]

New_list_I_a = [A, A]
New_list_II = [444,567]

New_list_II_a = [B, B]
New_list_III = [555]

New_list_III_a = [C]

One way would be to create a dictionary from the two lists: 一种方法是从两个列表创建字典:

from collections import defaultdict
d = defaultdict(list)
for k,v in zip(list_2, list_1):
    d[k].append(v)

# defaultdict(list, {'A': [123, 453], 'B': [444, 567], 'C': [555]})

And then obtain the specified output from the dictionary using a list comprehension. 然后使用列表推导从字典中获取指定的输出。

[[[k for _ in range(len(v))], v] for k,v in d.items()]
# [[['A', 'A'], [123, 453]], [['B', 'B'], [444, 567]], [['C'], [555]]]

Note that a nested list structure is much easier to handle and to work with than rather a series of lists created dynamically. 请注意,嵌套的列表结构比动态创建的一系列列表更易于处理和使用。 Note that from this result you can select each resulting sublist using Basic Slicing and Indexing 请注意,您可以从此结果中使用“ 基本切片和索引”选择每个结果子列表

Why not just create a dict out of those two lists with a key,value and then sort on the basis of value: 为什么不只用键,值从这两个列表中创建一个字典,然后根据值排序:

list_I = [123, 453, 444, 555, 567]
list_II = ['A' , 'A', 'B', 'C', 'B']

res = dict(zip(list_I, list_II))
print(sorted(res.items(), key=lambda x:x[1]))

OUTPUT : 输出

[(123, 'A'), (453, 'A'), (444, 'B'), (567, 'B'), (555, 'C')]

暂无
暂无

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

相关问题 如何根据 Python 中 2 个不同列表的索引对元素进行配对来创建新列表? - How to create a new list by pairing the elements on the basis of index from 2 different lists in Python? 如何分隔 python 列表中的不同元素并将它们放入新列表中 - how to separate different elements in a list in python and put them in a new list 如何创建一个由两个不同的列表元素组成的新列表,而无需重复? - How to create a new list that is combination of 2 different lists elements without repetition? 如何比较 python 中的 N 个列表并创建具有唯一元素的新列表 - How to compare N lists in python and create a new list with unique elements 如何从嵌套在特定元素之间的列表中提取元素,并将它们添加到新列表中? - How to pull elements from a list that are nested between particular elements, and add them to new lists? 使用Python中不同列表中的元素填充列表 - Populating a list with elements from different lists in Python 从列表中嵌入列表的列表中查找元素(Python) - Finding elements from list in lists embedded in list (Python) 如何比较两个具有相似元素的不同列表并使用循环创建没有相似元素的新列表 - How to compare two different lists with similar elements and create a new list without the similar elements using loops 如何在循环中创建新列表并命名,以便可以使用Python将其他列表中的项目填充到列表中? - How can I create new lists in a loop and name them so I can fill them up with items from another list using Python? 如何从不同的列表中随机抽取样本并将它们组合成一个新列表? - How to take random samples from different lists and combine them into a new list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM