简体   繁体   English

如何在 Python 中比较元组元组中的元素

[英]How to compare elements in a tuple of tuples in Python

I am new to python and I came across this exercise:我是 python 的新手,我遇到了这个练习:

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.我习惯于使用 java 或 c 并以某种方式开发了以前的解决方案,尽管我想要一个更像 python 和优雅的解决方案来解决这个问题。

One of my favorite patterns for counting is by using a defaultdict.我最喜欢的计数模式之一是使用 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:与您的版本相当的 Pythonic 版本(使用f 字符串和解包变量):

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 :我会通过使用Counter来计算水果(同名,因此in比较名称时使用==而不是in ):

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM