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 6126075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:19:34+00:00 2026-05-23T16:19:34+00:00

Today, on the basis of answers I got here, I wrote this little code

  • 0

Today, on the basis of answers I got here, I wrote this little code to create a random list of 16 elements.

import random

sources = ['Prone', 'Supine', 'Halfway', 'HalfInv']
result = [random.choice(sources)]
repeats = 0
fail = 0

while len(result) < 16:
    elem = random.choice(sources)
    repeats = result.count(elem)
    print(repeats)
    if (elem != result[-1]) & (repeats < 4):
        result.append(elem)
    else:
        fail = fail + 1
        print(fail)
        if fail > 100:
            result = [random.choice(sources)]

print(result)

Now what I’d like to do is:
1. to create 2 random lists naming them differently (how do I do this based on the for loop counter?)
2. put those 2 lists as columns in a tab delimited (txt) file, one next to the other in order to easily copy paste them in an excel file. I looked into csv module but it seems to only have methods for rows.

  • 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-23T16:19:35+00:00Added an answer on May 23, 2026 at 4:19 pm

    If you’re looking for a more efficient way to get a randomly ordered list of 4-of-each of your list elements from sources, try random.shuffle(list):

    >>> import random
    >>> random.seed()
    >>> sources = ['Prone', 'Supine', 'Halfway', 'HalfInv']
    >>> copy1 = sources * 4
    >>> copy2 = sources * 4
    >>> copy1 == copy2
    True
    >>> random.shuffle(copy1)
    >>> random.shuffle(copy2)
    >>> copy1 == copy2
    False
    >>> copy1
    ['HalfInv', 'Prone', 'Halfway', 'Supine', 'Prone', 'Halfway', 'Prone', 'Supine', 'Prone', 'HalfInv', 'HalfInv', 'Halfway', 'Supine', 'Halfway', 'HalfInv', 'Supine']
    >>> copy2
    ['Prone', 'Halfway', 'Prone', 'Prone', 'HalfInv', 'Halfway', 'Halfway', 'HalfInv', 'Supine', 'HalfInv', 'Halfway', 'Supine', 'Prone', 'Supine', 'HalfInv', 'Supine']
    

    Next, you wanted to generate and name the lists based on the loop counter… I’d recommend against this. Instead just append your random lists into a list one at a time, and you can index them in the order they were appended.

    >>> n = 2 # number of random lists you want
    >>> rand_lists = [] # A list for holding your randomly generated lists
    >>> for i in range(n):
        sources_copy = sources * 4
        random.shuffle(sources_copy)
        rand_lists.append(sources_copy)
    

    But this still leaves you with the problem that your data is in two separate lists. To remedy this you want the zip() function. It joins each element in list 1 with its correspondingly indexed element in list 2, forming a tuple at each index. This can be done with an arbitrary number of sequences.

    >>> list1 = [1,2,3,4,5]
    >>> list2 = ['a','b','c','d','e']
    >>> zip(list1, list2)
    [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]
    

    To zip together your lists-within-a-list, use the * star unpacking operator before the name of your meta-list.

    >>> my_data = zip(*rand_lists)
    

    Now you want to output to a csv file; it’s easy enough to just write your own csv function. Remember that the column delimiters ate commas, the row delimiters are newlines.

    def out_csv(mydata, filename):
        with open(filename, 'w') as out_handle:
            for line in mydata:
                out_handle.write(','.join(line) + '\n')
    

    From here just pass your zipped list combo into the out_csv function, and make sure your filename has the .csv extension. Excel can natively read in .csv files so you shouldn’t need to do any copy-pasting.

    Here’s the last step:

    >>> out_csv(my_data, 'my_name.csv')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got asked this interesting interview question today. Explain in detail the process by which
Stupid question here. Got total brain-lock today. I want to manually select a specific
Today I was reading Floyd's algorithm of detecting loop in a linked list. I
I've got a Windows DLL that I wrote, written in C/C++ (all exported functions
When doing a code review for a colleague today I saw a peculiar thing.
I was always wondering about this and today I have finally came to a
Earlier today I asked this question which arose from A- My poor planning and
I'm having an Idiot Day today. I'm sure this is relatively simple, but my
I read an interesting DailyWTF post today, Out of All The Possible Answers... and
I came across something very basic but extremely bewildering today. I needed to convert

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.