You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python排序报错:TypeError(需整数却传字符串)及排序逻辑求助

Fixing TypeError in sort() and Slice Issues in Your Python Code

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=[]*0 are unnecessary—just use a = [] (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, when l == len(arr)-1, arr[l:] already gives you a list with the last element, so you can replace the else branch with b = arr[l:] instead of b.append(arr[l]).
  • Avoid index out-of-bounds errors: If a or b is empty (e.g., when checking the first element of the list), accessing a[0] will throw an error. We need to handle these cases:
    • If a is empty, it means there are no elements to the left of arr[i], so the condition arr[i] > a[0] is automatically true.
    • If b is empty, it means there are no elements to the right, so b[0] < arr[i] is automatically true.

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" with reverse=True to fix the TypeError
  • Fixed the left slice from arr[-1:k] to arr[:k] to get the correct elements
  • Standardized list assignments (always using list literals or slices) to avoid type inconsistencies
  • Added checks for empty a/b to prevent index errors
  • Removed redundant print statements (you can add them back for debugging if needed)

内容的提问来源于stack exchange,提问作者user10598529

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:14:19