输入整型数组和排序标识,对其元素按照升序或降序进行排序

135 阅读1分钟

1. 题目

www.nowcoder.com/practice/dd…

2. 考点

数组的排序 使用sorted(list, key=lambda x: int(x), reverse=bool(sort_flag))

3. 核心代码

def test(count):
    inputs = input()
    lis = inputs.split(' ')
    sort_flag = int(input())
    return ' '.join(sorted(lis, key=lambda x: int(x), reverse=bool(sort_flag)))


if __name__ == '__main__':
    try:
        print(test(int(input())))
    except EOFError:
        pass

4. 疑惑点

为何需要三个参数 是我没有搞懂的 明明第二行限制了输入数字 按照n个 并且这里保证了n和第一个参数一致 那么第一个参数就没有使用的情况