24小时热门版块排行榜    

查看: 487  |  回复: 4
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

额纪

木虫 (初入文坛)

[求助] 数组排列问题

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

» 猜你喜欢

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

活在当下,做好自己
已阅   回复此楼   关注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的回帖
查看全部 5 个回答

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的回帖

额纪

木虫 (初入文坛)

引用回帖:
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的回帖
信息提示
请填处理意见