如下所示的列表,包含一些具有相同内容的元素。
例如,“ A120相对于B127”和“ B127相对于A120”自然具有相同的内容,因此在这种情况下,仅“ B127相对于A120”被视为重复内容。
我想找出哪些重复项:
a_list = [
"A120 v.s. B127",
"A125 v.s. B105",
"A122 v.s. B141",
"A150 v.s. B100",
"A147 v.s. B141",
"B127 v.s. A120",
"B100 v.s. A150"
]
for a in a_list:
flip = a.split(' v.s. ')[1] + " v.s. " + a.split(' v.s. ')[0]
if flip in a_list:
print a + " is a duplicate."
但是代码标记了更多(也是第一次出现)重复项:
A120 v.s. B127 is a duplicate.
A150 v.s. B100 is a duplicate.
B127 v.s. A120 is a duplicate.
B100 v.s. A150 is a duplicate.
仅输出实际重复的方式是什么?
B127 v.s. A120 is a duplicate of A120 v.s. B127.
B100 v.s. A150 is a duplicate of A150 v.s. B100
谢谢。
答案 0 :(得分:3)
a_list = [
"A120 v.s. B127",
"A125 v.s. B105",
"A122 v.s. B141",
"A150 v.s. B100",
"A147 v.s. B141",
"B127 v.s. A120",
"B100 v.s. A150"
]
tmp=[]
for v,i in enumerate(a_list):
if v not in tmp:
var =' '.join(i.split()[::-1])
if var in a_list:
tmp.append(a_list.index(var))
print('{} is duplicate of {}'.format(i, var))
输出
A120 v.s. B127 is duplicate of B127 v.s. A120
A150 v.s. B100 is duplicate of B100 v.s. A150
答案 1 :(得分:3)
带有 hashable dict键的简短解决方案:
a_list = [
"A120 v.s. B127", "A125 v.s. B105", "A122 v.s. B141",
"A150 v.s. B100", "A147 v.s. B141", "B127 v.s. A120",
"B100 v.s. A150"
]
d = {}
for s in a_list:
items = tuple(sorted(s.split()))
if items in d:
print(s, 'is a duplicate of', d[items])
else:
d[items] = s
输出:
B127 v.s. A120 is a duplicate of A120 v.s. B127
B100 v.s. A150 is a duplicate of A150 v.s. B100
答案 2 :(得分:2)
您可以使用set
进行快速查找,并使用已排序的元组来知道某项是否重复,而与顺序无关:
a_list = [
"A120 v.s. B127",
"A125 v.s. B105",
"A122 v.s. B141",
"A150 v.s. B100",
"A147 v.s. B141",
"B127 v.s. A120",
"B100 v.s. A150"
]
seen_set = set()
for line in a_list:
pair = line.split(' v.s. ')
sorted_pair = tuple(sorted(pair))
if sorted_pair in seen_set:
print('{} is a duplicate of {}'.format(' v.s. '.join(pair), ' v.s. '.join(pair[::-1])))
seen_set.add(sorted_pair)
输出:
B127 v.s. A120 is a duplicate of A120 v.s. B127
B100 v.s. A150 is a duplicate of A150 v.s. B100
答案 3 :(得分:0)
您在这里:
a_list = [
"A120 v.s. B127",
"A125 v.s. B105",
"A122 v.s. B141",
"A150 v.s. B100",
"A147 v.s. B141",
"B127 v.s. A120",
"B100 v.s. A150"
]
for a in a_list:
flip = a.split(' v.s. ')[1] + " v.s. " + a.split(' v.s. ')[0]
if flip in a_list:
print(flip + " is a duplicate of " + a)
del a_list[a_list.index(flip)]
输出:
B127 v.s. A120 is a duplicate of A120 v.s. B127
B100 v.s. A150 is a duplicate of A150 v.s. B100