I am new to python and I came across this exercise:
Given the following tuple of tuples, calculate the total number of apples, bananas, melons and pineapples comparing the first element of each tuple:
myTupleofTuples = (("apple",1),("banana",1),("apple",2),("melon",1),("pineapple",2),("banana",2))
for i,j in myTupleofTuples:
counter = 0
for k in range(len(myTupleofTuples)):
if i in myTupleofTuples[k][0]:
counter = counter + myTupleofTuples[k][1]
print(i+" is in " +myTupleofTuples[k][0]+ " the counter of " +i+ " is " ,counter )
I am used to work with java or c and somehow developed the previous solution, although I would like a more python like and elegant solution for the problem.
One of my favorite patterns for counting is by using a defaultdict. Like this:
from collections import defaultdict
myTupleofTuples = (("apple",1),("banana",1),("apple",2),("melon",1),("pineapple",2),("banana",2))
sums = defaultdict(int)
for fruit, count in myTupleofTuples:
sums[fruit] += count
print(list(sums.items()))
Dictionary comes very handy when you are solving this problem. As a example here is the code below.
count_dict = {} # creating an empty dictionary
for fruit,num in myTupleofTuples: # loops each tuple and separate each individual tuple into fruit and num variable
count = count_dict.get(fruit, 0) # get value if fruit exist in dictionary else give 0
count_dict[fruit] = num + count # add the count with number
If you want to print them as you have:
for key,value in count_dict.items():
print('{} is in {} and There are {} {}\n'.format(key, [x for x in range(len(myTupleofTuples)) if myTupleofTuples[x][0]==key] value, key))
# just adding list comprehension for finding index, you can use function as well
Output:
apple is in [0, 2] and There are 3 apple
banana is in [1, 5] and There are 3 banana
melon is in [3] and There are 1 melon
pineapple is in [4] and There are 2 pineapple
A bit more Pythonic version (using f-strings and unpacking variables) equivalent to yours:
for fruit1, number1 in myTupleofTuples:
counter = 0
for fruit2, number2 in myTupleofTuples:
if fruit1 in fruit2:
counter += number2
print(f"{fruit1} is in {fruit2} the counter of {fruit1} is {counter}")
I would have counted the fruits instead (same name, so by using ==
instead of in
when comparing names), by using a Counter :
from collections import Counter
counter = Counter()
for name, value in myTupleofTuples:
counter[name] += value
Another version:
my_tuple_of_tuples = (
("apple", 1),
("banana", 1),
("apple", 2),
("melon", 1),
("pineapple", 2),
("banana", 2)
)
quantities = (
(this_fruit,
sum(quantity
for fruit, quantity in my_tuple_of_tuples
if fruit == this_fruit))
for this_fruit in {tuple_[0] for tuple_ in my_tuple_of_tuples}
)
print(*(f'{fruit}: {quantity}' for fruit, quantity in quantities))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.