简体   繁体   English

从元组列表中获取每个元组的最大值列表

[英]Get a list of maximum values per tuple from a list of tuples

I'm just getting into Python, and having some trouble understanding the control flow and iteration logic.我刚开始接触 Python,在理解控制流和迭代逻辑方面遇到了一些麻烦。

I am trying to create a function which takes a list of tuples, and I want to return a new list with the maximum element per tuple.我正在尝试创建一个接受元组列表的函数,并且我想返回一个包含每个元组最大元素的新列表。

I know I'm missing putting the maximum element into a new list, but first I am trying to get that maximum value.我知道我没有将最大元素放入新列表中,但首先我试图获得该最大值。

def max_element_per_tuple(tuple_list):
    maximum = tuple_list[0]

    for item in tuple_list:
        if item > maximum:
            maximum = item

    return maximum

# test it
tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]
print(max_element_per_tuple(tuple_list))

This returns: (100, 50, 25)这将返回: (100, 50, 25)

Want returned: (1, 30, 100, 75)想要退货: (1, 30, 100, 75)

Or if a list (?), then: [1, 30, 100, 75]或者如果是列表 (?),则: [1, 30, 100, 75]

Simply, try this one-linear pythonic solution简单地,试试这个单线性pythonic解决方案

>>> tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]
>>> [max(e) for e in tuple_list]       # List
[1, 30, 100, 75]
>>> tuple(max(e) for e in tuple_list)  # Tuple
(1, 30, 100, 75)

Your function max_element_per_tuple is correct (though unnecessary, because standard function max() exists).您的函数max_element_per_tuple是正确的(尽管没有必要,因为存在标准函数max() )。 What you did wrong was calling that function using a list of tuples as the argument.您做错的是使用元组列表作为参数调用该函数。 This found the biggest tuple in the list ("biggest" for tuples means "the one with first element biggest"), which happened to be the third one - (100,50,25) .这找到了列表中最大的元组(元组的“最大”意味着“第一个元素最大的那个”),恰好是第三个 - (100,50,25) What you need to do is either:您需要做的是:

result = list(map(max, tuple_list))

or或者

result = [max(t) for t in tuple_list]

This last one is roughly equivalent to:最后一个大致相当于:

result = []
for t in tuple_list:
    result.append(max(t))

If you replace max with your max_element_per_tuple the results should be the same.如果用max_element_per_tuple替换max ,结果应该是一样的。

Right now you are just looping through the tuples and returning the "biggest" one - tuple comparison is done element-wise.现在你只是遍历元组并返回“最大”的一个 - 元组比较是按元素完成的。

What you want is to add another loop level that will find the maximum of each tuple:你想要的是添加另一个循环级别,它将找到每个元组的最大值:

def max_element_per_tuple(tuple_list):
    res = []

    for tup in tuple_list:      # loops over the tuples in the list
        maximum = tup[0]
        for item in tup:        # loops over each item of the tuple
            if item > maximum:
                maximum = item
        res.append(maximum)

    return res

This gives as expected:这按预期给出:

>>> max_element_per_tuple([(-1, 0, 1), (10, 20, 30), (100, 50, 25), (55, 75, 65)])
[1, 30, 100, 75]

This should work这应该工作

def max_element_per_tuple(tuple_list):
    maximum = []

    for item in tuple_list:
        maximum.append(max(item))


    return maximum

will give this output : [1, 30, 100, 75]将给出这个输出: [1, 30, 100, 75]

The issue: max_element_per_tuple(tuple_list) returns the wrong result because it is looking for the max tuple, not the max value in each tuple.问题: max_element_per_tuple(tuple_list) 返回错误的结果,因为它正在寻找最大元组,而不是每个元组中的最大值。

def max_element_per_tuple(tuple_list):
    maximum = tuple_list[0]  # maximum is = (-1,0,1)

    for item in tuple_list:
        if item > maximum:  # compares the tuples e.g (-1,0,1) == (10,20,30)
            maximum = item

    return maximum  # at the end you have (100,50,25). it's the max tuple

Try any of below options:尝试以下任一选项:

tuple_list = [(-1,0,1), (10,20,30), (100,50,25), (55,75,65)]

# Get the max from each tuple using List comprehension
max_items_list  = [max(tuple_item) for tuple_item in tuple_list]   # in a list
max_items_tuple = tuple(max(tuple_item) for tuple_item in tuple_list)   # in a tuple
print(max_items_list)
print(max_items_tuple)

# Get the max from each tuple using For Loop
# Can be used with List only, tuples are immutable

for_max_items_list = list()

for tuple_item in tuple_list:
  max_value = max(tuple_item)  # get the max of each tuple e.g Max((-1,0,1)) = 1
  for_max_items_list.append(max_value)  # add the max to list
print(for_max_items_list)

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

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