| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
我的代码是这样 
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[1:] not in temp:
 
 -          three.append(i)
 
 -          temp.append(i[1:])
 
  
- print(three)
 
  复制代码 
 
 
 |   
 
 
 
 |