本文介绍了Python天才如何遍历Python元组中的单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dictionary named 'score' with keys that are tuples.Each tuple is of the form (x, y, tag).

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

Some possible score initializations are:

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

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

I'd like to be able to keep x and y constant (e.g. 0, 1) and then iterate over the given values for tag (e.g. N, V, NP).

那里有Python天才吗?我正在为此寻找多个选择.谢谢.

Any Python geniuses out there know of ways to do this? I'm looking for multiple options on this one. Thanks.

推荐答案

列表理解如何处理?

[ x[2] for x in score.keys() if x[0:2] == (0,1)]

这篇关于Python天才如何遍历Python元组中的单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:30