Skip to main content
 首页 » 编程设计

python之从列表中的嵌套元组中获取特定值

2025年05月04日19powertoolsteam

我在列表中有一个嵌套的元组,如下所示:

x = [(('subject', 'to'), 18), (('subject', 'and'), 5), (('subject', 'of'), 4), (('subject', 'the'), 3), (('subject', 'that'), 2), (('subject', 'owing'), 2), (('subject', 'making'), 2)] 
 

我需要一个内部元组的第二个值(索引 1)的列表。我如何列出它?

我需要的输出:

['to','and','of','the','that','owing','making'] 

按照特定的顺序

这是我尝试过的

x[0][0][1] 

我的输出

to 

请您参考如下方法:

使用列表理解和变量拆包:

newList = [j for (i, j), index in x]