In Python, write a function that takes in an arbitrary list of numeric triples (tuples of size 3), and returns a new list containing the maximum elements from each
Explanation is in comments wherever needed.
First I have simply written program to print list of max element from each tuple from better understanding.
Then I have written a function as you asked for in the question.
Code:
# List of tuples
listTuples = [(1, 2, 3), (3, 4, 5), (5, 6, 4), (6,5,8), (4,8,5), (5,8,2)]
# initialization of required result list
result = []
# iterating over the list of tuples
for tup in listTuples :
# temporary variable t to store max element
t = max (tup) #max function to find the max value
result.append(t) # append max value in the list
# printing output
print(result)
#implemantation using function
def listMax(listTup):
# initialization of required result list
result = []
# iterating over the list of tuples
for tup in listTuples :
# temporary variable t to store max element
t = max (tup) #max function to find the max value
result.append(t) # append max value in the list
# returning teh list of max of tuples
return result
# List of tuple
listTuples1 = [(1, 2, 3), (3, 4, 5), (5, 6, 4), (6,5,8), (4,8,5), (5,8,2)]
# invoking listMax function to get list of max of each tuples
listMax1 = listMax(listTuples1)
# Print the list got from the function
print(listMax1)
Get Answers For Free
Most questions answered within 1 hours.