Python 从defaultdict打印值
我正在编写一些代码,如下所示:Python 从defaultdict打印值,python,python-3.x,Python,Python 3.x,我正在编写一些代码,如下所示: def check_states(inventory): inventory_list = defaultdict(list) for i in inventory: inventory_list[i['store_site']].append(i) 这将给我一个defaultdict,其中有一个存储站点的键和一个所有库存的列表,这是一个信息,在另一个字典中包含数据,如接收日期、传输日期、序列号、状态等。因此它是一个映射到其中多个
def check_states(inventory):
inventory_list = defaultdict(list)
for i in inventory:
inventory_list[i['store_site']].append(i)
这将给我一个defaultdict,其中有一个存储站点的键和一个所有库存的列表,这是一个信息,在另一个字典中包含数据,如接收日期、传输日期、序列号、状态等。因此它是一个映射到其中多个值的字典。所以我们有一个字典列表。我想打印一些,但不是所有的值。因此,在上述函数的末尾,我添加了:
for store in inventory_list:
print(store['serial_id'])
我得到
TypeError: string indices must be integers.
TypeError: tuple indices must be integers, not str.
我还尝试过使用items()
:
我得到
TypeError: string indices must be integers.
TypeError: tuple indices must be integers, not str.
有人能帮我指出我的错误吗?我寻找了类似的问题,但大多数字典问题都是关于打印1:1的值,而不是1:multi的值
{'store5278': [{'abstract_state': 'GONE',
'aud_last_updated_time': '2017-03-29T08:03:20Z',
'bin_id': 9178,
'disposed_at': '2017-03-29T08:03:20Z',
'entity_type': 'parts',
'external_serial_id': '',
'id': 8336471,
'is_fake_serial_id': False,
'is_model_active': True,
'is_part_active': True,
'is_vending_bin': False,
'joint_asset_id': '_part_8336471',
'last_updated_time': '2017-03-29T08:03:20Z',
'model': 'Z-CAT6-16M-OR-UTP-AA-L-BBB',
'model_apn': 'OrangePatch16M',
'model_description': 'CAT6 Copper Patch Cord 16m PVC ORANGE ',
'model_id': 7285,
'model_mpn': '',
'po_number': '312949',
'received_at': '2017-03-23T14:38:25Z',
'room': 'PARTS',
'serial_id': '002042869',
'state': 'CONSUMED',
'state_id': '10',
'store': 'store5278',
'tracking_id': '7097553',
'transferred_at': '2017-03-29T08:03:20Z',
'type_id': 27,
'type_name': 'Cable',
'unit_cost': 0.0,
'vendor': 'Excel',
'vendor_id': 135}]}
我认为问题在于没有完全理解在for
循环中迭代的数据的性质。并且可能不了解库存
参数中的内容
for store in inventory_list:
...
inventory\u list
是一个dict
。当在如上所示的dict
上进行迭代时,会得到dict中所有键的序列。在dict中,这些键“显然”是字符串。我说,通过逆向工程,你所显示的错误:
TypeError: string indices must be integers.
这就是运行时出现的错误:
stored = "some_name"
store['serial_id']
所以。。。您的第一个问题是理解inventory
序列中的数据。元素显然有一个“store_site”,但该值是一个字符串,而不是某个更高级别的数据结构
在第二个示例中:
for store in inventory_list.items():
...
dict上的items()方法生成键/值元组序列。它不会生成一系列键(我认为您等同于“存储”)。这就解释了为什么存储['serial_id']
会失败。您的第一位代码
for store in inventory_list:
print(store['serial_id'])
正在引发错误,因为字典中的k的迭代字典的键,将每个键设置为k
。当您试图访问store[x]
时,您告诉python访问字符串store
的x
位置的字符。但是,您使用了一个字符串(serial\u id
),因此python告诉您它需要一个整数
第二段
for store in inventory_list.items():
print(store['serial_id'])
执行相同的错误,但在本例中,存储的是(键、值)
的元组。使用此语法的更常见方法是访问键和值,如下所示:
inventory_list = {
'store a': { 'apples': 10, 'bananas': 5, 'cows': 2 },
'store b': { 'apples': 5, 'bananas': 10 } }
for store, stuff in inventory_list.items():
print( store )
print( stuff )
返回
store a
{'apples': 10, 'bananas': 5, 'cows': 2}
store b
{'apples': 5, 'bananas': 10}
如果要遍历每个嵌套字典中的数据,请像访问顶级项一样进行
# using the same inventory list as before
for k,v in inventory_list.items():
print("Inventory for store " + k)
# v is a reference to a dictionary, so iterate through that
for item, qtt in v.items():
print("Item: " + item + "; quantity: " + str(qtt))
输出:
Inventory for store store a
Item: apples; quantity: 10
Item: bananas; quantity: 5
Item: cows; quantity: 2
Inventory for store store b
Item: apples; quantity: 5
Item: bananas; quantity: 10
store a has 10 apples in stock
store b has 5 apples in stock
{'apples': 10, 'bananas': 5, 'cows': 2}
{'apples': 5, 'bananas': 10}
10
5
store a
apples: 10
apples: 20
store b
apples: 5
apples: 2
如果要直接访问嵌套字典中的数据,可以使用“按键”进行访问:
for k,v in inventory_list.items():
print(k + " has " + str(v['apples']) + ' apples in stock')
输出:
Inventory for store store a
Item: apples; quantity: 10
Item: bananas; quantity: 5
Item: cows; quantity: 2
Inventory for store store b
Item: apples; quantity: 5
Item: bananas; quantity: 10
store a has 10 apples in stock
store b has 5 apples in stock
{'apples': 10, 'bananas': 5, 'cows': 2}
{'apples': 5, 'bananas': 10}
10
5
store a
apples: 10
apples: 20
store b
apples: 5
apples: 2
如果您有一个字典列表,您可以使用列表中x的对其进行迭代,其中每个x
都是字典:
inventory_list = [
{ 'apples': 10, 'bananas': 5, 'cows': 2 },
{ 'apples': 5, 'bananas': 10 }]
for store in inventory_list:
print( store )
输出:
Inventory for store store a
Item: apples; quantity: 10
Item: bananas; quantity: 5
Item: cows; quantity: 2
Inventory for store store b
Item: apples; quantity: 5
Item: bananas; quantity: 10
store a has 10 apples in stock
store b has 5 apples in stock
{'apples': 10, 'bananas': 5, 'cows': 2}
{'apples': 5, 'bananas': 10}
10
5
store a
apples: 10
apples: 20
store b
apples: 5
apples: 2
或直接访问值:
for store in inventory_list:
print( store['apples'] )
输出:
Inventory for store store a
Item: apples; quantity: 10
Item: bananas; quantity: 5
Item: cows; quantity: 2
Inventory for store store b
Item: apples; quantity: 5
Item: bananas; quantity: 10
store a has 10 apples in stock
store b has 5 apples in stock
{'apples': 10, 'bananas': 5, 'cows': 2}
{'apples': 5, 'bananas': 10}
10
5
store a
apples: 10
apples: 20
store b
apples: 5
apples: 2
将所有这些放在一起,以访问字典列表字典中的数据:
dd = { 'store a': [ { 'apples': 10, 'bananas': 5, 'cows': 2 }, { 'apples': 20, 'bananas': 15, 'cows': 25 } ],
'store b': [{ 'apples': 5, 'bananas': 10 }, {'apples': 2, 'bananas': 30, 'cows': 0}] }
for k,v in dd.items():
print(k)
for list_item in v: # v is the list, list_item is each dictionary
print('apples: ' + str(list_item['apples']))
输出:
Inventory for store store a
Item: apples; quantity: 10
Item: bananas; quantity: 5
Item: cows; quantity: 2
Inventory for store store b
Item: apples; quantity: 5
Item: bananas; quantity: 10
store a has 10 apples in stock
store b has 5 apples in stock
{'apples': 10, 'bananas': 5, 'cows': 2}
{'apples': 5, 'bananas': 10}
10
5
store a
apples: 10
apples: 20
store b
apples: 5
apples: 2
如果有疑问,我强烈建议您在代码中添加print
语句,以确定每个变量的外观(是字符串、元组还是字典?等等)。它将极大地帮助您理解和调试代码
所有这些信息都可以从和中收集。您能否提供一个?例如,我们可以运行一些特定的输入来验证您的示例?您是否阅读了有关defaultdict
s的文档?如果defaultdict
中的值是另一个数据结构,则只需使用该数据结构的相应函数迭代即可。请阅读有关dict
s的文档。您没有正确使用它们。它在网上有很好的文档记录。在最后一段中,站点
是什么?我想你所需要的就是存储在库存清单中。value():print(store['serial\u id'])
,但不清楚你想要的输出是什么。我更新了我的原始帖子,提供了更多信息,但清单实际上是一个字典列表的字典。如果它是列表的字典,我可以使用.items()打印列表中的值,我以前也这样做过。我不知道如何进一步深入了解这种类型的数据结构,以获得详细的响应。我已经更新了我原来的问题。我的问题是,它是一个字典列表的字典。我很难找到上一个字典结构中的值。是的,谢谢。这是我的障碍,我不得不进一步深入列表。我不知道您可以迭代items()给出的值。提高评分,感谢您的多次回复。