Python天才如何迭代Python元组中的单个值?

Uss*_*bin 0 python loops tuples

我有一个名为'score'的字典,其中的键是元组.每个元组的形式为(x,y,tag).

一些可能的分数初始化是:

score[(0, 1, 'N')] = 1.0
score[(0, 1, 'V')] = 1.5
score[(0, 1, 'NP')] = 1.2
score[(1, 2, 'N')] = 0.2
score[(1, 2, 'PP')] = 0.1
score[(1, 2, 'V')] = 0.1
Run Code Online (Sandbox Code Playgroud)

我希望能够保持x和y不变(例如0,1),然后迭代标签的给定值(例如N,V,NP).

任何Python天才都知道如何做到这一点?我正在寻找这个的多种选择.谢谢.

ami*_*mit 8

[tag for x,y,tag in score if x==0 and y==1]
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢你的解决方案 - 它比kgiannakakis更清洁. (2认同)

kgi*_*kis 7

列表理解怎么样:

[ x[2] for x in score.keys() if x[0:2] == (0,1)]
Run Code Online (Sandbox Code Playgroud)