24小时热门版块排行榜    

查看: 521  |  回复: 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的回帖
相关版块跳转 我要订阅楼主 额纪 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 306求0703调剂一志愿华中师范 +7 纸鱼ly 2026-03-21 8/400 2026-03-23 23:31 by chixmc
[考研] 291求调剂 +8 hhhhxn.. 2026-03-23 8/400 2026-03-23 23:15 by peike
[考研] 335分 | 材料与化工专硕 | GPA 4.07 | 有科研经历 +4 cccchenso 2026-03-23 4/200 2026-03-23 23:00 by 徐ckkk
[基金申请] 请教下大家 2026年国家基金申请是双盲审吗? +3 lishucheng1 2026-03-22 4/200 2026-03-23 20:48 by god_tian
[考研] 303求调剂 +4 元夕元 2026-03-20 4/200 2026-03-23 19:00 by macy2011
[考研] 考研化学308分求调剂 +7 你好明天你好 2026-03-23 8/400 2026-03-23 18:39 by macy2011
[考研] 328求调剂,英语六级551,有科研经历 +7 生物工程调剂 2026-03-17 12/600 2026-03-23 18:18 by YMU施老师
[考研] 263求调剂 +6 yqdszhdap- 2026-03-22 9/450 2026-03-23 12:57 by yqdszhdap-
[考研] 323求调剂 +6 洼小桶 2026-03-18 6/300 2026-03-23 00:29 by king123!
[考研] 08工科 320总分 求调剂 +11 梨花珞晚风 2026-03-17 11/550 2026-03-22 17:42 by luoyongfeng
[考研] 一志愿 西北大学 ,070300化学学硕,总分287,双非一本,求调剂。 +3 晨昏线与星海 2026-03-20 3/150 2026-03-22 16:00 by ColorlessPI
[考研] 269专硕求调剂 +6 金恩贝 2026-03-21 6/300 2026-03-22 14:31 by ColorlessPI
[考研] 0856材料专硕353求调剂 +4 NIFFFfff 2026-03-20 4/200 2026-03-22 09:49 by 2026paper
[考研] 初试 317 +7 半拉月丙 2026-03-20 7/350 2026-03-21 22:26 by peike
[考研] 一志愿南大,0703化学,分数336,求调剂 +3 收到VS 2026-03-21 3/150 2026-03-21 18:42 by 学员8dgXkO
[考研] 311求调剂 +3 勇敢的小吴 2026-03-20 3/150 2026-03-21 17:40 by ColorlessPI
[考研] 336求调剂 +5 rmc8866 2026-03-21 5/250 2026-03-21 17:24 by 学员8dgXkO
[考研] 279求调剂 +5 红衣隐官 2026-03-21 5/250 2026-03-21 14:59 by lature00
[考研] 296求调剂 +6 www_q 2026-03-18 10/500 2026-03-20 23:56 by JourneyLucky
[考研] 材料与化工 322求调剂 +4 然11 2026-03-19 4/200 2026-03-20 22:12 by luoyongfeng
信息提示
请填处理意见