【问题标题】:Generate a list of random string of fixed length in python [duplicate]在python中生成一个固定长度的随机字符串列表[重复]
【发布时间】:2016-04-01 19:09:25
【问题描述】:

我需要生成一个列表,其中包含固定长度的随机字母数字字符串。它将类似于:list = ['ag5','5b9','c85'] 我可以使用随机数制作列表,但我无法制作同时包含数字和字母的字符串。列表将具有固定长度(例如 100 项)。我正在使用 python 3。

【问题讨论】:

  • 列表中每个元素的大小是多少?
  • 尝试谷歌搜索“生成随机字母数字字符串 python。”
  • @intboolstring...不是悄悄地...OP想要混合字母数字字符...
  • 代码是否应该是唯一的?
  • 请先google。你试过什么?

标签: python list python-3.x random


【解决方案1】:

要获得一组字母数字字符,您可以组合string 模块中的常量。然后您可以在列表推导中使用random.choice() 来生成列表:

from random import choice
from string import ascii_lowercase, digits

chars = ascii_lowercase + digits
lst = [''.join(choice(chars) for _ in range(2)) for _ in range(100)]
print(lst)

【讨论】:

    【解决方案2】:

    我想,你想要像下面这样的东西:

    >>> import string
    >>> import random
    >>> L1 = []
    >>> my_chars = string.ascii_lowercase + string.digits #list of alphanumeric characters
    >>> while(len(L1)<100): #To keep iterating until len(L1)=100
        s = random.sample(my_chars,3) #Pick a sample(list) of three characters len from chars list
        if not all(t in string.digits for t in s) and not all(t in string.ascii_lowercase for t in s): #Check if these characters are neither only digits nor letters, only mix of both will be allowed
            L1.append(''.join(s)) #concatenate the characters to form the string and add it to list L1
    
    
    >>> len(L1)
    100
    >>> L1
    ['f3m', 'gw2', '9ua', 'm4r', 'fv5', 'jw1', 'd1b', 'lh1', 'i61', '53m', 'j6y', 'fg6', '90d', 'xz1', 'n9f', 'k6r', '31b', 'm8w', '8w1', 'h5q', 'h3d', 'ju2', 'q1i', '6ci', '07m', '40c', 's0h', 'q1p', 'u2o', 'r4g', '6gq', 'rj4', '08x', 'yr6', 'il7', '21w', 'v3q', 'kv9', 'e4i', 'g3o', 'r2p', 'nl7', 'k8h', 'by9', 'qd1', 't71', 'x8f', 'uq3', 'k2z', '5i7', '7pc', 'd68', '6n0', '81y', 'c34', 'la0', 'a0c', '1d9', '7oi', 'z7x', '8l9', '0te', 'e9b', '2yp', '17h', 'vm1', 'vm1', 'ow9', '1ma', 'y7q', '7wa', 'a6b', '9uo', '5t2', 'i40', 'ja1', '16v', '0fe', '6bc', 'ek3', 'th6', '26g', 'a9n', 'fo5', '3hg', '4wz', 'v6z', 'r7b', '9cr', 'a0s', '8yp', 'v0f', 'es4', '8do', 'd0e', 'o6z', 'x3q', 'qw3', 'gi0', '0eg']
    

    【讨论】:

    • 是的,它更像我想要的那样工作。我现在已经添加了 ascii_uppercase 。谢谢。
    • 刚刚发现这段代码有问题。该列表应该有 100 个字母数字字符串作为列表元素。但是每次我运行它时,列表长度都是随机变化的,主要在 50 到 70 之间。我的意思是有时列表包含 53 个项目,有时包含 68 个左右。 i in range(100) 应该生成 100 个字符串,但我不明白为什么它没有发生。你能帮忙吗?
    • @Aman...是的,我已经更新了答案。实际上它应该是一个while 循环...
    • 为什么要投反对票?..请评论..
    【解决方案3】:

    由于您需要每个字符串都包含字母和数字,因此我们需要比平时多做一些工作。我们可以继续生成随机单词,只接受满足我们约束的单词,或者我们可以通过构造来构建满足约束的字符集合。

    from random import choice, shuffle
    from string import digits, ascii_lowercase
    
    def gen_word(N, min_N_digits, min_N_lower):
        choose_from = [digits]*min_N_dig + [ascii_lowercase]*min_N_low
        choose_from.extend([digits + ascii_lowercase] * (N-min_N_low-min_N_dig))
        chars = [choice(bet) for bet in choose_from]
        shuffle(chars)
        return ''.join(chars)
    
    def gen_word_rejection(N, min_N_digits, min_N_lower):
        bet = digits + ascii_lowercase
        while True:
            word = ''.join([choice(bet) for i in range(N)])
            if (sum(c.isdigit() for c in word) >= min_N_digits and
                sum(c.islower() for c in word) >= min_N_lower):
                return word
    

    这给了我

    >>> [gen_word(3, 1, 1) for i in range(5)]
    ['mb6', 'g4b', 'y5g', '28p', 'ki2']
    >>> [gen_word_rejection(3, 1, 1) for i in range(5)]
    ['y37', 'dr0', 'w1z', 'h2a', 'i6r']
    

    所有这些都至少有一个数字和至少一个小写字母。

    【讨论】:

      【解决方案4】:

      如果您愿意,我会推荐至少三个长度的 hashids 和自定义盐:

      codes = [hasher.encode(i) for i in range(100)] # assuming hasher = hashids.Hashids('', 3)
      

      【讨论】:

      • 因为这是一种散列技术,如果继续使用相同的盐,生成的散列码不会很随机。我强烈建议使用随机生成的盐。
      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      相关资源
      最近更新 更多