在<键值>对</key-value>中将字典中的列表作为值列出

时间:2015-01-05 14:01:34

标签: python list python-2.7 loops dictionary

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

  
      
  • 我们说,存在字典: {'x':[0,0,70,100,...] , 'y':[0,20,...] , ...}
  •   
  • 我想抓住每个value
  • key (这里恰好是一个列表)   
  • 然后,我希望能够使用列表中的元素,例如对列表的值进行一些比较等。
  •   
  • 我希望执行此任务dynamically,即使用loop
  •   
  • 目前我可以statically hard-coding{'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*

我的问题:

有人能帮助我吗?

{{1}}

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

紧凑版:

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

答案 2 :(得分:1)

尝试这样的事情:

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

发表评论后,您可能希望了解非常常见的ziplambdasdictionary comprehension

答案 3 :(得分: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)]