简体   繁体   English

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

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

I have an output that is a list of tuples. 我有一个输出是元组列表。 It looks like this: 看起来像这样:

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]')…

I need to use the second part of the tuple only to apply 'split' and get each word on the sentence separately. 我只需要使用元组的第二部分来应用“拆分”并分别获取句子中的每个单词。

At this point, I'm not able to isolate the second part of the tuple (the text). 在这一点上,我无法隔离元组的第二部分(文本)。

This is my code: 这是我的代码:

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

But I get: 但是我得到:

TypeError: sequence item 0: expected string, tuple found

I tried to use annot1[1] but it gives me the second index of the text instead of the second element of the tuple. 我尝试使用annot1 [1],但是它给了我文本的第二个索引,而不是元组的第二个元素。

You can do something like this with list comprehensions: 您可以使用列表推导功能执行以下操作:

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]

Output: 输出:

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

You can calculate the intersection of strings in corresponding positions in annot1 and annot2 like this: 您可以像这样在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 is a list of tuples. annot1是一个元组列表。 To get the string from each of the elements, you can do something like this 要从每个元素中获取字符串,您可以执行以下操作

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.

相关问题 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element? 获取元组列表中元素元组的最大值 - Getting the maximum value of element tuple in list of tuples 对元组列表进行排序,其中元组的第二个元素为列表 - Sorting a list of tuples with the second element of tuple being a list Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 通过添加元组的第二个和第三个元素对元组列表进行排序 - Sorting a list of tuples by the addition of second and third element of the tuple 按元组的第二个和第三个值对三元素元组列表进行排序和计数 - Sort & count list of 3-element tuples by second and third value of a tuple 检查元组列表是否具有元组的元组作为定义的字符串 - Check that list of tuples has tuple with 1st element as defined string 检查元组的列表,其中元组的第一个元素由定义的字符串指定 - Check list of tuples where first element of tuple is specified by defined string 根据元组列表中的第二个值找到元组后返回元组的第一个元素 - Returning first element of a tuple after finding the tuple according to the second value in a list of tuples 在元组的元组列表中通过内部元组查找元素 - Find an element by inner tuple in a list of a tuple of tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM