Python排序报错:TypeError(需整数却传字符串)及排序逻辑求助
Let's break down and fix the issues in your code step by step:
1. The Immediate TypeError: Wrong reverse Argument Type
The error you're seeing (TypeError: an integer is required (got type str)) comes directly from these lines:
a.sort(reverse="True") b.sort(reverse="True")
Python's list.sort() method expects the reverse parameter to be a boolean value (True or False), not a string "True". Passing a string here confuses the method, hence the error. Just remove the quotes around True to fix this:
a.sort(reverse=True) b.sort(reverse=True)
2. Fixing Broken Slice Logic
Your slice for the left elements (arr[-1:k]) is incorrect and will often return an empty list—this leads to further issues when you try to access a[0] later.
Why the slice fails:
arr[-1:k] tries to slice from the last element (index -1) to index k, but Python slices are left-to-right by default (step=1). If k < len(arr)-1, this range is invalid and returns an empty list. For example, if arr = [1,3,5,7] and k=1, arr[-1:1] would try to slice from index 3 to 1—no elements fit this, so you get [].
Correct slice for left elements:
If you want all elements before index k, use arr[:k] (this slices from the start of the list up to, but not including, index k):
a = arr[:k]
3. Other Cleanup & Edge Case Fixes
- Redundant list initialization: Lines like
a=[]*0are unnecessary—just usea = [](it's identical and cleaner). - Inconsistent list handling: In your else branches, you use
append()but in the if branches you assign sliced lists. This can lead to unexpected behavior. For example, whenl == len(arr)-1,arr[l:]already gives you a list with the last element, so you can replace the else branch withb = arr[l:]instead ofb.append(arr[l]). - Avoid index out-of-bounds errors: If
aorbis empty (e.g., when checking the first element of the list), accessinga[0]will throw an error. We need to handle these cases:- If
ais empty, it means there are no elements to the left ofarr[i], so the conditionarr[i] > a[0]is automatically true. - If
bis empty, it means there are no elements to the right, sob[0] < arr[i]is automatically true.
- If
Modified Working Code
Here's the fixed version of your code with all these changes:
def ele(arr): m = 0 for i in range(1, len(arr)-1): k = i - 1 l = i + 1 a = [] b = [] if k != 0: a = arr[:k] a = list(map(int, a)) a.sort(reverse=True) else: a = [arr[k]] # Wrap in list to keep consistent type if l != len(arr)-1: b = arr[l:] b = list(map(int, b)) b.sort(reverse=True) else: b = [arr[l]] # Handle empty a or b cases left_condition = True if not a else arr[i] > a[0] right_condition = True if not b else arr[i] > b[0] if right_condition and left_condition: m = 1 print(arr[i]) break if m == 0: print(-1) t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) ele(arr)
Key Changes Recap:
- Replaced
reverse="True"withreverse=Trueto fix the TypeError - Fixed the left slice from
arr[-1:k]toarr[:k]to get the correct elements - Standardized list assignments (always using list literals or slices) to avoid type inconsistencies
- Added checks for empty
a/bto prevent index errors - Removed redundant
printstatements (you can add them back for debugging if needed)
内容的提问来源于stack exchange,提问作者user10598529

