各位老师这段代码中append如何才能添加不重复的数据
我的代码是这样import re
one = ['a123','b456','c123','d112','e122','f456','a123','r987','f123','b456']
two = ['123','456','789','112','122','452']
three =[]
for i in one:
if re.search(r'(\d+)',i).group() in two and i not in three:
three.append(i)
print(three)
得到的结果是这样
['a123', 'b456', 'c123', 'd112', 'e122', 'f456', 'f123']
有没有什么办法能够得到这样的结果
['a123', 'b456',, 'd112', 'e122']
就是数字部分不能重复,感谢老师能够帮我看看。谢谢了
可以拿个临时列表统计下,若存在这个临时列表则不执行 if 代码块,参考代码:
import re
one = ['a123','b456','c123','d112','e122','f456','a123','r987','f123','b456']
two = ['123','456','789','112','122','452']
three =[]
temp = []
for i in one:
if re.search(r'(\d+)',i).group() in two and i not in temp:
three.append(i)
temp.append(i)
print(three)
页:
[1]