Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 9
Threads: 1
Joined: Aug 2017
Sep-01-2017, 04:28 PM
(This post was last modified: Sep-01-2017, 04:31 PM by Azerate.)
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
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)
Posts: 12,038
Threads: 487
Joined: Sep 2016
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.
Posts: 9
Threads: 1
Joined: Aug 2017
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
...
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
Posts: 12,038
Threads: 487
Joined: Sep 2016
for preferences in []: is the same as saying for preferences in empty list.
change to:
for element in preferences.items():
Posts: 9
Threads: 1
Joined: Aug 2017
Sep-01-2017, 07:03 PM
(This post was last modified: Sep-01-2017, 07:21 PM by Azerate.)
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
Posts: 12,038
Threads: 487
Joined: Sep 2016
Sep-01-2017, 07:26 PM
(This post was last modified: Sep-01-2017, 07:26 PM by Larz60+.)
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]
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 9
Threads: 1
Joined: Aug 2017
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
|