[英]How do i find all the elements in a list of tuples that are same in python?
[英]how to split all first elements of tuples in list? (Python)
我创建了一个名为conditionList的列表,如下所示:
('I'm a man.', 2, 5, 10), ('I'm 20 years old', 6, 8, 10), ('This is just another sentence', 5, 6 10)
这个conditionList
是4个列表的zip函数的结果:
conditionlist = zip(sentence, variable1, variable2, variable3)
因此,列表中的每个元素都包含一个句子,后跟三个数字。 但是,我需要一个函数,它会产生以下列表:
('I', 2, 5, 10), ('am', 2, 5, 10), ('a', 2, 5, 10), ('man.', 2, 5, 10), ('I', 6, 8, 10) etc.
这样,每个单词都与它们所属的句子的变量相关联。
如何从条件列表转到所需列表?
谢谢。
据推测,你所拥有的是一个错字,你只需要一个理解和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.