[英]Convert a list of bigram tuples to a list of strings
I am trying to create Bigram tokens of sentences.我正在尝试创建句子的 Bigram 标记。 I have a list of tuples such as
我有一个元组列表,例如
tuples = [('hello', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'bob')]
and I was wondering if there is a way to convert it to a list using python, so it would look love this:我想知道是否有一种方法可以使用 python 将其转换为列表,所以看起来会很喜欢这个:
list = ['hello my', 'my name', 'name is', 'is bob']
thank you谢谢你
Try this snippet:试试这个片段:
list = [' '.join(x) for x in tuples]
join
is a string method that contatenates all items of a list (tuple) within a separator defined in ''
brackets. join
是一个字符串方法,它在''
括号中定义的分隔符内连接列表(元组)的所有项目。
Try this尝试这个
list = [ '{0} {1}'.format(t[0],t[1]) for t in tuples ]
In general if you want to use both values of a tuple, you can use something like this:一般来说,如果你想使用一个元组的两个值,你可以使用这样的东西:
my_list = []
for (first, second) in tuples:
my_list.append(first+ ' '+ second)
In this case在这种情况下
my_list = [' '.join(x) for t in tuples]
should be fine应该没事
tuples = [('hello', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'bob')] result=[] [result.append(k+" "+v) for k,v in tuples]元组 = [('hello', 'my'), ('my', 'name'), ('name', 'is'), ('is', 'bob')] result=[] [result. append(k+" "+v) for k,v in tuple]
print(result)
output: output:
['hello my', 'my name', 'name is', 'is bob']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.