繁体   English   中英

将二元组列表转换为字符串列表

[英]Convert a list of bigram tuples to a list of strings

我正在尝试创建句子的 Bigram 标记。 我有一个元组列表,例如

tuples = [('hello', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'bob')]

我想知道是否有一种方法可以使用 python 将其转换为列表,所以看起来会很喜欢这个:

list = ['hello my', 'my name', 'name is', 'is bob']

谢谢你

试试这个片段:

list = [' '.join(x) for x in tuples]

join是一个字符串方法,它在''括号中定义的分隔符内连接列表(元组)的所有项目。

尝试这个

list = [ '{0} {1}'.format(t[0],t[1]) for t in tuples ]

一般来说,如果你想使用一个元组的两个值,你可以使用这样的东西:

my_list = []
for (first, second) in tuples:
    my_list.append(first+ ' '+ second)

在这种情况下

my_list = [' '.join(x) for t in tuples]

应该没事

元组 = [('hello', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'bob')] result=[] [result. append(k+" "+v) for k,v in tuple]

print(result)

output:

['hello my', 'my name', 'name is', 'is bob']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM