繁体   English   中英

将元组的第二个元素(在元组列表中)获取为字符串

[英]Getting second element of a tuple (in a list of tuples) as a string

我有一个输出是元组列表。 看起来像这样:

annot1=[(402L, u"[It's very seldom that you're blessed to find your equal]"), 
        (415L, u'[He very seldom has them in this show or his movies]')…

我只需要使用元组的第二部分来应用“拆分”并分别获取句子中的每个单词。

在这一点上,我无法隔离元组的第二部分(文本)。

这是我的代码:

def scope_match(annot1):
    scope = annot1[1:]
    scope_string = ‘’.join(scope)
    scope_set = set(scope_string.split(' '))

但是我得到:

TypeError: sequence item 0: expected string, tuple found

我尝试使用annot1 [1],但是它给了我文本的第二个索引,而不是元组的第二个元素。

您可以使用列表推导功能执行以下操作:

annot1=[(402L, u"[It's very seldom that you're blessed to find your equal]"), 
        (415L, u'[He very seldom has them in this show or his movies]')]
print [a[1].strip('[]').encode('utf-8').split() for a in annot1]

输出:

[["It's", 'very', 'seldom', 'that', "you're", 'blessed', 'to', 'find', 'your', 'equal'], ['He', 'very', 'seldom', 'has', 'them', 'in', 'this', 'show', 'or', 'his', 'movies']]

您可以像这样在annot1和annot2中的相应位置计算字符串的交集:

for x,y in zip(annot1,annot2):
    print set(x[1].strip('[]').encode('utf-8').split()).intersection(y[1].strip('[]').encode('utf-8').split())

annot1是一个元组列表。 要从每个元素中获取字符串,您可以执行以下操作

def scope_match(annot1):
    for pair in annot1:
        string = pair[1]
        print string  # or whatever you want to do

暂无
暂无

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

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