在python中迭代一系列列表以查找列表中的相似元素

在python中迭代一系列列表以查找列表中的相似元素,python,list,duplicates,series,Python,List,Duplicates,Series,我有一个系列,例如: ID 1 [a,b,c,d,e] 2 [b,c,d,e,f] 3 [z,t,c,d,w] 我想打印出列表中的常用项 output: [b,c,d,e] 另外,我想知道他们是哪个ID的一部分 输出: 欢迎来到StackOverflow 如果我了解您的问题,您可以利用defaultdict实现以下目的: 从集合导入defaultdict l1=['a','b','c','d','e'] l2=['b','c','d','e','f'] l3=['z','t','c','d'

我有一个系列,例如:

ID
1 [a,b,c,d,e]
2 [b,c,d,e,f]
3 [z,t,c,d,w]
我想打印出列表中的常用项

output: [b,c,d,e]
另外,我想知道他们是哪个ID的一部分
输出:


欢迎来到StackOverflow

如果我了解您的问题,您可以利用
defaultdict
实现以下目的:

从集合导入defaultdict
l1=['a','b','c','d','e']
l2=['b','c','d','e','f']
l3=['z','t','c','d','w']
输出=默认DICT(列表)
对于[l1,l2,l3]中的l:
对于l中的项目:
输出[项目]。追加(l)
output=[{k:v}表示k,如果len(v)==3,则在output.items()中输入v
打印(输出)
产出:

[
{'c':['a','b','c','d','e'],['b','c','d','e','f'],['z','t','c','d','w']},
{'d':['a','b','c','d','e'],['b','c','d','e','f'],['z','t','c','d','w']}
]

这回答了你的问题吗?

欢迎来到StackOverflow

如果我了解您的问题,您可以利用
defaultdict
实现以下目的:

从集合导入defaultdict
l1=['a','b','c','d','e']
l2=['b','c','d','e','f']
l3=['z','t','c','d','w']
输出=默认DICT(列表)
对于[l1,l2,l3]中的l:
对于l中的项目:
输出[项目]。追加(l)
output=[{k:v}表示k,如果len(v)==3,则在output.items()中输入v
打印(输出)
产出:

[
{'c':['a','b','c','d','e'],['b','c','d','e','f'],['z','t','c','d','w']},
{'d':['a','b','c','d','e'],['b','c','d','e','f'],['z','t','c','d','w']}
]

这是否回答了您的问题?

如果您创建一个字典,将索引映射到字符列表,您可以得到答案的两个部分:

from collections import defaultdict
d = defaultdict(list)
arr = [
    ['a','b','c','d','e'],
    ['b','c','d','e','f'],
    ['z','t','c','d','w']
    ]

for ind, l in enumerate(arr):
    for c in l:
        d[c].append(ind)
print(d)
d
将是一本类似以下内容的词典:

defaultdict(list,
            {'a': [0],
             'b': [0, 1],
             'c': [0, 1, 2],
             'd': [0, 1, 2],
             'e': [0, 1],
             'f': [1],
             'z': [2],
             't': [2],
             'w': [2]})
通过查看以下内容可以找到出现在多个列表中的项目:

[k for k, v in d.items() if len(v) > 1]
# ['b', 'c', 'd', 'e']
您可以直接索引到dict中,以查找它们所属的索引:

d['e']
# [0, 1]

如果创建一个字典,将索引映射到字符列表,则可以得到答案的两个部分:

from collections import defaultdict
d = defaultdict(list)
arr = [
    ['a','b','c','d','e'],
    ['b','c','d','e','f'],
    ['z','t','c','d','w']
    ]

for ind, l in enumerate(arr):
    for c in l:
        d[c].append(ind)
print(d)
d
将是一本类似以下内容的词典:

defaultdict(list,
            {'a': [0],
             'b': [0, 1],
             'c': [0, 1, 2],
             'd': [0, 1, 2],
             'e': [0, 1],
             'f': [1],
             'z': [2],
             't': [2],
             'w': [2]})
通过查看以下内容可以找到出现在多个列表中的项目:

[k for k, v in d.items() if len(v) > 1]
# ['b', 'c', 'd', 'e']
您可以直接索引到dict中,以查找它们所属的索引:

d['e']
# [0, 1]

让我们尝试一种计数方法,因为我们想要一个好的时间复杂度

l1 = ['a','b','c','d','e']
l2 = ['b','c','d','e','f']
l3 = ['z','t','c','d','w']

# create an empty dictionary
count = dict()

# start your id counter
list_id = 1    

# iterate over the lists
for lst in [l1,l2,l3]:
    # iterate over each list, getting the char
    for char in lst:
        try:
            # try to append the list id to each corresponding char
            count[char].append(list_id)
        except:
            # if the char key doesn't exist in the dict, we add it as a list
            # containing our list id in which it was first found
            count[char] = [list_id]
    # increment our list id, as we finished looking on li
    list_id = list_id + 1

# print each char and list that contains more than one list_id
for key in count:
    if len(count[key])>1:
        print(key+': '+str(count[key]))

输出将是

b: [1, 2]
c: [1, 2, 3]
d: [1, 2, 3]
e: [1, 2]

让我们尝试一种计数方法,因为我们想要一个好的时间复杂度

l1 = ['a','b','c','d','e']
l2 = ['b','c','d','e','f']
l3 = ['z','t','c','d','w']

# create an empty dictionary
count = dict()

# start your id counter
list_id = 1    

# iterate over the lists
for lst in [l1,l2,l3]:
    # iterate over each list, getting the char
    for char in lst:
        try:
            # try to append the list id to each corresponding char
            count[char].append(list_id)
        except:
            # if the char key doesn't exist in the dict, we add it as a list
            # containing our list id in which it was first found
            count[char] = [list_id]
    # increment our list id, as we finished looking on li
    list_id = list_id + 1

# print each char and list that contains more than one list_id
for key in count:
    if len(count[key])>1:
        print(key+': '+str(count[key]))

输出将是

b: [1, 2]
c: [1, 2, 3]
d: [1, 2, 3]
e: [1, 2]

1) 以适当的方式格式化您的输入;2) 发布您的初始编码,您可以提供更多详细信息或示例吗
c
d
出现在每个列表中,但
b
e
仅出现在3个列表中的2个列表中,因此不清楚给定的元素必须与您感兴趣的列表有多少不同。假设我们有100个列表,在2个列表中出现的元素对您是否感兴趣?如果不是,您需要多少外观?1)以适当的方式格式化您的输入;2) 发布您的初始编码,您可以提供更多详细信息或示例吗
c
d
出现在每个列表中,但
b
e
仅出现在3个列表中的2个列表中,因此不清楚给定的元素必须与您感兴趣的列表有多少不同。假设我们有100个列表,在2个列表中出现的元素对您是否感兴趣?如果不是的话,你需要多少次出场?我想你需要更多的循环来获得单个角色。他说“常见项目”,我假设一个列表并比较它们的项目。如果比较字符串项目中的单个字符,则需要在
项目
字符串中的每个字符上更正第三个循环。我认为您需要更多级别的循环才能获得单个字符。他说“常用项目”,我假设一个列表并比较它们的项目。如果比较字符串项目中的单个字符,则需要在
项目
字符串中的每个字符上更正第三个循环。注意:1)几乎不需要手动增加这样的id字段。只需在enumerate([l1,l2,l3],1]中为list_id,lst创建循环
,就可以删除
list_id
0
的显式初始化,以及每个循环结束时的显式增量。2) 除了s之外,不要使用裸
;您需要一个
KeyError
,只捕获它,这样您就不会忽略
TypeError
s、
KeyboardInterrupt
s等,或者只使用一个
collections.defaultdict(list)
,所以您根本不需要
try
/
除了
之外,并且可以无条件地进行
计数[char].append(list\id)
。注意:1)您几乎不需要手动增加这样的id字段。只需在enumerate([l1,l2,l3],1]中为list_id,lst创建循环
,就可以删除
list_id
0
的显式初始化,以及每个循环结束时的显式增量。2) 除了
s之外,不要使用裸
;你需要一个
KeyError
,只捕获它,这样你就不会忽略
TypeError
s、
KeyboardInterrupt
s等等,或者只使用一个
collections.defaultdict(list)
所以你根本不需要
try
/
除了
之外,而且可以无条件地做
计数[char].append(list\u id)