8

让我澄清一下:

获得两个数字之间所有唯一数字的每个数字的最快方法是什么。例如,10,000 和 100,000。

一些明显的数字是 12,345 或 23,456。我正试图找到一种方法来收集所有这些。

for i in xrange(LOW, HIGH):
  str_i = str(i)
  ...?
4

5 回答 5

15

使用itertools.permutations

from itertools import permutations

result = [
    a * 10000 + b * 1000 + c * 100 + d * 10 + e
    for a, b, c, d, e in permutations(range(10), 5)
    if a != 0
]

我使用了以下事实:

  • 之间的数字100001000005 位或 6 位数字,但这里只有 6 位数字没有唯一数字,
  • itertools.permutations以给定的长度创建所有组合,所有排序(因此两者都1234554321出现在结果中),
  • 您可以直接对整数序列进行排列(因此没有转换类型的开销),

编辑

感谢您接受我的回答,但这是其他人的数据,比较提到的结果:

>>> from timeit import timeit
>>> stmt1 = '''
a = []
for i in xrange(10000, 100000):
    s = str(i)
    if len(set(s)) == len(s):
        a.append(s)
'''
>>> stmt2 = '''
result = [
    int(''.join(digits))
    for digits in permutations('0123456789', 5)
    if digits[0] != '0'
]
'''
>>> setup2 = 'from itertools import permutations'
>>> stmt3 = '''
result = [
    x for x in xrange(10000, 100000)
    if len(set(str(x))) == len(str(x))
]
'''
>>> stmt4 = '''
result = [
    a * 10000 + b * 1000 + c * 100 + d * 10 + e
    for a, b, c, d, e in permutations(range(10), 5)
    if a != 0
]
'''
>>> setup4 = setup2
>>> timeit(stmt1, number=100)
7.955858945846558
>>> timeit(stmt2, setup2, number=100)
1.879319190979004
>>> timeit(stmt3, number=100)
8.599710941314697
>>> timeit(stmt4, setup4, number=100)
0.7493319511413574

所以,总结一下:

  • 解决方案编号 17.96 s
  • 解决方案编号 2(我原来的解决方案)拿了1.88 s
  • 解决方案编号 3拿了8.6 s
  • 解决方案编号 4(我更新的解决方案)拿了0.75 s

最后一个解决方案看起来比其他解决方案快 10 倍。

注意:我的解决方案有一些我没有测量的导入。我假设您的导入将发生一次,并且代码将被执行多次。如果不是这种情况,请根据您的需要调整测试。

编辑#2:我添加了另一个解决方案,因为甚至不需要对字符串进行操作 - 它可以通过实整数的排列来实现。我敢打赌,这可以加快速度。

于 2013-09-11T03:53:33.730 回答
7

便宜的方法:

for i in xrange(LOW, HIGH):
    s = str(i)
    if len(set(s)) == len(s):
        # number has unique digits

这使用 aset来收集唯一数字,然后检查是否有与数字总数一样多的唯一数字。

于 2013-09-11T03:44:48.337 回答
4

列表理解在这里会起作用(从 nneonneo 窃取的逻辑):

[x for x in xrange(LOW,HIGH) if len(set(str(x)))==len(str(x))]

对于那些好奇的人来说,这是一个时间:

> python -m timeit '[x for x in xrange(10000,100000) if len(set(str(x)))==len(str(x))]'
10 loops, best of 3: 101 msec per loop
于 2013-09-11T03:57:51.607 回答
2

Here is an answer from scratch:

def permute(L, max_len):
    allowed = L[:]
    results, seq = [], range(max_len)
    def helper(d):
        if d==0:
            results.append(''.join(seq))
        else:
            for i in xrange(len(L)):
                if allowed[i]:
                    allowed[i]=False
                    seq[d-1]=L[i]
                    helper(d-1)
                    allowed[i]=True
    helper(max_len)
    return results

A = permute(list("1234567890"), 5)
print A
print len(A)
print all(map(lambda a: len(set(a))==len(a), A))

It perhaps could be further optimized by using an interval representation of the allowed elements, although for n=10, I'm not sure it will make a difference. I could also transform the recursion into a loop, but in this form it is more elegant and clear.

Edit: Here are the timings of the various solutions

  • 2.75808000565 (My solution)
  • 8.22729802132 (Sol 1)
  • 1.97218298912 (Sol 2)
  • 9.659760952 (Sol 3)
  • 0.841020822525 (Sol 4)
于 2013-09-11T15:29:13.387 回答
0
no_list=['115432', '555555', '1234567', '5467899', '3456789', '987654', '444444']
rep_list=[]
nonrep_list=[]
for no in no_list:
    u=[]
    for digit in no:
        # print(digit)
        if digit not in u:
            u.append(digit)
    # print(u)

    #iF REPEAT IS THERE
    if len(no) != len(u):
        # print(no)
        rep_list.append(no)
    #If repeatation is not there
    else:
        nonrep_list.append(no)
print('Numbers which have no repeatation are=',rep_list)
print('Numbers which have repeatation are=',nonrep_list)
于 2020-06-04T16:33:22.647 回答