我有两个长度相同的列表。
第一个包含字符串。第二个 - 字符串可以是'True'
or 'False'
.
如果第二个列表的第 n 个元素是'True'
,我想将第一个列表的第 n 个元素附加到另一个列表。
所以如果我有:
List1:
('sth1','sth2','sth3','sth4')
List2:
('True','False','True','False')
结果应该是List3:
('sth1','sth3')
.
我怎样才能以这种方式交叉两个列表?
Use zip:
result = [x for x, y in zip(xs, ys) if y == 'True']
Example:
xs = ('sth1','sth2','sth3','sth4')
ys = ('True','False','True','False')
result = [x for x, y in zip(xs, ys) if y == 'True']
result
['sth1', 'sth3']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)