【问题标题】:How to iterate through alpha and numeric numbers如何遍历字母和数字
【发布时间】:2012-04-02 14:48:01
【问题描述】:

我想知道如何在 Python 中遍历一组条件。

  1. 包含 2-6 个小写字母或数字字符的字符串
  2. 第一个字符始终是数字

所以一个简短的进展将是:

1a
1b
1c
...
1aa
1ab
1ac
...
2aaa
2aab
2aac

etc.

一个可以做到前两个的可怕例子是

##Loop through 1a-z0-9
start = '1'
l = 97
while l < 123:
    num = start
    num += chr(l)
    print num
    l += 1

l = 48
while l < 58:
    num = start
    num += chr(l)
    print num
    l += 1

我找到了 itertools,但找不到很好的例子。

【问题讨论】:

    标签: python numbers loops itertools letters


    【解决方案1】:

    让我们使用广度优先搜索类型算法来做到这一点

    starting from 
    Root:
        have 10 children, i = 0,1,...,9
        so , this root must have an iterator, 'i'
        therefore this outermost loop will iterate 'i' from 0 to 9
    
    i:
        for each 'i', there are 5 children (ix , ixx, ixxx, ixxxx, ixxxxx)
        ( number of chars at the string )
        so each i should have its own iterator 'j' representing number of chars
        the loop inside Root's loop will iterate 'j' from 1 to 5
    
    j:
        'j' will have 'j' number of children ( 1 -> x , 2 -> xx ,..., 5-> xxxxx)
        so each j will have its own iterator 'k' representing each "character"
        so, 'k' will be iterated inside this j loop, from 1 to j
        ( i=2, j=4, k = 3 will focus on 'A' at string  "2xxAx" )
    
    k:
        each 'k' represents a character, so it iterates from 'a' to 'z'
        each k should have a iterator(value) 'c' that iterates from 'a' to 'z' (or 97 to 122)
    

    我认为这比我之前想向你展示的更有意义。 :) 如果你不明白请告诉我..顺便说一句,这是一个有趣的问题:)

    【讨论】:

    • 我想遍历它,并将它放在一个列表中。我正在尝试浏览 URL 的所有组合。所以它会是site.com/place/1a 然后是site.com/place/1ab 等等。所以我正在尝试构建 URL 的最后一部分。
    • 所以,你想通过名为“”的每一个可能的链接进行暴力破解(贪婪搜索)。仪式?好的,我将编辑我的答案。
    【解决方案2】:

    我会使用 itertools 的产品功能。

    import itertools 
    digits = '0123456789'
    alphanum = 'abcdef...z' + digits # this should contain all the letters and digits
    
    for i in xrange(1, 6):    
        for tok in itertools.product(digits, itertools.product(alphanum, repeat=i)):
            # do whatever you want with this token `tok` here.
    

    【讨论】:

    • hmm.. 将permutations(letters, 5 ) 包含重复的字母,如aaaaa,并且绝对不会包含长度小于5 的字母数字,如@9​​87654324@
    • 我错过了可变长度。现在看起来好多了:) 我认为我们可以在这里重复字母。
    • 我不确定重复的字母,list(itertools.permutations( 'ab', 2 ))为我返回[('a', 'b'), ('b', 'a')],其中不包括aabb
    • permutations 不是你需要的。使用 productrepeat 给定的可选参数。
    【解决方案3】:

    您可以使用itertools.productitertools.chain 执行此操作。首先定义数字和字母的字符串:

    numbers = '0123456789'
    alnum = numbers + 'abcdefghijklmnopqrstuvwxyz'
    

    使用itertools.product,可以得到不同长度字符串的元组:

    len2 = itertools.product(numbers, alnum) # length 2
    len3 = itertools.product(numbers, alnum, alnum) # length 3
    ...
    

    将所有长度的迭代器链接在一起,将元组连接成字符串。我会用列表理解来做:

    [''.join(p) for p in itertools.chain(len2, len3, len4, len5, len6)]
    

    【讨论】:

    • 如果字母是['a','b','c'],你可以做以下一行,哈哈[ ''.join(p) for p in chain( *[ product( numbers, *letters * times )for times in xrange(2, 6 + 1 ) ] )
    • 但是,不要使用那个班轮而是使用其中的概念,即*拆包操作员
    • 是的,它做到了!非常感谢!
    【解决方案4】:

    您可以考虑以 26 为基数的这个问题(忽略第一个数字,我们将把它放在一个单独的案例中。)因此,我们希望以 26 为基数的字母范围从 'a' 到 'zzzzz' 将是0 和 (26,26,26,26,26) = 26 ^ 0 + 26 + 26^2 + 26^3 + 26^4 + 26^5。所以现在我们有一个从数字到字母的双射,我们只想写一个函数把我们从一个数字变成一个单词

     letters = 'abcdef..z'
    
     def num_to_word( num ):
          res = ''
          while num:
               res += letters[num%26]
               num //= 26
          return res
    

    现在编写枚举 this 的函数

     def generator():
         for num in xrange(10):
             for letter_num in xrange( sum( 26 ** i for i in xrange( 6 ) ) + 1 ):
                 tok = str(num) + num_to_word( letter_num )
                 yield tok
    

    【讨论】:

    • 不要使用+= 构建字符串,它很慢。而是建立一个子字符串列表并join 它们。
    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2017-10-10
    • 2013-05-23
    • 1970-01-01
    • 2022-11-14
    • 2015-05-14
    • 2021-09-05
    相关资源
    最近更新 更多