Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
如果yes值为1,则匹配两个单词,否则为0 python或Linux_Python - Fatal编程技术网

如果yes值为1,则匹配两个单词,否则为0 python或Linux

如果yes值为1,则匹配两个单词,否则为0 python或Linux,python,Python,我有两个不同的文件。我想把字母配对。如果字母匹配,则值为1,其余为0 file1.txt a b c d e file2.txt a b c d e 我的预期产出 file3.txt a b c d e a 1 0 0 0 0 b 0 1 0 0 0 c 0 0 1 0 0 d 0 0 0 1 0 e 0 0 0 0 1 如何生成这样的矩阵?以下是代码: with open( 'file1.txt' ) as fin : file1 = fin.read().split

我有两个不同的文件。我想把字母配对。如果字母匹配,则值为1,其余为0

file1.txt
a
b
c
d
e

file2.txt
a b c d e 
我的预期产出

file3.txt

  a b c d e 
a 1 0 0 0 0
b 0 1 0 0 0
c 0 0 1 0 0
d 0 0 0 1 0 
e 0 0 0 0 1
如何生成这样的矩阵?

以下是代码:

with open( 'file1.txt' ) as fin :
    file1 = fin.read().split()

with open( 'file2.txt' ) as fin :
    file2 = fin.read().split()

print ' '.join( [' '] + file1 )
for ch in file2 :
    print ' '.join( [ch] + ['1' if ch == i else '0' for i in file1] )
结果如下:

  a b c d e
a 1 0 0 0 0
b 0 1 0 0 0
c 0 0 1 0 0
d 0 0 0 1 0
e 0 0 0 0 1

这与
linux
sh
有什么关系?到目前为止,您编写的代码在哪里,您有哪些具体问题?StackOverflow在这里帮助您解决您编写的代码中遇到的任何问题,而不是要求其他人为您编写代码(尽管他们有时会回答您的问题)。请分享你所做的尝试,并指出你在让它工作时遇到的任何问题。非常感谢你宝贵的帮助。这对我来说真是一个很大的帮助。在print之后出现了一个错误,必须使用()print(“”.join(['']+file1))print(“”.join([ch]+[1'如果ch==i,则文件1中的i为“0”)@jonson如果使用python3,则必须使用
print(…)
,而不仅仅是
print