广告
返回顶部
首页 > 资讯 > 后端开发 > Python >列表内数字组合最大值
  • 466
分享到

列表内数字组合最大值

组合最大值数字 2023-01-31 00:01:04 466人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

第一种import itertoolslt = [4, 40, 45, 6, 9, 3, 5, 2, 8]lt2 = map(str, lt)it = itertools.permutations(lt2,len(lt))# for i


第一种
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)


--结束END--

本文标题: 列表内数字组合最大值

本文链接: https://www.lsjlt.com/news/181484.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作