Re: [问题] 排列组合问题或逻辑

楼主: rexyeah (ccccccc)   2017-10-18 11:11:48
import itertools
# repeat means how many elements you want in this combination.
for args in itertools.product(range(10), repeat=3):
print list(args)
EDIT:
# 如果你跟我一样是一行控的话...
print map(lambda x: list(x), itertools.product(range(10), repeat=3))
※ 引述《aegisqoo (flymetomoon)》之铭言:
: 大家好,我新手也有爬文,有个问题
: 假设 a=[x,y,z] 且 0 <= x,y,z <= 9 的 x.y.z组合
: 如下:
: for x in range(10):
: for y in range(10):
: for z in range(10):
: a=[x,y,z]
: print(a)
: 那问题是,有办法更简短吗?,假设今天a=[b,c,d,e,...z]
: 整个for循环就打很长,在网络上有看到指令
: itertools中permutations可以直接排列,但想知道假设不用
: 这个指令,且只能用一般循环指令来写,有办法更简单吗?
: 或者是有人可以解释itertools.permutations的原始代码逻辑吗?
: 谢谢大大
作者: bibo9901 (function(){})()   2017-10-18 11:29:00
你要是一行控的话应该是 map(list, ...)
楼主: rexyeah (ccccccc)   2017-10-18 11:33:00
ah, that's better.
作者: uranusjr (←這人是超級笨蛋)   2017-10-18 15:42:00
lambda x: list(x) -> 脱裤子放屁, 直接写 list 就好
楼主: rexyeah (ccccccc)   2017-10-18 15:58:00
谢谢指教lambda顺手了,下次换你用嘴巴放表演一下

Links booklink

Contact Us: admin [ a t ] ucptt.com