Bottom Page

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

Quote
#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)
Quote
#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.
Azerate likes this post
Quote
#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
Quote
#15
for preferences in []:
is the same as saying for preferences in empty list.
change to:
 for element in preferences.items():
Azerate likes this post
Quote
#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
Quote
#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]
Quote
#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.
Azerate likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#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
nilamo likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding MINIMUM number in a random list is not working Mona 5 149 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  finding the closest floating point number in a list Skaperen 17 581 Sep-19-2019, 10:39 PM
Last Post: Skaperen
  Filter only highest version of list with alpanumeric names Haasje 6 249 Aug-31-2019, 10:17 AM
Last Post: perfringo
  Checking number in a list to see if they are beside each other Allaye 5 223 Aug-26-2019, 07:15 AM
Last Post: perfringo
  String + Number from List BollerwagenIng 5 254 Aug-25-2019, 07:47 PM
Last Post: perfringo
  Counting number of occurrences of a single digit in a list python_newbie09 12 493 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Count the number of items in a nested list iofhua 2 299 Jun-07-2019, 06:47 PM
Last Post: iofhua
  Filter to List and/or Tuple conversion vindo 3 351 May-18-2019, 05:40 AM
Last Post: perfringo
  How to list number of times element is in defaultdict and delete it mrapple2020 3 342 Apr-15-2019, 07:34 AM
Last Post: perfringo
  determine if an number is in a list Dbeah 7 713 Nov-06-2018, 12:11 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)