I am trying to sort a python list of strings. I know that I can use the method sorted and set the attribute key to be a function that implements the behavior I need to sort the elements of the dictionary on. My problem is that this method needs an argument.
UPDATE: I want the method to generalize to multiple arguments.
Example: I want to sort a list of strings based on their priorities in 2 dictionaries . So I need to use those priority dictionaries in sorting the list.
I want something like:
sorted(myList, key=sortingAlgorithm(priorityDictionary1, priorityDictionary2), reverse=True)
This can be done if I set the priority dictionaries as global variables and then I will not need to have it as an argument to the sorting algorithm. But, I want to be able to do it without global variables.
If this is not possible, can you please recommend the pythonic way of doing this.
Thanks in advance.
sortingAlgorithm()in your code ? Thesorted()function has its own sorting algorithm, what it needs is instructions where each item should go, compared to the others.Key=needs to be "a function which takes a list item, and returns a number/string/sortable value". And as the answers come in, there are many approaches to that - but none of them involve passing a sorting algorithm to sort...dict1[x] + dict2[x]ordict2[dict1[x]]?