简体   繁体   English

循环进入python中的向量列表

[英]Looping into a list of vectors in python

Given a list of sentences I am trying to do in a short way, get from a list the strings from a structure(list of vectors) in which the first position is the value of the string: 给定我要用简短的方式列出的句子列表,从列表中获取结构(向量列表)中的字符串,其中第一个位置是字符串的值:

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

The result would be a list with the values of the position 0 of the vector. 结果将是带有向量位置0值的列表。

My first try: 我的第一次尝试:

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

The Target is to improve it in a way to do it in a simple line. 目标是通过一种简单的方式来改进它。

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

If the question is "how do I make it a one-liner" then the answer is: 如果问题是“我如何使其成为单线”,那么答案是:

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

but that doesn't make the code much more readable. 但这并不能使代码更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM