There is a list of dataset and I need to reshuffle them randomly then partition the dataset into n groups. I am using dictionary to store the list of dataset, but I am not sure how to partition items of a dictionary into n groups? What I can think of now is that to divide total of keys by n and get 1/n of keys from the dictionary to make n new dictionaries and then start the rest from there. Is this a practical method? Thanks a lot!
pythonlearning
0
Light Poster
Recommended Answers
Jump to Postit is possible, though simpler is to make a number range of n repeated key_num//n +1 times, shufle and then divide list of keys by taking zip(my_dict.keys(), index_list)
Jump to PostI noticed that better to add random sample of range to fill the not full length in the end as otherwise the lenghts can be not so equal (if many same indexes end up in the end of list in shuffle):
import string import random from pprint …
Jump to PostWhy don't you print out each part of the expression? Maybe cleaner actually to calculate both division and modulo to variables before the expression by divmod function.
All 9 Replies
TrustyTony
888
pyMod
Team Colleague
Featured Poster
pythonlearning
0
Light Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
pythonlearning
0
Light Poster
pythonlearning
0
Light Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
pythonlearning
0
Light Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
pythonlearning
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.