2

I have a program that I'm writing for a computer class, and I'm having a little bit of trouble accessing the index value of something. I'm fairly new to python (I have only been using it for a week) and I need this program to work successfully. I've already tried other posts and have used content from other posts, but I'm still struggling with trying to make my program work. I hope this is enough to work with.

I want to print the corresponding index to each item in the "tasks" list at the bottom "wModt" spot. I've tried asking one of the instructors but I was still confused. I want the tasks to be chosen in the index order 0, 1, 2, etc. and then repeat. Please help!

Here is some of my code.

workers = ['Jim','Bob','Matt','Brian','Evan','Alan','Gary','Henry','Noah']

tasks = ['Drilling','Digging','Filling','Pouring']

...

while len(workers) > 0:
    wModt = num_workers % num_tasks

    working = random.choice(workers)
    workers.remove(working)
    num_workers -= 1

    for idx, val in enumerate(tasks):
        pass

    print working + "'s job is", wModt

1 Answer 1

1

the best way to do something like this is to zip your lists together ... you can make something repeat by using itertools cycle

from itertools import cycle
for worker,job in zip(workers,cycle(tasks)):
    #do something with them

you could likewise make a dictionary mapping workers to jobs

datamap = dict(zip(workers,cycle(tasks))
print datamap["Jim"]

that said you can easily get the index of something by asking

jims_index = workers.index("Jim")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.