Python Forum
Filter list respecting seniority & no more than 3 same number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter list respecting seniority & no more than 3 same number
#11
(Aug-31-2017, 08:13 PM)nilamo Wrote: No, that's a list: [ ]. A quirk of the forum's editor removes empty lists when you preview posts. I went back and edited that post so it should be runnable code now. 

Whoops.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Alright ! so a little update :)

I believe i managed to understand most of the content of the code you wrote & tweaking it best i can with the limited knowledge i got in python Shifty

I am confused about the line 9 which is 'assignments = [ ]'

I believe it will refer to the list of the assigned week.
Can i get few pointer on how i am supposed to build that list?
I guess it will need to be built from the result of the code you wrote right?

Also, what is the reference to week_num, what does it do and where does it come from, is it built-in into python or something ?

from collections import OrderedDict

preferences = OrderedDict([('Jean', (1, 2, 3, 4, 5)), ('Claude', (1, 2, 3, 4, 5)),  ('Van', (1, 2, 3, 4, 5)),
                     ('Kung', (1, 2, 3, 4, 5)), ('Fu', (1, 2, 3, 4, 5)), ('Panda', (1, 2, 3, 4, 5)),
                     ('To',(1, 2, 3, 4, 5)), ('Much', (1, 2, 3, 4 , 5)), ('Absolver', (1, 2, 3, 4, 5))])

weeks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
         31, 32,33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]

assignments = [ ]

tracking = {week_num: 0 for week_num in weeks}
assignments = [ ]
for preferences in [ ]:
    assignments.append([ ])
    for week_num in preferences:
        if tracking[week_num] < 3:
            assignments[-1].append(week_num)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break

for key, value in assignments.items() :
    print (key, value)
Reply
#13
assignments = []
This assigns an empty list to the variable assignments. It allows you to be able to append items to the list dynamically
as is done with the statement
assignments[-1].append(week_num)
The -1 tells append to add the item to the end of the list.
if you temporarily add:
print('assignments: {}'.format(assignments))
after the append statement, you can watch the list build.

Your second questions (how can I build the list) is answered above.
Reply
#14
Thank Larzi, I feel we are so close.
Do you have any idea why it's only printing: "assignments: []"
i Believe i probably changed something in the code that i shouldn't have but i can't figure it out ...

I tried to put it in multiple places within the code, it never returns an error, sometimes it does print, sometimes it says Process Finished with exit code 0

... Huh


from collections import OrderedDict

preferences = OrderedDict([('Jean', (1, 2, 3, 4, 5)), ('Claude', (1, 2, 3, 4, 5)),  ('Van', (1, 2, 3, 4, 5)),
                     ('Kung', (1, 2, 3, 4, 5)), ('Fu', (1, 2, 3, 4, 5)), ('Panda', (1, 2, 3, 4, 5)),
                     ('To',(1, 2, 3, 4, 5)), ('Much', (1, 2, 3, 4, 5)), ('Absolver', (1, 2, 3, 4, 5))])

weeks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
         31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]

tracking = {week_num: 0 for week_num in weeks}
assignments = [ ]
for preferences in [ ]:
    assignments.append([ ])
    print('assignments: {}'.format(assignments))
    for week_num in preferences:
        if tracking[week_num] < 3:
            assignments[-1].append(week_num)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break
Reply
#15
for preferences in []:
is the same as saying for preferences in empty list.
change to:
 for element in preferences.items():
Reply
#16
I get the following error from the code below

Traceback (most recent call last):
File "C:/Users/h.villeneuve/PycharmProjects/OrderedDict/OrderedDict.py", line 29, in <module>
if tracking[week_pool] < 3:
TypeError: unhashable type: 'list'
assignments: [[]]

from collections import OrderedDict

ids_names = {
    ("01", "Jean"),
    ("02", "Claude"),
    ("03", "Van"),
    ("04", "Damme"),
    ("05", "Kristopher"),
    ("06", "Bianca"),
}

week_pool = list(range(1, 53))

employee_choices = OrderedDict([
    ("01", [1,2,3,4,5]),
    ("02", [1,2,3,4,5]),
    ("03", [1,2,3,4,5]),
    ("04", [1,2,3,4,5]),
    ("05", [2,3,4,5,6]),
    ("06", [1,2,3]),
])

tracking = {week_num: 0 for week_num in week_pool}
assignments = [ ]
for element in employee_choices.items():
    assignments.append([ ])
    print('assignments: {}'.format(assignments))
    for week_num in employee_choices:
        if tracking[week_pool] < 3:
            assignments[-1].append(week_pool)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break

