简体   繁体   中英

How does a Python genius iterate over a single value in a Python tuple?

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

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

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

[tag for x,y,tag in score if x==0 and y==1]

列表理解呢?

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

You'll have to iterate over all elements and check if they key matches what you are looking for. However, you can do this nicely by using a filtered iterator:

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

You could also use an iterator that only gives you the tag value and the element instead of the whole item tuples:

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

If you really just need the tag values and not the corresponding elements, you can do it even easier:

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

Demo:

>>> 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']

My IQ is over 200 so I hope this counts:

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)]
"""

of course if you just want the tags by themselves then change the loop:

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']
"""

If you just want the tags then defaultdict will be the most simple option.

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']"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM