[英]Looping into a list of vectors in 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..
如果問題是“我如何使其成為單線”,那么答案是:
result = [index for index, target in structure for sentence in sentence_list if sentence in target.split('.')]
但這並不能使代碼更具可讀性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.