Python 比较从列表创建的两个字符串
我想比较来自两个不同列表d1和d2的两个字符串。 如果d1中的字符串与d2相似,则返回相似的字符串 这是我的密码:Python 比较从列表创建的两个字符串,python,string,list,for-loop,if-statement,Python,String,List,For Loop,If Statement,我想比较来自两个不同列表d1和d2的两个字符串。 如果d1中的字符串与d2相似,则返回相似的字符串 这是我的密码: d1 = [['learn'],['car']] d2 = [['learn'],['motor']] str1 = ', '.join(str(i) for i in d1) str2 = ', '.join(str(j) for j in d2) for i in str1: for j in str2: if i == j:
d1 = [['learn'],['car']]
d2 = [['learn'],['motor']]
str1 = ', '.join(str(i) for i in d1)
str2 = ', '.join(str(j) for j in d2)
for i in str1:
for j in str2:
if i == j:
print str1, str2
但结果是:
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
['learn'], ['car'] ['learn'], ['motor']
我预计产出为:
['learn','learn']
^它来自str1和str2中类似的字符串
有人能帮忙吗?像这样的东西怎么样:
d1 = [['learn'],['car']]
d2 = [['learn'],['motor']]
for elem in d1:
if elem in d2:
print([elem[0],elem[0]])
使用
zip
.About解决方案的简单方法
试试看:
打印i,j
我不明白你在找什么。你想得到两个列表的交集吗?也就是说,出现在两个列表中的元素。它认为他是。
for i,j in zip(d1,d2):
if i==j:
print i,j