繁体   English   中英

如何使用循环创建带有多个键值对的嵌套字典的字典?

[英]How to create a dictionary with nested dictionary with multiple key value pairs using a loop?

你好,我正在构建一个网络抓取程序,我正在努力以我想要的某种方式创建一个字典,经过数小时的搜索,我没有发现任何与我想要做的事情太相似的东西。

这些是我想合并成字典的列表。

number = [1,2]
attributes = [' 75 cm', ' 3 cm', ' 125 cm', ' 7.30 kg', ' 1', ' 27 cm', ' 9 cm', ' 71 cm', ' 21.70 kg', ' 1']
measure_without_duplicates = ['Width:', 'Height:', 'Length:', 'Weight:', 'Package(s):']

这是我想创建字典的具体方式:

dictionary = {1 : {'Width:':' 75 cm', 'Height:':' 3 cm', 'Length:':' 125 cm', 'Weight:': '7.30 kg', 'Package(s):':' 1'}, 2: {'Width:':' 27 cm', 'Height:':' 9 cm', 'Length:':' 71 cm', 'Weight:': '21.70 kg', 'Package(s):':' 1'}

它将需要重复 measure_without_duplicates 中的项目作为属性值的键所需的次数,具体取决于有多少个数字。

这个实例有2个,所以需要迭代两次,因为单个产品需要两个包。 这些度量需要关联到属性数组,但这些不会重复,而是延续到下一个包。

我无法将列表与属性拼接,因为列表至少可以包含 5 个项目,具体取决于需要多少个包。 就像我说的,这是一个网络抓取项目,所以我需要能够拥有灵活的代码。

我创建了一个名为 measure_without_duplicates 的变量的原因是因为我的程序中有另一个数组,称为measures,如下所示:

measure = ['Width:', 'Height:', 'Length:', 'Weight:', 'Package(s):', 'Width:', 'Height:', 'Length:', 'Weight:', 'Package(s):']

我写了一个小代码,从列表中删除任何重复。

我试图为这个问题编写一些代码,但它不会在没有错误发生的情况下工作。 我已经尝试了其他的东西,但我很难获得以前的尝试,所以我会放下我拥有的最新代码:

for key in range(len(number)):
    for iteration in range(len(measure_without_duplicates)):
        dictionary[str(key+1)][measure_without_duplicates[iteration]]=attributes[iteration]
print(dictionary)

我收到一个错误:

    dictionary[str(key+1)][measure_without_duplicates[iteration]]=attributes[iteration]
KeyError: '1'

我真的很感激任何帮助,感谢您的时间。

使用iterzip iter将确保你只迭代它你消耗了多少,在这里它允许我们从我们停止的地方重新开始:

number = [1,2]
attributes = [' 75 cm', ' 3 cm', ' 125 cm', ' 7.30 kg', ' 1', ' 27 cm', ' 9 cm', ' 71 cm', ' 21.70 kg', ' 1']
measure_without_duplicates = ['Width:', 'Height:', 'Length:', 'Weight:', 'Package(s):']

attrs = iter(attributes)

print({i: dict(zip(measure_without_duplicates, attrs)) for i in number})

{1: {'Width:': ' 75 cm', 'Height:': ' 3 cm', 'Length:': ' 125 cm', 'Weight:': ' 7.30 kg', 'Package(s):': ' 1'}, 2: {'Width:': ' 27 cm', 'Height:': ' 9 cm', 'Length:': ' 71 cm', 'Weight:': ' 21.70 kg', 'Package(s):': ' 1'}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM