redaiconglin 发表于 2022-4-29 16:36:13

各位老师append如何才能不添加重复的数据

我的代码是这样的
import re
one = ['a123','b456','c789','d112','e122','f452','a123','r987','f452','b456']
two = ['123','456','789','112','122','452']
three =[]
for i in one:
    if re.search(r'(\d+)',i).group() in two:
      three.append(i)
print(three)
得出的结果是
['a123', 'b456', 'c789', 'd112', 'e122', 'f452', 'a123', 'f452', 'b456']
如何才能得到这样的结果
['a123', 'b456', 'c789', 'd112', 'e122', 'f452']
谢谢老师们能帮我看看谢谢了

isdkz 发表于 2022-4-29 16:37:58

import re
one = ['a123','b456','c789','d112','e122','f452','a123','r987','f452','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)

redaiconglin 发表于 2022-4-29 17:24:05

isdkz 发表于 2022-4-29 16:37


老师不好意思。还有一个问题还是需要请教你。请老师帮我再看看谢谢了
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']
就是数字部分不能重复
页: [1]
查看完整版本: 各位老师append如何才能不添加重复的数据