繁体   English   中英

如何将两个嵌套的列表列表转换为 Python 中的嵌套元组列表?

[英]How to convert a two nested list of lists into a nested list of tuples in Python?

我正在尝试将两个嵌套的列表列表转换为 Python 中的嵌套元组列表。 但我无法得到想要的结果。 输入看起来像:

first_list = [['best', 'show', 'ever', '!'],
              ['its', 'a', 'great', 'action','movie']]

second_list = [['O', 'B_A', 'O', 'O'],
               ['O', 'O', 'O', 'B_A','I_A']]

所需的output应如下所示:

result = [[('best','O'),('show','B_A'),('ever','O'),('!','O')],
          [('its','O'),('a','O'),('great','O'),('action','B_A'),('movie','I_A')]]

先感谢您!

# zip both lists. You end up with pairs of lists
pl = zip(first_list, second_list)

# zip each pair of list and make list of tuples out of each pair of lists.
[[(e[0], e[1]) for e in zip(l[0], l[1])] for l in pl]

注意:未经测试,在手机上完成。 但你明白了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM