Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较从列表创建的两个字符串_Python_String_List_For Loop_If Statement - Fatal编程技术网

Python 比较从列表创建的两个字符串

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和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:
            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