前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >列表内数字组合最大值

列表内数字组合最大值

作者头像
py3study
发布2020-01-17 15:57:39
8100
发布2020-01-17 15:57:39
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
第一种
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)
代码语言:javascript
复制
第三种
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)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档