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
#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


Messages In This Thread
RE: Filter list respecting seniority & no more than 3 same number - by Azerate - Sep-01-2017, 07:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,591 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,291 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,932 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,341 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,104 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  When did the number got included in the list? Frankduc 14 3,157 Feb-03-2022, 03:47 PM
Last Post: Frankduc
  Count number of occurrences of list items in list of tuples t4keheart 1 2,405 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 2,005 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,962 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Topic: “Filter numbers with a list comprehension” (PyBite #107) Drone4four 4 2,418 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