简体   繁体   English

如何根据另一个字典中的奇数和偶数值创建字典?

[英]How to create a dictionary based on odd and even values from another dictionary?

I have the following dictionary: 我有以下字典:

{'key1': [value1], 'key2': [value2], 'key3': [value3], 'key4': [value4] }

what I want to do is to use the values to create a new dictionary. 我想要做的是使用值来创建一个新的字典。 The value of the odd elements will be the new keys, and the value of the next element will be the new value of that key. 奇数元素的值将是新键,下一个元素的值将是该键的新值。 I mean: 我的意思是:

{'value1': [value2], 'value3': [value4]}

how can i do it? 我该怎么做? i would like to clarify that this is a small example, since dictionaries have hundreds of elements, therefore the solution should be scalable to dictionaries with any amount of elements. 我想澄清这是一个小例子,因为字典有数百个元素,因此解决方案应该可以扩展到具有任意数量元素的字典。

Assuming, that 1) no KeyError will occur, ie there are no odd-indexed dictionary values with the same values, and 2) you have even number of entries in your dict, I would do something like this: 假设1)没有KeyError会发生,即没有奇数索引的字典值具有相同的值,2)你的dict中有偶数个条目,我会这样做:

vals = my_dict.values()
new_dict = {}
for i in range(0, len(vals), 2):
    new_dict[vals[i]] = vals[i+1]

But, as others pointed out, dictionaries don't actually have order, so it's a "let's say" solution. 但是,正如其他人所指出的那样,词典实际上并没有秩序,所以这是一个“让我们说”的解决方案。

Steps to follow: 要遵循的步骤:

  1. Extract Keys Array from DictObj 从DictObj中提取Keys Array
  2. List them to category Even and Odd Indexed Array 将它们列为偶数和奇数索引数组
  3. Create a dictionary out of these two array. 从这两个数组中创建一个字典。

     dictObj = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4' }; print (dictObj.keys()); evenArray = dictObj.keys()[1::2]; oddArray = dictObj.keys()[::2]; dictObjNew = dict(zip(evenArray, oddArray)) print dictObjNew 

暂无
暂无

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

相关问题 如何使用来自另一个字典的聚合值创建一个新字典 - How to create a new dictionary using aggregate values from another dictionary 如何根据一个字典修改或创建字典,查询Python中另一个字典的值 - How to modify or create a dictionary based on one dictionary querying the values of another dictionary in Python 从字典中过滤偶数和奇数值并将这些偶数和奇数值添加到lists.in python - filter even and odd values from a dictionary and add these even and odd values to lists.in python 从字典中过滤偶数和奇数值并将这些偶数和奇数值添加到列表中并将它们写入 txt 文件 - filter even and odd values from a dictionary and add these even and odd values to lists and write them to a txt file 如何从另一个字典值制作字典 - How to make a dictionary from another dictionary values Python - 根据给定的键数动态创建新字典中的键数:值:另一个字典中的值 - Python - dynamically create number of key:values in a new dictionary, based on given number of key:values from another dictionary 如何将字典中的值存储到另一个字典中 - How to store values from a dictionary into another dictionary 如何根据匹配键将一个字典中的值添加到另一个字典中? - How to add values from one dictionary to another based on matching keys? 从另一个字典的特定值创建新的 Python 字典 - Create new Python dictionary from specific values from another dictionary 根据重复值从字典创建嵌套字典 - Create a nested dictionary from a dictionary based on repeated values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM