Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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 合并两个列表并以CONLL数据格式写入文本文件_Python_Merge - Fatal编程技术网

Python 合并两个列表并以CONLL数据格式写入文本文件

Python 合并两个列表并以CONLL数据格式写入文本文件,python,merge,Python,Merge,我有两个列表,如下所示: pos_tag(word_tokenize('This shoe is of Blue color.')) [('This', 'DT'), ('shoe', 'NN'), ('is', 'BEZ'), ('of', 'IN'), ('Blue', 'JJ-TL'), ('color', 'NN'), ('.', '.')] custom_tags('This shoe is of Blue color.') Out[125]: [('This', '

我有两个列表,如下所示:

pos_tag(word_tokenize('This shoe is of Blue color.'))

[('This', 'DT'),
 ('shoe', 'NN'),
 ('is', 'BEZ'),
 ('of', 'IN'),
 ('Blue', 'JJ-TL'),
 ('color', 'NN'),
 ('.', '.')]

custom_tags('This shoe is of Blue color.')
Out[125]: 
[('This', 'Other'),
 ('shoe', 'Product'),
 ('is', 'Other'),
 ('of', 'Other'),
 ('Blue', 'Color'),
 ('color', 'Other'),
 ('.', 'Other')]
LEICESTERSHIRE NNP I-NP I-ORG
TAKE NNP I-NP O
OVER IN I-PP O
AT NNP I-NP O
TOP NNP I-NP O
AFTER NNP I-NP O
INNINGS NNP I-NP O
VICTORY NN I-NP O
它们由两个函数返回。 现在我想将它们合并为一个,最后以CONLL格式写入一个文本文件,如下所示:

pos_tag(word_tokenize('This shoe is of Blue color.'))

[('This', 'DT'),
 ('shoe', 'NN'),
 ('is', 'BEZ'),
 ('of', 'IN'),
 ('Blue', 'JJ-TL'),
 ('color', 'NN'),
 ('.', '.')]

custom_tags('This shoe is of Blue color.')
Out[125]: 
[('This', 'Other'),
 ('shoe', 'Product'),
 ('is', 'Other'),
 ('of', 'Other'),
 ('Blue', 'Color'),
 ('color', 'Other'),
 ('.', 'Other')]
LEICESTERSHIRE NNP I-NP I-ORG
TAKE NNP I-NP O
OVER IN I-PP O
AT NNP I-NP O
TOP NNP I-NP O
AFTER NNP I-NP O
INNINGS NNP I-NP O
VICTORY NN I-NP O
仅在我的情况下,输出为:

This    DT  Other
shoe    NN  Product
is  BEZ Other
of  IN  Other
Blue    JJ-TL   Color
Color   NN  Other
我已尝试使用以下方法进行此操作:

list(zip(pos_tag(word_tokenize(sentence)),custom_tags(sentence)))
但这给了我:

[(('This', 'DT'), ('This', 'Other')),
 (('footwear', 'NN'), ('footwear', 'Product')),
 (('is', 'BEZ'), ('is', 'Other')),
 (('of', 'IN'), ('of', 'Other')),
 (('blue', 'JJ'), ('blue', 'Color')),
 (('color', 'NN-HL'), ('color', 'Other'))]
有人能帮我得到想要的输出吗?我还需要把每个输出都写进一个文本文件,在每行之间加上分隔符。

理解一下

l1=[('This', 'DT'), ('shoe', 'NN'), ('is', 'BEZ'), ('of', 'IN'), ('Blue', 'JJ-TL'), ('color', 'NN'), ('.', '.')]
l2=[('This', 'Other'), ('shoe', 'Product'), ('is', 'Other'), ('of', 'Other'), ('Blue', 'Color'), ('color', 'Other'), ('.', 'Other')]

l3=[(x[0][0],x[0][1],x[1][1]) for x in zip(l1, l2)]

为什么不尝试使用append,即使它不是最优雅的方式

    A =

    [('This', 'DT'),
     ('shoe', 'NN'),
     ('is', 'BEZ'),
     ('of', 'IN'),
     ('Blue', 'JJ-TL'),
     ('color', 'NN'),
     ('.', '.')]

    B =
    [('This', 'Other'),
     ('shoe', 'Product'),
     ('is', 'Other'),
     ('of', 'Other'),
     ('Blue', 'Color'),
     ('color', 'Other'),
     ('.', 'Other')]

    Title = 
    [('This', ),
     ('shoe', ),
     ('is', ),
     ('of', ),
     ('Blue', ),
     ('color', ),
     ('.', )]

    for j, item in enumerate(A):
        Title[j].append(item)
        Title[j].append(B[j][1])

    for tuple in Title:
        line = '{0[0]} {0[1]} {0[2]}'.format(tuple)
对于写入文件,请使用open() 比如说,

    f = open('This/is/your/destination/file.txt', 'w')
    # Here you do something

    f.write( )
    f.close()