2016-12-07 228 views
0

我有一個輸出是一個元組列表。它看起來像這樣:將元組的第二個元素(在元組列表中)作爲字符串獲取

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

我需要使用元組的第二部分只應用'split'並分別獲取每個單詞的句子。

在這一點上,我無法隔離元組的第二部分(文本)。

這是我的代碼:

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],但它給了我文本的第二個索引,而不是元組的第二個元素。

回答

3

你可以做這樣的事情跟列表解析:

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()) 
+0

我需要在輸出上傳遞'intersection()'(我有兩個輸出,我需要找到常用詞)。我怎樣才能把它從列表中刪除? '十字路口'不會列表。 – norpa

+0

我沒有得到你。你想計算兩個列表還是多個列表之間的交集?因爲annot1似乎包含多個元組,根據您在問題中如何定義它。 – MYGz

+0

我想計算兩個元組列表的第二個元素(一個字符串)之間的交集。我有'annot1'和'annot2'(有如上所示的元組),我需要比較annot1中tuple1的第二個元素和annot2中tuple1的第二個元素;那麼annot1中的tuple2的第二個元素與annot2中的tuple2的第二個元素... etc – norpa

1

annot1是元組列表。爲了從每個元素的字符串,可以做這樣的事情

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

我得到'TypeError:'長'對象沒有屬性'__getitem __''。你知道會發生什麼嗎?... – norpa

+0

這聽起來像你試圖訪問一個長的列表。你確定'annot1'是元組列表嗎?聽起來像其他人,但你已經覆蓋。 – Iluvatar

相關問題