data = ['10', '0', '1915', '387', '1933', '402']
# Convert strings to int
result = [int(i) for i in data]
# sorted works with numeric values.
# This uses tuple unpacking, even though sorted returns a list
result[2], result[4] = sorted([result[2], result[4]], reverse=True)
result[3], result[5] = sorted([result[3], result[5]], reverse=True)
# cast ints back to str as per spec
result = [str(i) for i in result]
这是输出:
>>> data = ['10', '0', '1915', '387', '1933', '402']
...
... # Convert strings to int
... result = [int(i) for i in data]
...
... # sorted works with numeric values.
... # This uses tuple unpacking, even though sorted returns a list
... result[2], result[4] = sorted([result[2], result[4]], reverse=True)
... result[3], result[5] = sorted([result[3], result[5]], reverse=True)
...
... # cast ints back to str as per spec
... result = [str(i) for i in result]
>>> result
['10', '0', '1933', '402', '1915', '387']
像这样:
这是输出:
这是另一个,只是为了有选择:
相关问题 更多 >
编程相关推荐