2017-04-27 6 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