【问题标题】:How does a Python genius iterate over a single value in a Python tuple?Python 天才如何迭代 Python 元组中的单个值?
【发布时间】:2012-05-19 18:40:07
【问题描述】:

我有一个名为“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

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

有没有 Python 天才知道如何做到这一点?我正在为此寻找多种选择。谢谢。

【问题讨论】:

  • 看起来这个线程中的每个参赛者都回答了他们自己版本的问题。您能否更具体地说明您想要实现的目标,例如有问题的代码应该返回或打印什么?

标签: python tuples loops


【解决方案1】:
[tag for x,y,tag in score if x==0 and y==1]

【讨论】:

  • 我喜欢你的解决方案 - 它比 kgiannakakis 的更干净。
  • @Tadeck 我同意,除了 kgiannakakis 对.keys() 的明确使用对于不使用此 python 习惯用法的人来说更清楚,其中遍历字典会遍历键。说到这里,我也不使用.keys() :D
  • 我会做的差不多,但我会把它变成一个生成器表达式。 () 而不是 []
  • 这看起来是最直接的解决方案。但是,我对 Python 非常陌生。您能否通过提供一些代码来帮助我,以便我有更多关于如何使用它的上下文?
  • @Ussabin 给定 score dict,这就是你需要做的所有事情。这是一个列表推导,它将返回标签列表(例如,['N', 'V', 'NP'])。如果您可以更具体地说明您希望人们能够提供什么帮助。
【解决方案2】:

列表理解怎么样:

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

【讨论】:

  • 我的 +1。我更喜欢你的if 部分。 .keys() 可以省略,因为它会强制创建一个列表(不是大问题),并且字典会隐式地遍历键。
【解决方案3】:

您必须遍历所有元素并检查它们的键是否与您要查找的内容匹配。但是,您可以使用过滤迭代器很好地做到这一点:

elems = (item for item in score.iteritems() if item[0][:2] == (0, 1))

你也可以使用只给你标签值和元素而不是整个项目元组的迭代器:

elems = ((item[0][2], item[1]) for item in score.iteritems() if item[0][:2] == (0, 1))

如果你真的只需要标签值而不是相应的元素,你可以更简单:

tags = [k[2] for k in score if k[:2] == (0, 1)]

演示:

>>> score
{(0, 1, 'NP'): 1.2,
 (0, 1, 'V'): 1.5,
 (1, 2, 'N'): 0.2,
 (1, 2, 'PP'): 0.1,
 (1, 2, 'V'): 0.1}

>>> list(item for item in score.iteritems() if item[0][:2] == (0, 1))
[((0, 1, 'NP'), 1.2), ((0, 1, 'V'), 1.5)]

>>> list(((item[0][2], item[1]) for item in score.iteritems() if item[0][:2] == (0, 1)))
[('NP', 1.2), ('V', 1.5)]

>>> [k[2] for k in score if k[:2] == (0, 1)]
['NP', 'V']

【讨论】:

  • 从您的回复中肯定学到了一些东西,ThiefMaster。这将派上用场。不过,看起来上面的响应更有效率。感谢您抽出宝贵时间将您的天才借给我。
  • 你应该用赞成票来纪念时间。关于效率:没有真正的区别。
  • 是的...感谢您的建议。但是,我的声誉仍然低于 15(刚开始使用 SOF),我还不能投票。
【解决方案4】:

我的智商超过 200,所以我希望这很重要:

score = {}
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

from itertools import groupby

def group_key(dict_key):
    return dict_key[:2]

sorted_keys = sorted(score)
for group_key, group_of_dict_keys in groupby(sorted_keys, key=group_key):
    print group_key
    print [(dict_key, score[dict_key]) for dict_key in group_of_dict_keys]

"""
(0, 1)
[((0, 1, 'N'), 1.0), ((0, 1, 'NP'), 1.2), ((0, 1, 'V'), 1.5)]
(1, 2)
[((1, 2, 'N'), 0.2), ((1, 2, 'PP'), 0.1), ((1, 2, 'V'), 0.1)]
"""

当然,如果您只想要标签本身,那么请更改循环:

for group_key, group_of_dict_keys in groupby(sorted_keys, key=group_key):
    print group_key
    tags = [tag for x, y, tag in group_of_dict_keys]
    print tags
"""
(0, 1)
['N', 'NP', 'V']
(1, 2)
['N', 'PP', 'V']
"""

【讨论】:

  • 你的智商不重要,你回答的质量很重要。 sorted_keys = sorted(score.keys()) 可以(应该)替换为 sorted_keys = sorted(score)。就您答案的其他部分而言,它们还可以。
  • 谢谢,我现在编辑我的答案。我使用 defaultdict fyi 添加了另一个答案。 +1 表示感谢。
【解决方案5】:

如果你只想要标签,那么 defaultdict 将是最简单的选择。

score = {}
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

from collections import defaultdict

dict_ = defaultdict(list)

for x,y,tag in score:
    dict_[x,y].append(tag)

#now get a result for any x,y we like:
print dict_[0,1]
"""['NP', 'N', 'V']"""

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2011-10-06
    • 2018-07-13
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多