24小时热门版块排行榜    

Znn3bq.jpeg
查看: 525  |  回复: 4

额纪

木虫 (初入文坛)

[求助] 数组排列问题

例如数组a=[1,2,3,4,5,6]
6个元素,重排列的话应该有720种情况,怎样实现把720种情况都列出来?
用python
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

活在当下,做好自己
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
ben_ladeng: 金币+2, 专家考核, 谢谢指教 2013-04-03 19:18:15
额纪: 金币+20, ★★★★★最佳答案, 原来有现成的呀,谢啦。 2013-04-03 22:59:08
CODE:
>>> from itertools import permutations
>>> a = [1,2,3,4,5,6]
>>> b = [x for x in permutations(a)]
>>> len(b)
720
>>> b[0]
(1, 2, 3, 4, 5, 6)
>>> b[1]
(1, 2, 3, 4, 6, 5)
>>>

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2013-04-03 00:07:23
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

haisan

银虫 (小有名气)

【答案】应助回帖

★ ★
感谢参与,应助指数 +1
ben_ladeng: 金币+2, 谢谢指教 2013-04-03 19:18:21
用“枚举法”。
就是嵌套循环,把每一种情况都列举出来。
教育改变生活,知识改变命运。
3楼2013-04-03 09:01:23
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

额纪

木虫 (初入文坛)

引用回帖:
2楼: Originally posted by libralibra at 2013-04-03 00:07:23
>>> from itertools import permutations
>>> a =
>>> b =
>>> len(b)
720
>>> b
(1, 2, 3, 4, 5, 6)
>>> b
(1, 2, 3, 4, 6, 5)
>>>

再问个问题,怎么知道有这些现成的函数呀?
活在当下,做好自己
4楼2013-04-03 23:00:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

引用回帖:
4楼: Originally posted by 额纪 at 2013-04-03 23:00:13
再问个问题,怎么知道有这些现成的函数呀?...

多看python的帮助文档
对于每一个包,你import之后,可以dir看都有哪些函数在当前导入的包中,help可以看docstring,大概就明白了,例如
CODE:
>>> import itertools
>>> dir(itertools)
['__doc__', '__name__', '__package__', 'chain', 'combinations', 'combinations_with_replacement', 'compress', 'count', 'cycle', 'dropwhile', 'groupby', 'ifilter', 'ifilterfalse', 'imap', 'islice', 'izip', 'izip_longest', 'permutations', 'product', 'repeat', 'starmap', 'takewhile', 'tee']
>>> help(itertools.permutations)
Help on class permutations in module itertools:

class permutations(__builtin__.object)
|  permutations(iterable[, r]) --> permutations object
|  
|  Return successive r-length permutations of elements in the iterable.
|  
|  permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
|  
|  Methods defined here:
|  
|  __getattribute__(...)
|      x.__getattribute__('name') <==> x.name
|  
|  __iter__(...)
|      x.__iter__() <==> iter(x)
|  
|  next(...)
|      x.next() -> the next value, or raise StopIteration
|  
|  ----------------------------------------------------------------------
|  Data and other attributes defined here:
|  
|  __new__ =
|      T.__new__(S, ...) -> a new object with type S, a subtype of T

>>>  

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
5楼2013-04-04 01:39:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 额纪 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 297,工科调剂? +3 河南农业大学-能 2026-04-14 3/150 2026-04-15 22:02 by noqvsozv
[考研] 求调剂 +11 小聂爱学习 2026-04-11 15/750 2026-04-15 21:57 by noqvsozv
[考研] 复试调剂 +21 积极向上; 2026-04-10 23/1150 2026-04-15 12:50 by 西北望—风沙
[考研] 调剂 +12 月@163.com 2026-04-11 12/600 2026-04-14 15:37 by zs92450
[教师之家] 转长聘了 +7 简单化xn 2026-04-13 7/350 2026-04-14 14:50 by xindong
[考研] 机械还有还有名额吗?太难了 +8 笑笑袁 2026-04-10 8/400 2026-04-14 08:44 by screening
[考研] 本科西工大 324求调剂 +5 wysyjs25 2026-04-10 5/250 2026-04-13 23:08 by pies112
[考研] 331求调剂 +5 王国帅 2026-04-11 5/250 2026-04-11 22:56 by 溪涧流水
[考研] 288求调剂 +15 代fish 2026-04-09 16/800 2026-04-11 10:26 by wwj2530616
[考研] 广东省 085601 329分求调剂 +14 Eddieddd 2026-04-10 14/700 2026-04-11 09:58 by bljnqdcc
[考研] 0854调剂 +4 长弓傲 2026-04-09 4/200 2026-04-11 09:18 by 猪会飞
[考研] 337求调剂 +4 研s. 2026-04-10 4/200 2026-04-11 08:57 by zhq0425
[考研] 一志愿北理工298英一数二已上岸,感谢各位老师 +14 Reframe 2026-04-10 16/800 2026-04-10 23:07 by caotw2020
[考研] 调剂 +19 小张ZA 2026-04-10 20/1000 2026-04-10 22:08 by 猪会飞
[考研] 314求调剂 +18 xhhdjdjsjks 2026-04-09 19/950 2026-04-10 18:53 by HPUCZ
[考研] 本9 一志愿西工大085601 324求调剂 +5 wysyjs25 2026-04-10 5/250 2026-04-10 16:57 by luoyongfeng
[考研] 一志愿京区985,085401电子信息,本科电子信息 +3 阳光开朗的男孩 2026-04-10 3/150 2026-04-10 16:29 by sophia_93
[考研] 求调剂 +11 翩翩一书生 2026-04-09 13/650 2026-04-10 10:27 by liuhuiying09
[考研] 一志愿中科大070300化学,314分求调剂 +12 wakeluofu 2026-04-09 12/600 2026-04-10 09:57 by liuhuiying09
[考研] 本科211 工科085400 280分求调剂 可跨专业 +3 LZH(等待调剂中 2026-04-09 3/150 2026-04-09 21:29 by wutongshun
信息提示
请填处理意见