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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:01:59+00:00 2026-05-12T00:01:59+00:00

I’m trying to create a list of tasks that I’ve read from some text

  • 0

I’m trying to create a list of tasks that I’ve read from some text files and put them into lists. I want to create a master list of what I’m going to do through the day however I’ve got a few rules for this.

One list has separate daily tasks that don’t depend on the order they are completed. I call this list ‘daily’. I’ve got another list of tasks for my projects, but these do depend on the order completed. This list is called ‘projects’. I have a third list of things that must be done at the end of the day. I call it ‘endofday’.

So here are the basic rules.

A list of randomized tasks where daily tasks can be performed in any order, where project tasks may be randomly inserted into the main list at any position but must stay in their original order relative to each other, and end of day tasks appended to the main list.

I understand how to get a random number from random.randint(), appending to lists, reading files and all that……but the logic is giving me a case of ‘hurty brain’. Anyone want to take a crack at this?

EDIT:

Ok I solved it on my own, but at least asking the question got me to picture it in my head. Here’s what I did.

random.shuffle(daily)
while projects:
    daily.insert(random.randint(0,len(daily)), projects.pop(0))
random.shuffle(endofday)
daily.extend(endofday)
for x in daily: print x

Thanks for the answers, I’ll give ya guys some kudos anyways!

EDIT AGAIN:

Crap I just realized that’s not the right answer lol

LAST EDIT I SWEAR:

position = []
random.shuffle(daily)
for x in range(len(projects)):
    position.append(random.randint(0,len(daily)+x))
position.sort()
while projects:
    daily.insert(position.pop(0), projects.pop(0))
random.shuffle(endofday)
daily.extend(endofday)
for x in daily: print x

I LIED:

I just thought about what happens when position has duplicate values and lo and behold my first test returned 1,3,2,4 for my projects. I’m going to suck it up and use the answerer’s solution lol

OR NOT:

position = []
random.shuffle(daily)
for x in range(len(projects)):
    while 1:
        pos = random.randint(0,len(daily)+x)
        if pos not in position: break
    position.append(pos)
position.sort()
while projects:
    daily.insert(position.pop(0), projects.pop(0))
random.shuffle(endofday)
daily.extend(endofday)
for x in daily: print x
  • 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-12T00:02:00+00:00Added an answer on May 12, 2026 at 12:02 am

    First, copy and shuffle daily to initialize master:

    master = list(daily)
    random.shuffle(master)
    

    then (the interesting part!-) the alteration of master (to insert projects randomly but without order changes), and finally random.shuffle(endofday); master.extend(endofday).

    As I said the alteration part is the interesting one — what about:

    def random_mix(seq_a, seq_b):
        iters = [iter(seq_a), iter(seq_b)]
        while True:
            it = random.choice(iters)
            try: yield it.next()
            except StopIteration:
                iters.remove(it)
                it = iters[0]
                for x in it: yield x
    

    Now, the mixing step becomes just master = list(random_mix(master, projects))

    Performance is not ideal (lots of random numbers generated here, we could do with fewer, for example), but fine if we’re talking about a few dozens or hundreds of items for example.

    This insertion randomness is not ideal — for that, the choice between the two sequences should not be equiprobable, but rather with probability proportional to their lengths. If that’s important to you, let me know with a comment and I’ll edit to fix the issue, but I wanted first to offer a simpler and more understandable version!-)

    Edit: thanks for the accept, let me complete the answer anyway with a different way of “random mixing preserving order” which does use the right probabilities — it’s only slightly more complicated because it cannot just call random.choice;-).

    def random_mix_rp(seq_a, seq_b):
        iters = [iter(seq_a), iter(seq_b)]
        lens = [len(seq_a), len(seq_b)]
        while True:
            r = random.randrange(sum(lens))
            itindex = r < lens[0]
            it = iters[itindex]
            lens[itindex] -= 1
    
            try: yield it.next()
            except StopIteration:
                iters.remove(it)
                it = iters[0]
                for x in it: yield x
    

    Of course other optimization opportunities arise here — since we’re tracking the lengths anyway, we could rely on a length having gone down to zero rather than on try/except to detect that one sequence is finished and we should just exhaust the other one, etc etc. But, I wanted to show the version closest to my original one. Here’s one exploiting this idea to optimize and simplify:

    def random_mix_rp1(seq_a, seq_b):
        iters = [iter(seq_a), iter(seq_b)]
        lens = [len(seq_a), len(seq_b)]
        while all(lens):
            r = random.randrange(sum(lens))
            itindex = r < lens[0]
            it = iters[itindex]
            lens[itindex] -= 1
            yield it.next()
        for it in iters:
            for x in it: yield x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.