Issue
I got a Unexpected type(s): (set\[Selfcheckout | int\]) Possible type(s): (SupportsKeysAndGetItem) (Iterable\[tuple\[Any, Any\]\])
error.
Lane
is a normal counter, and Self
is a self checkout both have an attribute in the form of queue which is just a list of customers.
I'm trying to sort this list of Lanes
and Self
s called openqueues
using a dictionary.
I don't really have an idea what to do next, this function is just supported to return the sorted list of lanes basted on their lengths.
def getsortedopen(openqueues):
sort = []
dictionary = {}
if isinstance(openqueues, list):
for lane in openqueues:
if isinstance(lane, Lane) | isinstance(lane, Self):
dictionary.update({lane, len(lane.queue)})
dictionary = sorted(dictionary.items(), key=lambda x: x[1])
dictionaryb = dict(dictionary)
for key in dictionaryb:
sort.append(key)
Solution
Changes needed:
def getsortedopen(openqueues):
sort = []
dictionary = {}
if isinstance(openqueues, list):
for lane in openqueues:
if isinstance(lane, Lane) or isinstance(lane, Self):
dictionary[lane] = len(lane.queue)
# Sorting the dictionary by the length of the queues
sorted_dict = dict(sorted(dictionary.items(), key=lambda x: x[1]))
# Appending the lanes to the result list
sort.extend(sorted_dict.keys())
return sort
- Use or instead of | for logical OR.
- Use {} to create a dictionary.
- Simplify the sorting and appending process.
Answered By - TheHungryCub
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.