Python Forum

Full Version: list or dictionary manipulation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So here's the skinny. I am new to python, I'm new to JavaScript. I've been writing bash scripts for a while now. So I have some knowledge but not where it needs to be to do what I want. So I'll state my question, but I'd prefer tips, what to read, where to find examples, or example codes. I'd rather not get an answer that is the full code of what I'm wanting to do. That discourages learning.

I'm trying to create a list and manipulate it through a function. So at work I have to assign tickets to a total of 8 guys. But I need an easy way to keep up with how many tickets each guy has been assigned. Not the ticket number, but just a "tally" mark for each ticket he has been assigned. I've been writing down there names and putting a tally mark each time I've assigned a ticket but that is tedious.

I'd like the code to keep up with this and when I run it, the code will tell me who is up next in line to have a ticket assigned to them. Not randomly. It needs to tell me who to assign it to by who has the least amount of tickets assigned to them. I'm not 100% sure how to go about this. Any help would be appreciated.
You might try starting here: Free-Python-Resources
Sparkz_alot,

Thanks. I've gone through the Codecademy.com training for Python. I think my biggest issue now is figuring out exactly what I need it to do to achieve the results I'm looking for. I'm gonna go through some of the other courses listed in the other post also.
Store it in a dictionary, and increment it every time someone gets a ticket:

tickets['Bob'] += 1
A defautldict from the collections module will work well for this, since you won't have to initialize it for new staff. Then:

ticket_counts = [(count, name) for name, count in tickets.items()]
ticket_counts.sort()
print(ticket_counts[0][1])
That will get you the name of the person with the least tickets. Technically it will give you the person with the first name alphabetically of those with the lowest ticket count. You could use print(ticket_counts[:3]) to see the top three if you wanted.
ichabod801,

Thanks for the tips. I was thinking a dictionary would be the way to go, but because of my lack of experience I kept doubting myself. Also, I've never heard of the defaultdict, so I'll have to look in to that. But it sounds like it will be helpful.
Definitely look at the collections module. It's one of the more useful modules out there without an obvious use. Another one is functools, although I don't think it has application to this project.