Python program to split a dictionary and its keys into K equal dictionaries

PHPz
Release: 2023-08-28 08:57:22
forward
1138 people have browsed it

Python program to split a dictionary and its keys into K equal dictionaries

A dictionary is a unique form of array used to implement data structures in Python. Dictionaries have several related features that make them a very powerful tool in Python. It stores data in the form of key-value pairs, where each key is a unique identifier used to access the corresponding value associated with it.

We can perform various operations on this dictionary and manipulate the data stored in it. This article will explain such an operation, where we split a dictionary and its keys into K equal dictionaries.

Understanding Questions

We have to pass a dictionary and then split it into K equal dictionaries, where "K" is the size of the original dictionary. The way of partitioning should be to divide all keys equally. Let us understand this through an example -

Input: dict1 = {"Score":100, "Age": 40, "Salary": 25000, "cutoff":44}
Output: [{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}]
Copy after login

Here each value associated with a different key is reduced to 1/K times the original value and a list of K dictionaries is returned. Now that we've discussed the problem statement, let's discuss some solutions.

Use iteration

In this method, we will pass a sample dictionary and then get the "K" value with the help of "len()" method. This method will return the length of the dictionary. After that, we will iterate over the example dictionary and divide each "key value" by K with the help of the "/" operand.

We will store these divided values ​​in an empty dictionary, and then add all newly created dictionaries to an empty list with the help of the "append()" method.

Example

dict1 = {"Score":100 , "Age": 40, "Salary": 25000, "cutoff":44}
print(f"Original dictionary is: {dict1}")
K = len(dict1)
print(f"The value for K is: {K}")

empLis = []
empDict ={}
for keys in dict1:
   empDict[keys] = dict1[keys]/K
   empLis.append(empDict)

print(f"The newly divided dictionary is: {empLis}")
Copy after login

Output

Original dictionary is: {'Score': 100, 'Age': 40, 'Salary': 25000, 'cutoff': 44}
The value for K is: 4
The newly divided dictionary is: [{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}]
Copy after login

Use list comprehension and dictionary comprehension

This method is an optimized version of the previous solution. Here we will summarize iterations over a single dictionary and list with the help of dictionary comprehension and list comprehension. After passing the example dictionary, we will create a dictionary in which to store the divided values ​​(DivDict).

Iterate over and return the original dictionary divided by K keys. List (lisDict) stores K dictionaries containing divided values. We specify that the length of the list is equal to the K value.

Example

dict1 = {"Number of sixes":244, "Number of fours": 528, "Strike rate": 164, "Balls faced":864}
print(f"Original dictionary is: {dict1}")
K = len(dict1)
print(f"The value for K is: {K}")

#using dictionary comprehension
DivDict ={key_value:dict1[key_value]/K for key_value in dict1}
#using list comprehension
lisDict = [DivDict for i in range(K)]

print(f"The newly divided dictionary is: {lisDict}")
Copy after login

Output

Original dictionary is: {'Number of sixes': 244, 'Number of fours': 528, 'Strike rate': 164, 'Balls faced': 864}
The value for K is: 4
The newly divided dictionary is: [{'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}]
Copy after login

There are other methods involving the use of: - zip(), lambda(), groupby(), slicing etc.

These methods can be used when we have to introduce certain specifications in the code, such as for a specific value or key in a dictionary. The above solution is the basic approach that can be used to split the sample dictionary into K equal parts.

in conclusion

In this article, we discussed two solutions for partitioning a dictionary and its keys into K equal dictionaries. The first solution revolves around "Loop Concept" where we iterate over the dictionary and add it to a list. The second solution focuses on a more optimized approach, where we summarize the entire loop concept into a single dictionary and list.

The above is the detailed content of Python program to split a dictionary and its keys into K equal dictionaries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!