简体   繁体   English

如何串联从python permutations()返回的数字?

[英]how to concatenate the numbers returned from python permutations()?

I have the following code that generates all 2-digit permutations in the range 0-9: 我有以下代码生成范围为0-9的所有2位数字排列:

p = permutations(range(10), 2)

Which produces a result like this: 产生这样的结果:

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

What should I do to get an output like [01,02,03....98] and then I can get certain element by calling p[0]? 我应该怎么做才能得到类似[01,02,03....98]的输出,然后可以通过调用p [0]获得某些元素?

Just aggregate each result: 只需汇总每个结果:

p = permutations(range(10), 2)
result = [str(x[0]) + str(x[1]) for x in p]

A (probably not most efficient) one-liner: 一个(可能不是最有效的)单线:

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

Or: 要么:

[("%d"*len(p[0]))%(elem) for elem in p]
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',
...

Use a list comprehension: 使用列表理解:

p = permutations(range(10), 2)
result = [ "%d%d" % (x[0], x[1]) for x in p ]

As John Clements and I pointed out in comments, its possible to make the strings or numbers you want without doing a permutation. 正如我和约翰·克莱门茨(John Clements)在评论中指出的那样,无需进行排列即可制作所需的字符串或数字。

Two-permutations of digits 0 to 9 simply represent integers from 1 to 98 that don't have any repeated digits. 数字0到9的两个置换仅表示1到98的整数,没有任何重复的数字。 Conveniently, all 2-digit numbers with repeated digits are multiples of 11, so you can easily make a list comprehension or generator expression that skips over the ones you don't want: 方便地,所有具有重复数字的2位数字都是11的倍数,因此您可以轻松地使列表理解或生成器表达式跳过不需要的数字:

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

If you want your result to be a list of two-character strings, you use the format function with a format string that says to to pad one-digit numbers with a zero: 如果希望结果是两个字符的字符串列表,则可以使用带格式字符串的format函数,该字符串表示要用零填充一位数字:

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

Unfortunately, it's a bit harder to extend this technique to longer numbers, since just skipping multiples of 11 won't cut it any more. 不幸的是,将这种技术扩展到更长的数字会有点困难,因为仅跳过11的倍数将不再削减它。 What the best approach would be will probably depend on what you're actually doing with the values you get. 最好的方法将取决于您实际使用的值。

This works: 这有效:

>>> 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']
>>>

Or, if you want them as integers, you can do this: 或者,如果您希望它们为整数,则可以执行以下操作:

>>> 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]
>>>

Note however that Python does not allow you to have an integer that starts with 0. 但是请注意,Python不允许您使用以0开头的整数。

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

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