簡體   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