Python 在嵌套列表中查找重复的元素
我有一个嵌套的元素列表:Python 在嵌套列表中查找重复的元素,python,list,nested-lists,Python,List,Nested Lists,我有一个嵌套的元素列表: employee_list = [ ['Name', '=', 'John'], ['Age', '=', '32'], ['Weight', '=', '60'], ['Name', '=', 'Steve'], ['Weight', '=', '85'] ] from collections import Counter c = Counter(l[0] for l in employee_list) # Counter
employee_list = [
['Name', '=', 'John'],
['Age', '=', '32'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
from collections import Counter
c = Counter(l[0] for l in employee_list)
# Counter({'Name': 2, 'Weight': 2, 'Age': 1})
uniq = [l for l in employee_list if c[l[0]] == 1]
# [['Age', '=', '32']]
rept = [l for l in employee_list if c[l[0]] > 1]
# [['Name', '=', 'John'],
# ['Weight', '=', '60'],
# ['Name', '=', 'Steve'],
# ['Weight', '=', '85']]
我想创建两个元素列表:一个包含重复元素,另一个包含唯一元素。但我也希望保持这种重复
unique_list = [['Age', '=', '32']]
repeated_list = [
['Name', '=', 'John'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
唯一性或重复性由每个子列表的第一个元素决定。例如:“名称”
,“重量”
。如果有两个子列表,其中第一个元素是“代码>‘名称’< /代码>,我将其视为重复。
有人能推荐一种简单的方法吗?您不能使用列表列表来执行
计数器,它将返回
不可损坏类型:“列表”
因此,我们需要转换为元组的列表
employee_tuple=list(map(tuple,employee_list))
# then we using Counter
from collections import Counter
d=Counter(employee_tuple)
l=list(map(d.get,employee_tuple))# get the freq of each item
l
Out[372]: [2, 1, 2, 2, 2]
# then we using filter
from itertools import compress
list(compress(employee_list, map(lambda x: x == 1, l)))
Out[380]: [['Age', '=', '32']]
list(compress(employee_list, map(lambda x: x != 1, l)))
Out[384]:
[['Name', '=', 'John'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60']]
您不能使用列表列表执行计数器
,它将返回
不可损坏类型:“列表”
因此,我们需要转换为元组的列表
employee_tuple=list(map(tuple,employee_list))
# then we using Counter
from collections import Counter
d=Counter(employee_tuple)
l=list(map(d.get,employee_tuple))# get the freq of each item
l
Out[372]: [2, 1, 2, 2, 2]
# then we using filter
from itertools import compress
list(compress(employee_list, map(lambda x: x == 1, l)))
Out[380]: [['Age', '=', '32']]
list(compress(employee_list, map(lambda x: x != 1, l)))
Out[384]:
[['Name', '=', 'John'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60']]
您可以使用a并根据重要的第一个元素的计数理解这两个列表:
employee_list = [
['Name', '=', 'John'],
['Age', '=', '32'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
from collections import Counter
c = Counter(l[0] for l in employee_list)
# Counter({'Name': 2, 'Weight': 2, 'Age': 1})
uniq = [l for l in employee_list if c[l[0]] == 1]
# [['Age', '=', '32']]
rept = [l for l in employee_list if c[l[0]] > 1]
# [['Name', '=', 'John'],
# ['Weight', '=', '60'],
# ['Name', '=', 'Steve'],
# ['Weight', '=', '85']]
更新:按“键”拆分报告
您可以使用a并根据重要的第一个元素的计数理解这两个列表:
employee_list = [
['Name', '=', 'John'],
['Age', '=', '32'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
from collections import Counter
c = Counter(l[0] for l in employee_list)
# Counter({'Name': 2, 'Weight': 2, 'Age': 1})
uniq = [l for l in employee_list if c[l[0]] == 1]
# [['Age', '=', '32']]
rept = [l for l in employee_list if c[l[0]] > 1]
# [['Name', '=', 'John'],
# ['Weight', '=', '60'],
# ['Name', '=', 'Steve'],
# ['Weight', '=', '85']]
更新:按“键”拆分报告
您可以使用多种解决方案,包括列表理解和过滤器。还可以使用集合和列表生成唯一的元素集,并将其转换回列表,如中所示
然后,在获得唯一元素列表后,可以从原始列表中筛选这些元素,以获得结果的重复列表(如果有)
请参见您可以使用多种解决方案,包括列表理解和过滤器。还可以使用集合和列表生成唯一的元素集,并将其转换回列表,如中所示
然后,在获得唯一元素列表后,可以从原始列表中筛选这些元素,以获得结果的重复列表(如果有)
请参见如果您创建了一个测试列表
,其中包含员工列表
中的所有项目
,您可以使用内置的计数
方法和计数
每个员工列表的外观[i][0]
在该列表中
如果计数=1
则我们将整个项目
附加到我们的唯一列表中
employee_list = [
['Name', '=', 'John'],
['Age', '=', '32'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
unique_list = []
repeated_list = []
test_list = []
for i in employee_list:
for j in i:
test_list.append(j)
for i in employee_list:
if test_list.count(i[0]) == 1:
unique_list.append(i)
else:
repeated_list.append(i)
print(f"Repeated: {repeated_list}")
print(f"Unique: {unique_list}")
如果您创建了一个测试列表
,其中包含员工列表
中的所有项目
,则可以使用内置的计数
方法和计数
每个员工列表[i][0]的外观
在该列表中
如果计数=1
则我们将整个项目
附加到我们的唯一列表中
employee_list = [
['Name', '=', 'John'],
['Age', '=', '32'],
['Weight', '=', '60'],
['Name', '=', 'Steve'],
['Weight', '=', '85']
]
unique_list = []
repeated_list = []
test_list = []
for i in employee_list:
for j in i:
test_list.append(j)
for i in employee_list:
if test_list.count(i[0]) == 1:
unique_list.append(i)
else:
repeated_list.append(i)
print(f"Repeated: {repeated_list}")
print(f"Unique: {unique_list}")
我使用纯numpy解决方案(我又添加了一行以使其更通用):
假设这是我们的数据:
data = np.array(data).astype(str)
data: array([['Name', '=', 'John'],
['Age', '_', '32'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60'],
['TT', '=', 'EE']], dtype='<U6')
仅重复值的矩阵:
result = np.delete(data, idx, axis=0)
result:
array([['Name', '=', 'John'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60']], dtype='<U6')
result=np.delete(数据,idx,轴=0)
结果:
数组([['Name','=','John'],
['Weight'、'='、'60'],
['Name','=','John'],
['Weight','=','60']],dtype='我使用纯numpy解决方案(我又添加了一行,使其更通用):
假设这是我们的数据:
data = np.array(data).astype(str)
data: array([['Name', '=', 'John'],
['Age', '_', '32'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60'],
['TT', '=', 'EE']], dtype='<U6')
仅重复值的矩阵:
result = np.delete(data, idx, axis=0)
result:
array([['Name', '=', 'John'],
['Weight', '=', '60'],
['Name', '=', 'John'],
['Weight', '=', '60']], dtype='<U6')
result=np.delete(数据,idx,轴=0)
结果:
数组([['Name','=','John'],
['Weight'、'='、'60'],
['Name','=','John'],
['Weight','=','60']],dtype='u尝试的方式是什么?@benvc我不认为它是完全重复的,OP不想删除重复项,他们想“分割”元素。然而,第一个答案已经为一个可能的有效解决方案提供了一个很大的提示。“重复”是指整个子列表['Weight','=','60']
是相同的还是仅仅是一个重要的元素(比如“权重”
)?您想保留原始顺序吗?您的列表将包含多少元素?(我的意思是像101001000000?)最重要的是:你尝试过什么不起作用?@Schwobasegll很抱歉我没有在问题中明确说明。我说的是“名称”、“重量”或“年龄”等元素的唯一性或重复性。你尝试的方式是什么?@benvc我不认为它是完全重复的,OP不想删除重复项,他们想“分割”元素。然而,第一个答案已经为一个可能的有效解决方案提供了一个很大的提示。“重复”是指整个子列表['Weight','=','60']
是相同的,或者只是一个重要元素(如'Weight'
)?是否要保留原始顺序?列表将包含多少元素?(我的意思是像101010011亿?)最重要的是:你试过哪些不起作用?@Schwobasegll很抱歉我没有在问题中说清楚。我说的是“名称”、“重量”或“年龄”等元素的独特性或重复性。OP在大量的评论之后编辑了这个问题,澄清了他们真正想要的是什么,使我最初的建议在这里被引用的次数减少了而不是帮助解决问题。OP在大量的评论之后编辑了问题,澄清了他们真正想要的是什么,使得我在这里引用的初始建议对解决问题没有多大帮助。感谢您的解决方案。只是出于好奇,如果我想为每一组重复的元素创建列表。这是如何实现的d、 例如:rept1=[['Name'、'='、'John']、['Name'、'='、'Steve']]以及类似的rest@Deep我将使用第一个元素作为键,使用LIT列表作为值来构建dict。我的意思是,我们在列表“rept”中得到的输出。它还具有重复(“name”和“weight”两次)。如果我需要将其分解为仅包含“名称”和“重量”的列表,或分解为仅包含一种类型元素的列表。@Deep这正是我理解它的方式。我在答案中添加了一些代码。谢谢