So according to a website, i must convert the employee_choices tuple to a list, which i did.

But now it says "expected at most 1 arguments, got 5."
Which can be resolved by Using the str.format method to provide a single string to input.

Am i destroying everything or ...


from collections import OrderedDict

ids_names = {
    ("01", "Jean"),
    ("02", "Claude"),
    ("03", "Van"),
    ("04", "Damme"),
    ("05", "Kristopher"),
    ("06", "Bianca"),
}

week_pool = list(range(1, 53))

employee_choices = OrderedDict(
    ("01", (1,2,3,4,5)),
    ("02", (1,2,3,4,5)),
    ("03", (1,2,3,4,5)),
    ("04", (1,2,3,4,5)),
    ("05", (2,3,4,5,6)),
)

tracking = {week_num: 0 for week_num in week_pool}
assignments = [ ]
for element in employee_choices.items():
    assignments.append([ ])
    print('assignments: {}'.format(assignments))
    for week_num in employee_choices:
        if tracking[week_pool] < 3:
            assignments[-1].append(week_pool)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break

Is what am i doing is considered as spamming ? If yes please let me know.

So i converted the week_pool to a list, which then give me this error

line 12, in <module>
week_pool = list(range[1, 53])
TypeError: 'type' object is not subscriptable


from collections import OrderedDict

ids_names = {
    ("01", "Jean"),
    ("02", "Claude"),
    ("03", "Van"),
    ("04", "Damme"),
    ("05", "Kristopher"),
    ("06", "Bianca"),
}

week_pool = list(range[1, 53])

employee_choices = OrderedDict([
    ("01", [1,2,3,4,5]),
    ("02", [1,2,3,4,5]),
    ("03", [1,2,3,4,5]),
    ("04", [1,2,3,4,5]),
    ("05", [2,3,4,5,6]),
    ("06", [1,2,3]),
])

tracking = {week_num: 0 for week_num in week_pool}
assignments = [ ]
for element in employee_choices.items():
    assignments.append([ ])
    print('assignments: {}'.format(assignments))
    for week_num in employee_choices:
        if tracking[week_pool] < 3:
            assignments[-1].append(week_pool)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break
Reply
#17
this is wrong:
assignments.append([ ])
[python]
[code]assignments.append([ ])
I don't know what you are trying to do.
all that statement does is add abunch empty lists to an empty list!
You need to read a tutorial on lists and dictionaries so that you understand what you are trying to do!
For lists, I suggest: https://python-forum.io/Thread-Basic-Lis...ight=lists
For dictionaries: https://python-forum.io/Thread-Basic-Dic...ctionaries    [/code]
Reply
#18
Back to the code I gave you, the initial assignments = [ ] creates a new, empty list. Then the first for loop loops over the people. So the assignments.append([ ]) adds a new empty list at the end of the list of assignments. That's because at the end, it creates a list of lists, each sub-list being the assignments for one person. Then the assignments[-1].append(week_num) line appends the current week_num to the last sub-list in the assignments list.

The week_num variable comes from the second for loop. For loops in Python are of the format for var in sequence:. The loop takes the first item in sequence and puts it into var. Then it runs all the code indented under the for loop. Then it takes the second item in sequence and puts it into var, and runs the indented code again. It keeps doing that until it runs out of items in sequence.

My code was taking a list and using it to generate another list. You are trying to change it to take a dictionary and generate another dictionary. Larz is right that you need to understand how lists and dictionaries work in order to do that. You should check out the tutorials he linked to.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#19
Ok, i went to deep xD

weeks = list(range(1, 52))
tracking = {week_num: 0 for week_num in weeks}
assignments = [ ]
for preferences in [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]:
    assignments.append([ ])
    print('assignments: {}'.format(assignments))
    for week_num in preferences:
        if tracking[week_num] < 3:
            assignments[-1].append(week_num)
            tracking[week_num] += 1
            if len(assignments[-1]) == 2:
                break
[[1, 2], [1, 2], [1, 2], [3, 4], [3, 4], []]

Work perfectly, thank to all of you, you are all amazing!

I will be more prepared the next time !!

I cant thank you guys enought
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,763 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 7,926 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 2,980 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Count number of occurrences of list items in list of tuples t4keheart 1 2,343 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 1,927 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,897 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 2,312 Jun-11-2020, 08:31 PM
Last Post: Drone4four

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020