(这是每个人的一种假设情况。我更倾向于寻找方向而不是直接的过程,但是如果你能提供它们,真棒!)
所以,假设我们有一份运动员名单,我现在将使用花样滑冰运动员,因为我现在正处于冬季奥运会的膝盖深处。 (因为那是我的第一直觉,所以我把它扔进字典里,不一定是这样。)
after_short_program = {
'1': 'Evgeni Plushenko',
'2': 'Evan Lysacek',
'3': 'Daisuke Takahashi',
'4': 'Nobunari Oda',
'5': 'Stephane Lambiel'
}
所以在自由滑之后(我没有想到这一点),让我们说这些是积分榜。
after_free_skate = {
'1': 'Evan Lysacek',
'2': 'Daisuke Takahashi',
'3': 'Evgeni Plushenko',
'4': 'Stephane Lambiel',
'5': 'Nobunari Oda',
}
所以,问题:
如何比较两组数据呢? Evan Lysacek上升一个空间赢得金牌,Daisuke上升一个位置赢得银牌,Evgeni下移两个空位赢得铜牌。如果我要提供这些信息,我想说,“Evan(+1或向上移动一个),Evgeni(-2或向下移动两个)等等。”
Python中有没有办法从比较中提取这类数据?
答案 0 :(得分:7)
我会使用运动员姓名作为你的决定中的关键。然后你可以更容易地找到他们的位置。类似的东西:
diff = {}
for (a, pos2) in after_free_skate.items():
pos1 = after_short_program[a]
diff[a] = pos2 - pos1
我希望它有所帮助
答案 1 :(得分:3)
此解决方案以与最终位置相同的顺序打印结果
如果地方未更改(+0)则打印
如果您希望过滤掉那些,只需在打印前放置if diff:
>>> after_short_program = [
... 'Evgeni Plushenko',
... 'Evan Lysacek',
... 'Daisuke Takahashi',
... 'Nobunari Oda',
... 'Stephane Lambiel',
... ]
>>>
>>> after_free_skate = [
... 'Evan Lysacek',
... 'Daisuke Takahashi',
... 'Evgeni Plushenko',
... 'Stephane Lambiel',
... 'Nobunari Oda',
... ]
>>>
>>> for i,item in enumerate(after_free_skate):
... diff = after_short_program.index(item)-i
... print "%s (%+d)"%(item,diff)
...
...
Evan Lysacek (+1)
Daisuke Takahashi (+1)
Evgeni Plushenko (-2)
Stephane Lambiel (+1)
Nobunari Oda (-1)
正如普瓦森指出的那样,如果你的秒表不够好,你可能会得到平局。因此,此修改使用dicts而不是列表。排名的顺序仍然保留
>>> from operator import itemgetter
>>>
>>> after_short_program = {
... 'Evgeni Plushenko':1,
... 'Evan Lysacek':2,
... 'Daisuke Takahashi':3,
... 'Stephane Lambiel':4,
... 'Nobunari Oda':5,
... }
>>>
>>> after_free_skate = {
... 'Evan Lysacek':1,
... 'Daisuke Takahashi':2,
... 'Evgeni Plushenko':3,
... 'Stephane Lambiel':4, # These are tied
... 'Nobunari Oda':4, # at 4th place
... }
>>>
>>> for k,v in sorted(after_free_skate.items(),key=itemgetter(1)):
... diff = after_short_program[k]-v
... print "%s (%+d)"%(k,diff)
...
...
Evan Lysacek (+1)
Daisuke Takahashi (+1)
Evgeni Plushenko (-2)
Nobunari Oda (+1)
Stephane Lambiel (+0)
>>>
如果第二个词典中的键可能不在第一个词中,你可以做这样的事情
for k,v in sorted(after_free_skate.items(),key=itemgetter(1)):
try:
diff = after_short_program[k]-v
print "%s (%+d)"%(k,diff)
except KeyError:
print "%s (new)"%k
答案 2 :(得分:1)
一种方法是翻转键和值,然后取出差异,即:
for k, v in after_free_skate.items():
print 'k', v - after_short_program[k]
答案 3 :(得分:1)
我个人使用列表,因为它们自然适合存储“位置”信息......以下是采用列表的功能相当的方法:
###_* input data
after_short_program = [
'Evgeni Plushenko',
'Evan Lysacek',
'Daisuke Takahashi',
'Nobunari Oda',
'Stephane Lambiel'
]
after_free_skate = [
'Evan Lysacek',
'Daisuke Takahashi',
'Evgeni Plushenko',
'Stephane Lambiel',
'Nobunari Oda'
]
## combine
all_athletes = set(after_short_program + after_free_skate)
###_* import libraries, define functions
from operator import add, sub
from functools import partial
def tryit(f,*args):
try: return f(*args)
except: return None
def compose(f,g): ## available in functional library
return lambda x: f(g(x))
###_* apply functions
## original and new positions for each athlete
## I usually wrap list.index() in a try-except clause
pos = [(x,{'orig':tryit(compose(partial(add,1),after_short_program.index),x),
'new':tryit(compose(partial(add,1),after_free_skate.index),x)})
for i,x in enumerate(all_athletes)]
## calculate the changes (now edited to sort by final position)
changes = [(x[0],tryit(sub,x[1]['orig'],x[1]['new']))
for x in sorted(pos,key=lambda x: x[1]['new'])]
输出如下:
>>> changes
[('Evan Lysacek', 1), ('Daisuke Takahashi', 1), ('Evgeni Plushenko', -2), ('Stephane Lambiel', 1), ('Nobunari Oda', -1)]
答案 4 :(得分:0)
我会将名称作为键,将位置作为值,将位置设为整数:
after_short_program = {
'1': 'Evgeni Plushenko',
'2': 'Evan Lysacek',
'3': 'Daisuke Takahashi',
'4': 'Nobunari Oda',
'5': 'Stephane Lambiel'
}
after_free_skate = {
'1': 'Evan Lysacek',
'2': 'Daisuke Takahashi',
'3': 'Evgeni Plushenko',
'4': 'Stephane Lambiel',
'5': 'Nobunari Oda',
}
after_short_program_swap = {}
for k,v in after_short_program.iteritems():
after_short_program_swap[v]=int(k)
after_free_skate_swap = {}
for k,v in after_free_skate.iteritems():
after_free_skate_swap[v]=int(k)
然后代码更简单:
moved = {}
for key in after_short_program_swap:
moved[key] = after_short_program_swap[key] - after_free_skate_swap[key]
print moved
打印:
<'Evan Lysacek':1,'Nobunari Oda':-1,'Evgeni Plushenko': - 2,'Stephane Lambiel':1,'Daisuke Takahashi':1}在@gnibbler之后打印出奖牌顺序:
from operator import itemgetter
print '\n'.join('%s (%+d)' % (k,moved[k]) for k,v in sorted(after_free_skate_swap.items(),key=itemgetter(1)))
Evan Lysacek(+1)
Daisuke Takahashi(+1)
Evgeni Plushenko(-2)
Stephane Lambiel(+1)
Nobunari Oda(-1)