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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.