简体   繁体   English

Python订购数字组合?

[英]Python ordered combination of numbers?

I am looking to write something that will display an ordered list of all possible combinations. 我正在寻找可以显示所有可能组合的有序列表的内容。 It can start at something like 它可以开始于

11-AA 11-AB 11-AC 11-AA 11-AB 11-AC

...and eventually end at... ...最终以...结束

59-YZ 59-ZZ 59-YZ 59-ZZ

I would also like it if the first part of the number can only be between 1-5. 我也希望数字的第一部分只能在1-5之间。 I have been working with itertools, but I am having a hard time generating this specific sort of thing. 我一直在使用itertools,但在生成这种特定类型的东西时遇到了困难。 I can generate all possible combinations of 4 letters repeated twice, but I'm having issues getting the dash in, and telling the program that "Hey, you can only chose between 1 and 4 for the first number". 我可以生成两次重复的4个字母的所有可能组合,但是在输入破折号时遇到了问题,并告诉程序“嘿,您只能为第一个数字选择1到4之间”。

import itertools

perms = itertools.product('ABCD', repeat=2)

for perm in perms:
    print('The possible combinations are', perm)

There is the code for that. 有相应的代码。 This doesn't HAVE to use itertools, that is just what I was familar with. 不必使用itertools,这正是我所熟悉的。

EDIT: The first answer was precisely what I needed. 编辑:第一个答案正是我需要的。 I didn't like what the itertools was doing because of the commas, but the little snippet you showed me is magic. 由于逗号,我不喜欢itertools的工作,但是您向我展示的小片段很神奇。 Thank you. 谢谢。

To answer the question, a loop just seemed less clean? 为了回答这个问题,循环看起来不太干净? I also just discovered the itertools, and am probably enjoying it too much. 我也刚刚发现了itertools,并且可能非常喜欢它。

Just do this in equal pieces. 做到相等。 One character has to be 1 , 2 , 3 , 4 , or 5 ; 一个字符必须是1234 ,或5 ; the second can be any digit; 第二个可以是任何数字; the third must be - , the last two must be A , B , C , or D , so: 第三个必须是- ,最后两个必须是ABCD ,所以:

perms = itertools.product('12345', '0123456789', '-', 'ABCD', 'ABCD')

And then, to join them up into strings: 然后,将它们组合成字符串:

print('The possible combinations are:')
for perm in perms:
    print(''.join(perm))

This starts with 10-AA , not 11-AA . 这从10-AA开始,而不是11-AA There's no way to end with 59 if you start with 11 (assuming you want the numbers in order). 如果以11开头,则无法以59结尾(假设您希望数字按顺序排列)。


I originally left the - out and joined up each string with format , but I think this is slower. 我最初将-省略,并使用format将每个字符串连接起来,但是我认为这比较慢。 It's also at least twice as fast on every Python version I have handy. 在我使用的每个Python版本上,它的速度至少也要快两倍。 (The cost of a 1-length loop in C is trivial, as is building a 5-tuple vs. a 4-tuple; the cost of format vs. join is not.) (C语言中的1个长度的循环的开销是微不足道的,构建5个元组和4个元组的开销是微不足道的;而formatjoin的开销则不是。)

You could also product up a range(11, 60) and a product('ABCD', repeat=2) , then flatten it with chain , then format each one. 您也可以product一个range(11, 60)和一个product('ABCD', repeat=2) ,然后用chain展平,然后format每个。 While that's conceptually simpler, it's going to be a lot harder to read, and is about as slow as the flat format version. 虽然这是概念比较简单,它要困难得多阅读,大约是平慢format版本。

Anyway, in 64-bit python.org 3.3.2 on OS X 10.9: 无论如何,在OS X 10.9上的64位python.org 3.3.2中:

In [1189]: %timeit collections.deque((''.join(perm) for perm in itertools.product('1234', '1234567890', '-', 'ABCD', 'ABCD')), maxlen=0)
10000 loops, best of 3: 129 µs per loop

In [1190]: %timeit collections.deque(('{}{}-{}{}'.format(*perm) for perm in itertools.product('1234', '1234567890', 'ABCD', 'ABCD')), maxlen=0)
1000 loops, best of 3: 359 µs per loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM