Python 中的自定义排序

问题描述 投票:0回答:1

在Python中,使用自定义排序,连接数组元素,使得结果将是这些元素中的最大值。

T = int(input("Enter number of test cases: "))

for _ in range(T):
    N = int(input())
    array = list(map(int, input().split()))
    
    array = list(map(str, array))
    array.sort(key=lambda x: (x * 3), reverse=True if '0' in array else False)
    
    largest_num = ''.join(array)
    print(largest_num)

# it is not solving for all test cases.
python sorting custom-sort
1个回答
0
投票
T = int(input("Enter number of test cases: "))

for _ in range(T):
    N = int(input())
    array = list(map(int, input().split()))

    array = list(map(str, array))
    
    
    def custom_sort(x):
    
  
        return x * 3 if '0' not in x else 0
    
    array.sort(key=custom_sort, reverse=True)
    
    largest_num = ''.join(array)
    print(largest_num)
© www.soinside.com 2019 - 2024. All rights reserved.