如何将两个嵌套的列表列表转换为 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']]
所需的输出应该如下所示:
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]
注意:未测试,在手机上完成。但是你明白了。
我正在尝试将 两个 嵌套列表列表转换为 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']]
所需的输出应该如下所示:
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]
注意:未测试,在手机上完成。但是你明白了。