2017-04-27 33 views
0

給定一個我想要做的短語列表,從列表中獲得一個結構(向量列表)中的字符串,其中第一個位置是字符串:循環插入到python中的向量列表中

sentence_list = ['I want this',' It hurts','Life is too short as it is' ] 

structure = [(1,'I want this. Not that. Right now.'), (2,'When I think about what you are doing, I wonder if you realize the effect you are having on me. It hurts. A lot.'),(3,'Life is too short as it is. In short, she had a cushion job.')] 

結果將是一個列表,其中包含向量位置0的值。

我第一次嘗試:

result = [] 
    for s in sentence_list: 
     for i in structure: 
      if s in i[1].split('.'): 
        result.append(i[0]) 

的目標是提高它的方式做一個簡單的線條。

result 
[1, 2, 3] # the result is giving the first value of the vector if the string is there.. 
+1

那麼你的問題到底是什麼? –

+0

我在隔離過程中使用此功能,它工作正常,但我想以更短的方式改善它。 – PeCaDe

+0

首先,你不需要'split' –

回答

0

如果問題是「我怎麼讓一個班輪」,那麼答案是:

result = [index for index, target in structure for sentence in sentence_list if sentence in target.split('.')] 

但是,這並不使代碼更易讀。

+0

這是非常有用的,不太可讀,但非常有趣... thx很多! – PeCaDe