2017-09-21 76 views
-3

其实我已经字典从蟒蛇来了下面的列表是一个列表:切片扔使用Python中

{"Age": {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55}, "Weight": {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54}, "Animal": {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey", "4": "mice"}} 

我如何能切这个让3名名单? 我想是这样的:

Age = {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55} 
Weight = {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54} 
Animal = {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey", "4": "mice"} 

我知道我应该使用像这样:

for k, v in Age.items(): 
print v 

但我怎么能做到这一点的3名名单以及如何分配的方式我想: 重量= {...},动物= {...}

+1

哪个列表?该代码使用字典。 – MSeifert

+0

这是一个字典而不是列表 – Abra001

+0

'年龄= {...'不是一个列表。你有一个嵌套的字典。请具体说明你想要的输出。 – offeltoffel

回答

1

只是分配给每个需要的密钥到您的变量:

combined = {"Age": {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55}, "Weight": {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54}, "Animal": {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey", "4": "mice"}} 

age = combined['Age'] 
weight = combined['Weight'] 
animal = combined['Animal'] 

print age 
print weight 
print animal 

给你:

{'1': 72, '0': 86, '3': 56, '2': 56, '4': 55} 
{'1': 71, '0': 70, '3': 39, '2': 61, '4': 54} 
{'1': 'Cat', '0': 'Dog', '3': 'Horse', '2': 'Monkey', '4': 'mice'} 
0

您正在使用错误的命名为数据结构。 你所指的是一个字典而不是列表。但对于所提到的问题。下面是一个解决方案。

input_dict = {"Age": {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55}, "Weight": {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54}, "Animal": {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey", "4": "mice"}} 
for k,v in input_dict.items(): 
    vars()[k] = v 

您现在可以打印变量。

print Age 
{'1': 72, '0': 86, '3': 56, '2': 56, '4': 55} 
print Weight 
{'1': 71, '0': 70, '3': 39, '2': 61, '4': 54} 
print Animal 
{'1': 'Cat', '0': 'Dog', '3': 'Horse', '2': 'Monkey', '4': 'mice'} 

修改了答案,因为每个建议从@Eric Duminil

0

你的数据格式是非常奇怪,使迭代和查找比必要的要困难得多。

以下是将其转换为更简单的数据格式的方法。这仍然是一个词典的词典。它应该是比较容易做的迭代和查找,但:

weird_data = {"Age": {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55}, "Weight": {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54}, "Animal": {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey", "4": "mice"}} 

data = {name:{ 
     'id' : int(i), 
     'age': weird_data["Age"][i], 
     'weight': weird_data["Weight"][i] 
    } for i,name in weird_data["Animal"].items()} 

print(data) 
#{'Cat': {'age': 72, 'id': 1, 'weight': 71}, 
# 'Dog': {'age': 86, 'id': 0, 'weight': 70}, 
# 'Horse': {'age': 56, 'id': 3, 'weight': 39}, 
# 'Monkey': {'age': 56, 'id': 2, 'weight': 61}, 
# 'mice': {'age': 55, 'id': 4, 'weight': 54}} 

举个例子:

>>> list(data) 
['Cat', 'Dog', 'Horse', 'Monkey', 'mice'] 
>>> [data[animal]['age'] for animal in data] 
[72, 86, 56, 56, 55] 
>>> data['Dog']['weight'] 
70 
0

比方说,在你的问题的字典由

dictionary = { 
     "Age": {"1": 72, "0": 86, "3": 56, "2": 56, "4": 55}, 
     "Weight": {"1": 71, "0": 70, "3": 39, "2": 61, "4": 54}, 
     "Animal": {"1": "Cat", "0": "Dog", "3": "Horse", "2": "Monkey","4":"mice"} 
} 

你可以表示创建如下功能: -

def split_dictionary(Age, Weight, Animal): 
    return Age, Weight, Animal 

然后简单地调用这样的功能: -

Age, Weight, Animal = split_dictionary(**dictionary)