循环到python中的向量列表

时间:2017-04-27 10:38:29

标签: python loops

鉴于我试图以一种简短的方式做的句子列表,从列表中获取结构(向量列表)中的字符串,其中第一个位置是字符串的值:

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 :(得分:0)

如果问题是"我如何使它成为单线"然后答案是:

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

但这并不能使代码更具可读性。