Reputation: 31
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.
Upvotes: 0
Views: 144
Reputation: 1928
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
Upvotes: 0
Reputation: 3375
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
Upvotes: 0
Reputation: 7514
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()))
Upvotes: 2