简体   繁体   English

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

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

Hello I am building a web scraping program and I am struggling with creating a dictionary in a certain way I would like, and I haven't found anything too similar with what I am trying to do after hours of searching.你好,我正在构建一个网络抓取程序,我正在努力以我想要的某种方式创建一个字典,经过数小时的搜索,我没有发现任何与我想要做的事情太相似的东西。

These are the lists I would like to combine into a dictionary.这些是我想合并成字典的列表。

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):']

This is the specific way I would like to create the dictionary:这是我想创建字典的具体方式:

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

It will need to repeat the items within measure_without_duplicates as many times needed as a key for the values of attributes depending on how many numbers there are.它将需要重复 measure_without_duplicates 中的项目作为属性值的键所需的次数,具体取决于有多少个数字。

There are 2 in this instance, so it needs to iterate twice, because there are two packages needed for a single product.这个实例有2个,所以需要迭代两次,因为单个产品需要两个包。 These measures need to correlate to the attributes array, but these will not repeat, instead carry over to the next package.这些度量需要关联到属性数组,但这些不会重复,而是延续到下一个包。

I cannot splice the list with the attributes because the list can have a minimum of 5 items, depending on how many packages will be needed.我无法将列表与属性拼接,因为列表至少可以包含 5 个项目,具体取决于需要多少个包。 Like I said, this is a web scraping project so I need to be able to have flexible code.就像我说的,这是一个网络抓取项目,所以我需要能够拥有灵活的代码。

The reason I have created a variable called measure_without_duplicates is because I have another array in my program which is called measures and looks like this:我创建了一个名为 measure_without_duplicates 的变量的原因是因为我的程序中有另一个数组,称为measures,如下所示:

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

And I wrote a little code which removes any duplication from the list.我写了一个小代码,从列表中删除任何重复。

I tried to write some code for this problem, but it doesn't work without an error occuring.我试图为这个问题编写一些代码,但它不会在没有错误发生的情况下工作。 I've tried other things but it would be hard for me to get the previous attempts so I'll just put down the latest code I have:我已经尝试了其他的东西,但我很难获得以前的尝试,所以我会放下我拥有的最新代码:

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)

and I get an error:我收到一个错误:

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

I really appreciate any help, thank you for your time.我真的很感激任何帮助,感谢您的时间。

Using iter and zip .使用iterzip iter will make sure you only iterate over it how many you consume and here it's allowing us to pick back up on that iterable where we left off: 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