Thank you for everyone who has helped. i have made the code work thanks to one comment pointing out my mistake, i would now like to add a function for 2 more options to sort this code numerically ascending and descending
fruit= [["apples", 5], ["pears", 10], ["oranges", 6], ["bananas", 12], ["grapes", 100]]
items= [["cars",3], ["sheets",5], ["bananas",8], ["bridges",17], ["roads",0], ["doors",1]]
colours= [["blue",1], ["black",7], ["orange",13], ["yellow",2], ["red",8], ["blue",5]]
merge = fruit items colours
result = set(
(item, sum(quantity for label, quantity in merge if label == item))
for item, _ in merge
)
print ()
print ("SHOW DATA")
print ()
print ("fruit :", fruit)
print ("items :", items)
print ("colours :", colours)
print ()
print ("WHICH ORDER?")
print ()
print ("1 = Alphabetically Ascending")
print ("2 = Alphabetically Descending")
print ("")
SortOrder = int(input ("Enter the sort order you prefer for the merged data >"))
if (SortOrder == 1):
lists = sorted(result)
for l in lists:
print(l)
elif (SortOrder == 2):
lists = sorted(result, reverse=True)
for l in lists:
print(l)
CodePudding user response:
A set does not have the .sort()
method, you need to use the sorted()
method, and you are looping through lists
which in not defined.
Here is a solution :
if (SortOrder == 1):
lists = sorted(result)
for l in lists:
print(l)
elif (SortOrder == 2):
lists = sorted(result, reverse=True)
for l in lists:
print(l)
CodePudding user response:
You currently build result
by iterating over all the lists once (to calculate the sum) for every element in all the lists, which makes it an O(n^2) operation. Instead, iterate over all the lists once for O(n), and build a dictionary out of the key-value pairs.
result = {}
for key, value in merge:
if key in result:
result[key] = value
else:
result[key] = value
Which gives the following result
:
{'apples': 5, 'pears': 10, 'oranges': 6,
'bananas': 20, 'grapes': 100, 'cars': 3,
'sheets': 5, 'bridges': 17, 'roads': 0,
'doors': 1, 'blue': 6, 'black': 7,
'orange': 13, 'yellow': 2, 'red': 8}
Next, you want to take the user's input to decide how to sort your output. We can sort just by calling sorted
on result.items()
, which is a collection of the key-value pairs in the dictionary. Read about the sorted
function here: https://docs.python.org/3/howto/sorting.html
print("1: Sort by name, ascending")
print("2: Sort by name, descending")
print("3: Sort by value, ascending")
print("4: Sort by value, descending")
sort_order = int(input("Enter sort order: "))
# Sort by value if input is 3 or 4
sort_numeric = (sort_order > 2)
# Sort descending if input is even
sort_reversed = (sort_order % 2 == 0)
sort_reversed
is now a boolean value that tells you what the reverse
argument to sorted()
should be.
Let's sort the items in our dictionary. To sort the items numerically, we use the key
argument to the sorted
function. This key
needs to be a function, which is given one argument -- the element that is being sorted -- and returns the value to sort it by -- the 1
th element (i.e. the value part) of the key-value pair in the dictionary:
if sort_numeric:
sorted_result = sorted(result.items(),
reverse=sort_reversed,
key=lambda kvp: kvp[1])
else:
sorted_result = sorted(result.items(),
reverse=sort_reversed)
And let's print our output:
for item, qty in sorted_result:
print(f"{item}\t{qty}")
For an input of 4
, we get this output:
grapes 100
bananas 20
bridges 17
orange 13
pears 10
red 8
black 7
oranges 6
blue 6
apples 5
sheets 5
cars 3
yellow 2
doors 1
roads 0