``` a = ['苹果', '橘子', '香蕉', ‘葡萄’] b = ['苹果', ‘越南橘’, '香蕉', '椰子']
c = [] for x in a: for y in b: if x == y: c.append(x) ```
sudoy
2020-12-18 15:09:26 +08:00
或者
a = ['苹果', '橘子', '香蕉', ‘葡萄’] b = ['苹果', ‘越南橘’, '香蕉', '椰子'] c = [x for x in a if x in b]
c 应该就是你要找的
sudoy
2020-12-18 15:10:30 +08:00
>>> a = ['apple', 'orange', 'banana'] >>> b = ['apple', 'grape', 'banana'] >>> c = [x for x in a if x in b] >>> c ['apple', 'banana']
sudoy
2020-12-18 15:20:20 +08:00
如果你要模糊匹配,可以为:
>>> a = ['apple', 'orange', 'banana'] >>> b = ['apple', 'grape', 'fresh banana'] >>> c = [] >>> for x in a: ...............for y in b: ........................if x == y or x in y: ...............................c.append(x)