在字典中列出 <Key-Value> 对中的值

List within a Dictionary as Value in <Key-Value> Pair

我是 Python 的新手,正在阅读 Python Tutorial 中的词典,遇到以下情况:

  • Let us say , that there exists a dictionary : {'x':[0,0,70,100,...] , 'y':[0,20,...] , ...}
  • I want to get hold of the value (which happens to be a list here) for each key
  • Then , I wish to play around with the elements of the list like make some comparisons among the values of the list etc.
  • I wish to do this task dynamically i.e. using a loop
  • At present I can do it statically i.e. by hard-coding it but that does not take me anywhere

预期输入

{'pikachu':[200,50,40,60,70] , 'raichu':[40 ,30,20,10,140] , ....}

预期输出

{pikachu:[1,0,0,0] , raichu:[0,0,0,1] , .....}

我的愿望:

我想成对比较 value (这里是 list 的元素:(200,50);(50,40);(40,60);(60,70) 每个键

比较的形式为:

   if(abs(x-x+1) > 20):
       then this event is marked as 1 
   else: 
       it is marked as 0

到目前为止我的代码:

import random   

def do_stuff():

  NUMBER_OF_ELEMENTS = 5      

  list_of_pokemon = ['pikachu', 'charizard', 'sabertooth' , 'raichu'] 

  dict_of_pokemon = {}              

  for i in list_of_pokemon:         
    dict_of_pokemon[i] = [random.randrange(0, 200,10) for j in range(NUMBER_OF_ELEMENTS)]


  #This just prints out a dict of the form : {'pikachu':[200,50,40,60,70] , .....}
  print dict_of_pokemon 

  dict_of_changes = {}

  temp = []

  for x in dict_of_pokemon:

    for y in dict_of_pokemon[x]:
        # I wish to compare the elements of a value list 
        # For example : pairwise comparing (200,50);(50,40);(40,60);(60,70)

我的问题:

有人可以帮我吗?

*P.S. This is not a homework question*

尝试这样的事情:

lst = [200,50,40,60,70]
def pairwise_map(l):
    pairs = zip(l[:-1], l[1:])
    cond = lambda x: abs(x[0] - x[1]) > 20
    return map(lambda x: 1 if cond(x) else 0, pairs)
print pairwise_map(lst)

pairwise_map应用于字典:

d = {
    'pikachu':[200,50,40,60,70] , 
    'raichu':[40 ,30,20,10,140]
} 

result = {k: pairwise_map(v) for k, v in d.iteritems()}
print result

输出:

{'pikachu': [1, 0, 0, 0], 'raichu': [0, 0, 0, 1]}

在评论之后,您可能想阅读有关非常常见的内容 zip, lambdas and dictionary comprehension

def identify_events(seq):
    result = []
    for i in range(len(seq)-1):
        current = seq[i]
        next = seq[i+1]
        if abs(current - next) > 20:
            result.append(1)
        else:
            result.append(0)
    return result


d = {
    'pikachu':[200,50,40,60,70] , 
    'raichu':[40 ,30,20,10,140]
} 

output = {key: identify_events(value) for key, value in d.iteritems()}

print output

结果:

{'pikachu': [1, 0, 0, 0], 'raichu': [0, 0, 0, 1]}

我了解到您想将字典中的每个条目与所有其他条目进行比较。首先,创建一个包含所有要比较的对的列表,然后使用 zip 得到后续元素对:

import itertools

keys = dict_of_pokemon.keys()
for key1,key2 in itertools.product(keys, keys):
    if key1 == key2:
        continue           # I assume you don't want to compare the same lists

    elements_to_compare = zip(dict_of_pokemons[key1], dict_of_pokemons[key2])

    print elements_to_compare # e.g. [(200,50), (50,40), (40,60), (60,70)]

精简版:

d = {'pikachu': [200, 50, 40, 60, 70], 'raichu': [40, 30, 20, 10, 140]}

print {k: map(lambda x,y: 1 if abs(x-y)>20 else 0, v[:-1],v[1:]) 
              for k,v in d.iteritems()}

{'pikachu': [1, 0, 0, 0], 'raichu': [0, 0, 0, 1]}