列表内数字组合最大值


第一种
import itertools
lt = [4, 40, 45, 6, 9, 3, 5, 2, 8]
lt2 = map(str, lt)
it = itertools.permutations(lt2,len(lt))
# for i in it:
# print(i)
m = map(lambda x:''.join(x), it)
# for i in m:
# print(i)
print(max(m))
# print(m, type(m))

第二种
lt = [4, 40, 45, 6, 9, 3, 5, 2, 8]
n = len(lt)
for i in range(n-1):
for j in range(n-1-i):
if str(lt[j])+str(lt[j+1]) < str(lt[j+1])+str(lt[j]):
lt[j],lt[j+1] = lt[j+1],lt[j]
lt2 = list(map(str, lt))
m = ''.join(lt2)
print(m)

第三种
from functools import cmp_to_key lt = [4, 40, 45, 6, 9, 3, 5, 2, 8] lt.sort(key=cmp_to_key(lambda x, y: int(str(x)+str(y))-int(str(y)+str(x))), reverse=True) print(lt)


猜你喜欢

转载自www.cnblogs.com/wenjiangtao/p/10572968.html