1

我有以下代码生成 0-9 范围内的所有 2 位排列:

p = permutations(range(10), 2)

这会产生这样的结果:

(0, 1); (0, 2); (0, 3); (0, 4); (0, 5); (0, 6); (0, 7); (0, 8); (0, 9); (1, 0); (1, 2); (1, 3); (1, 4); (1, 5); (1, 6); (1, 7); (1, 8); (1, 9); (2, 0); (2, 1); (2, 3); (2, 4); (2, 5); (2, 6); (2, 7); (2, 8); (2, 9); (3, 0); (3, 1); (3, 2); (3, 4); (3, 5); (3, 6); (3, 7); (3, 8); (3, 9); (4, 0); (4, 1); (4, 2); (4, 3); (4, 5); (4, 6); (4, 7); (4, 8); (4, 9); (5, 0); (5, 1); (5, 2); (5, 3); (5, 4); (5, 6); (5, 7); (5, 8); (5, 9); (6, 0); (6, 1); (6, 2); (6, 3); (6, 4); (6, 5); (6, 7); (6, 8); (6, 9); (7, 0); (7, 1); (7, 2); (7, 3); (7, 4); (7, 5); (7, 6); (7, 8); (7, 9); (8, 0); (8, 1); (8, 2); (8, 3); (8, 4); (8, 5); (8, 6); (8, 7); (8, 9); (9, 0); (9, 1); (9, 2); (9, 3); (9, 4); (9, 5); (9, 6); (9, 7); (9, 8)

我应该怎么做才能获得类似的输出[01,02,03....98],然后我可以通过调用 p[0] 来获得某些元素?

4

6 回答 6

2

只需汇总每个结果:

p = permutations(range(10), 2)
result = [str(x[0]) + str(x[1]) for x in p]
于 2013-10-06T18:45:42.287 回答
2

一个(可能不是最有效的)单线:

["".join([str(x) for x in elem]) for elem in p]

或者:

[("%d"*len(p[0]))%(elem) for elem in p]
于 2013-10-06T18:47:05.740 回答
2
In [13]: import itertools as IT
In [17]: p = IT.permutations(range(10), 2)

In [18]: list(IT.starmap('{}{}'.format, p))
Out[18]: 
['01',
 '02',
 '03',
 '04',
...
于 2013-10-06T18:47:19.710 回答
1

正如约翰克莱门茨和我在评论中指出的那样,可以在不进行排列的情况下制作您想要的字符串或数字。

数字 0 到 9 的两个排列仅表示 1 到 98 之间没有任何重复数字的整数。方便的是,所有具有重复数字的 2 位数字都是 11 的倍数,因此您可以轻松地创建一个列表理解或生成器表达式来跳过您不想要的那些:

nums = [i for i in range(1, 99) if i % 11]

如果您希望您的结果是一个包含两个字符的字符串的列表,您可以使用带有格式字符串的format函数,该格式字符串表示要用零填充一位数字:

strings = [format(i, '02') for i in range(1, 99) if i % 11]

不幸的是,将这种技术扩展到更长的数字有点困难,因为仅仅跳过 11 的倍数不会再减少它。最好的方法可能取决于您实际使用所获得的值所做的事情。

于 2013-10-06T22:27:27.000 回答
1

使用列表推导:

p = permutations(range(10), 2)
result = [ "%d%d" % (x[0], x[1]) for x in p ]
于 2013-10-06T18:48:40.097 回答
0

这有效:

>>> from itertools import permutations
>>> ["%i%i" % (a,b) for a,b in permutations(range(10), 2)]
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', 
'31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', 
'47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', 
'62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', 
'78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', 
'93', '94', '95', '96', '97', '98']
>>>

或者,如果您希望它们作为整数,您可以这样做:

>>> from itertools import permutations
>>> [int("%i%i" % (a, b)) for a,b in permutations(range(10), 2)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 
26, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 
49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 
72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 90, 91, 92, 93, 94, 
95, 96, 97, 98]
>>>

但是请注意,Python 不允许您拥有以 0 开头的整数。

于 2013-10-06T18:50:45.293 回答