🎉 बधाई! 🎉
आपने कार्य को शानदार ढंग से हल किया! यह कठिन था, लेकिन आपने सही उत्तर खोजा। आप प्रोग्रामिंग में महारत के करीब हैं! ऐसे ही जारी रखें।
ProgerLib सभी के लिए Python
n & nbsp; और & nbsp; m , जहां & nbsp; n & nbsp; & ndash; ऑर्डर की संख्या (तालिका में पंक्तियाँ), और & nbsp; m & nbsp; & ndash; सामग्री की संख्या (तालिका में कॉलम)। Div> rn n & nbsp; पंक्तियाँ: प्रत्येक पंक्ति में & nbsp; m & nbsp; पूर्णांक होते हैं जो रिक्त स्थान से अलग होते हैं, जो एक ऑर्डर योजना है। प्रत्येक संख्या & ndash; ऑर्डर में विशिष्ट सामग्री की मात्रा। a & nbsp; और & nbsp; b , एक रिक्त स्थान से अलग, जहां & nbsp; a & nbsp; और & nbsp; b & ndash; वे कॉलम जिन्हें आपस में बदलने की आवश्यकता है (कॉलम 0 से क्रमांकित हैं) 3 4
1 2 3 4
5 6 7 8
9 10 11 12
1 2
1 3 2 4
5 7 6 8
9 11 10 12
The min() function is your tool for quickly searching for the smallest element of several values or within a collection.
Syntax
min(iterable, *[, key, default])
min(arg1, arg2, *args[, key])
iterable: An iterable object (for example, a list, tuple, string, set).arg1, arg2, *args: One or more arguments passed directly for comparison.key (optional): A function that is applied to each element before comparison. The minimum will be searched based on the results of this function.default (optional): The value that will be returned if iterable is empty. If default is not specified, an attempt to find the minimum in an empty collection will cause the error ValueError.Usage examples
With a single iterable object:
numbers = [3, 1, 4, 1, 5, 9]
print(min(numbers)) # Output: 1
With multiple arguments:
print(min(3, 1, 4, 1, 5, 9)) # Output: 1
With the key parameter: Searching for an element by any of its properties.
words = ["apple", "banana", "cherry"]
# Finding the word with the shortest length
print(min(words, key=len)) # Output: 'apple'
With the default parameter: Safe handling of potentially empty collections.
empty_list = []
print(min(empty_list, default="empty")) # Output: 'empty'
Useful tips and subtleties:
min(5, "hello") will cause the error TypeError.key works: It is important to understand that key only changes the value for comparison, but returns the original element. In the example min(words, key=len), the lengths (5, 6, 6) are compared, but the word 'apple' itself is returned.lambda functions with key: For complex comparisons, it is convenient to use anonymous lambda functions. For example, to find a dictionary with the minimum value by the key 'age':
people = [{'name': 'John', 'age': 25}, {'name': 'Jane', 'age': 22}]
print(min(people, key=lambda person: person['age'])) # Output: {'name': 'Jane', 'age': 22}
min("a", "b", "A") returns 'A', since capital letters have a smaller code in the symbol table.max()The max() function works similarly to min(), but returns the largest element from the passed arguments or collection.
Syntax
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
The parameters (iterable, arg1, arg2, *args, key, default) have the same value as in the min() function.
Usage examples
With a single iterable object:
numbers = [3, 1, 4, 1, 5, 9]
print(max(numbers)) # Output: 9
With multiple arguments:
print(max(3, 1, 4, 1, 5, 9)) # Output: 9
With the key parameter:
words = ["apple", "banana", "cherry"]
# Finding the word with the longest length
print(max(words, key=len)) # Output: 'banana'
With the default parameter:
empty_list = []
print(max(empty_list, default='N/A')) # Output: 'N/A'
Useful tips and subtleties:
min() are also relevant for max(): avoid mixing types, use lambda functions for complex keys and keep in mind the default parameter for empty collections.max(words, key=len) the words "banana" and "cherry" have the same length of 6, but max returns the first encountered 'banana'.sum()The sum() function is used to find the sum of numeric elements in an iterable object.
Syntax
sum(iterable, start=0)
iterable: An iterable object containing numbers (for example, a list, a tuple, a set).start (optional): The initial value that is added to the total amount. The default value is 0.Usage examples
Sum of list/tuple/set elements:
numbers_list = [1, 2, 3, 4, 5]
print(sum(numbers_list)) # Output: 15
Using the start parameter:
numbers = [1, 2, 3, 4, 5]
# We start summing not from 0, but from 10
total = sum(numbers, 10)
print(total) # Output: 25 (10 + 1 + 2 + 3 + 4 + 5)
Application to two-dimensional arrays (lists of lists)
sum() can be effectively used to process matrices, but with one important caveat.
sum(matrix) will cause the error TypeError, as Python will try to add the initial value 0 with the list [1, 2, 3].# Two-dimensional array
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Sum of all array elements using a generator
total_sum = sum(sum(row) for row in matrix)
print(total_sum) # Output: 45
Useful tips and subtleties:
sum() only works with numeric types (int, float). An attempt to summarize a list of strings (sum(["a", "b"])) will result in a TypeError.True/False: True is interpreted as 1, and False as 0. This can be used to count the true values in the collection.
bools = [True, False, True, True] # 1 + 0 + 1 + 1
print(sum(bools)) # Output: 3
start parameter can be cunningly used to combine lists, although it is less readable and efficient than other methods.
list_of_lists = [[1, 2], [3, 4], [5]]
flat_list = sum(list_of_lists, [])
print(flat_list) # Output: [1, 2, 3, 4, 5]
Warning: This method is not recommended for large lists due to poor performance.
🎉 बधाई! 🎉
आपने कार्य को शानदार ढंग से हल किया! यह कठिन था, लेकिन आपने सही उत्तर खोजा। आप प्रोग्रामिंग में महारत के करीब हैं! ऐसे ही जारी रखें।
AD
विज्ञापन
कोड चल रहा है...