[英]how to split all first elements of tuples in list? (Python)
I made a list called conditionList looking like this: 我创建了一个名为conditionList的列表,如下所示:
('I'm a man.', 2, 5, 10), ('I'm 20 years old', 6, 8, 10), ('This is just another sentence', 5, 6 10)
This conditionList
is the result of a zip function of 4 lists: 这个
conditionList
是4个列表的zip函数的结果:
conditionlist = zip(sentence, variable1, variable2, variable3)
So every element of the list consists of a sentence followed by three numbers. 因此,列表中的每个元素都包含一个句子,后跟三个数字。 However, I need a function which would result in the following list:
但是,我需要一个函数,它会产生以下列表:
('I', 2, 5, 10), ('am', 2, 5, 10), ('a', 2, 5, 10), ('man.', 2, 5, 10), ('I', 6, 8, 10) etc.
In this way, every word is associated with the variables of the sentence they belong to. 这样,每个单词都与它们所属的句子的变量相关联。
How can I go from my conditionList to the desired list? 如何从条件列表转到所需列表?
Thank you. 谢谢。
Presumably what you had is a typo, you just need a comprehension and str.split
. 据推测,你所拥有的是一个错字,你只需要一个理解和
str.split
。
>>> l
(("I'm a man.", 2, 5, 10),
("I'm 20 years old", 6, 8, 10),
('This is just another sentence', 5, 6, 10))
>>> [(i, j, k, m) for p, j, k, m in l for i in p.split()]
[("I'm", 2, 5, 10),
('a', 2, 5, 10),
('man.', 2, 5, 10),
("I'm", 6, 8, 10),
('20', 6, 8, 10),
('years', 6, 8, 10),
('old', 6, 8, 10),
('This', 5, 6, 10),
('is', 5, 6, 10),
('just', 5, 6, 10),
('another', 5, 6, 10),
('sentence', 5, 6, 10)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.