Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7009307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:50:43+00:00 2026-05-27T21:50:43+00:00

So, I have a list of groups [[‘a’, ‘b’], [‘c’, ‘d’, ‘e’], [‘f’]] and

  • 0

So, I have a list of groups

[['a', 'b'], ['c', 'd', 'e'], ['f']]

and I need to shuffle a flattened version of this list

[a, b, c, d, e, f]

so that elements of the same group would end at some distance from each other. E. g.

[a, c, b, d, f, e] and not [a, c, b, d, e, f], because d and e are in the same group.

I don’t care if the distance is just one element or more, but any element must not be near another element from it’s group. Is there any algorithm for this?

The algorithm also needs to tell if this cannot be done.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:50:44+00:00Added an answer on May 27, 2026 at 9:50 pm

    This code makes a copy of your original list of groups and takes each time a random element from the (at each moment) largest remaining group. Not quite randomized, but should work for any case when there is no group with over the half of all elements.

    import random
    
    list_of_groups = [['a', 'b'], ['c', 'd', 'e'], ['f']]
    l = [group[:] for group in list_of_groups] # make a copy
    random.shuffle(l)
    out = []
    prev_i = -1
    while any(a for a in l):
        new_i = max(((i,a) for i,a in enumerate(l) if i != prev_i), key=lambda x: len(x[1]))[0]
        out.append(l[new_i].pop(random.randint(0, len(l[new_i]) - 1)))
        prev_i = new_i
    
    print out
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of MyObjects which I need to divide into three groups:
Say I have a list of all Projects , and that I group them
I have a list of Groups that can vary in number, with Items in
I have a rest service which has a list of Groups each group has
I have a list of member IDs that I need to loop over and
I have a list of names. I want to partition this list into groups
I have a list that contains groups of nearly identical numeric values. i.e. (1004.523,
I have an expandable list that contains groups : continents and child : countries.
I have User object with a list of groups that it belongs to: public
I have a list of lists that looks like this: animal_groups = [['fox','monkey', 'zebra'],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.