【问题标题】:Generate format following strings - random order生成以下字符串的格式 - 随机顺序
【发布时间】:2021-01-06 06:41:16
【问题描述】:

手头的问题

目前,出于与问题无关的原因,我正在努力制作代码生成器。

代码遵循00|letter|8string mix|letter的格式

预期的最终结果示例如下:

00b06c1161bc 00aee797645b

00c435ab439e 00da494a229a

中间 8 节字符串的快速分解导致最多需要两个字母字符和 6 个可以随机排列的数字。

虽然我对此有困难,但接受的有限信件成为附加问题。这些是字母a,b,c,d,e,和f

我已经为生成器制作了一个列表 (acceptedChars=["a","b","c","d","e","f"]) 以供从中提取,但是如何允许它按照我不确定如何拉出的要求将其用于生成。

任何关于此的信息都会很棒,如果您有任何问题,请发表评论,我一定会回复。

【问题讨论】:

  • 当您说最多 2 个字母字符时,这是否意味着在某些情况下也可能有 0 个字母字符?
  • 你好 @SayanipDutta ,是的,这确实意味着 0 Alpha Characters 是可能的。

标签: python python-3.x random


【解决方案1】:

这里是使用随机函数的完整代码实现。

此代码将为您生成 100 个随机 12 字符代码。

下面的代码也解决了requirement of a maximum of two alpha-characters and 6 numbers that can be in random order

import random

acceptedChars = list('abcdef')
acceptedDigit = list('0123456789')

for i in range(100):
    secretCode = '00' + random.choice(acceptedChars)
    charCount = digitCount = 0

    pos1 = random.randint(1,8)
    pos2 = pos1
    while pos2 == pos1: pos2 = random.randint(1,8)

    for i in range(1,9):
        if i in (pos1,pos2):
            secretCode += random.choice(acceptedChars)
        else:
            secretCode += random.choice(acceptedDigit)

    secretCode += random.choice(acceptedChars)

    print (secretCode)

随机码的示例输出(生成 10 个):

00e89642be3c
00ba75d2130e
00b56c9b906b
00da9294e87c
00b3664ce97f
00c4b6681a3e
00e6699f75cf
00d369d07a0a
00ce653a228f
00d5665f95bd

【讨论】:

  • 感谢您的帮助,这比我计划的混乱要简单得多,而且效果很好。
  • 已更新以解决您的 2 个字符和 6 个数字的要求
【解决方案2】:

我认为random.choice 是你想要的:

import random
acceptedChars = ["a","b","c","d","e","f"]
x = random.choice(acceptedChars)
y = random.choice(acceptedChars)

【讨论】:

  • 你可以给random.randint(0,9)的号码
  • 我推荐的数字:import string; random.choice(string.digits)
  • 或者如果你正在寻找一个组合,那么你可以在同一个 acceptedChars 列表中拥有 a thru f and 0 thru 9
  • 我知道random.choice 并积极使用它来随机选择第一个和最后一个字母字符。然而,问题是关于将字母和数字字符混合成一个 8 长度的字符串,字母字符的最大限制为 2。例如:endString = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(8)) 虽然这能够生成字母和数字的混合字符串,字母字符超出了可接受的字符范围,并且无法对两个字母的总数进行限制。
【解决方案3】:

检查整个代码以解决您的问题。也许你会发现一些有用的东西。我的复杂度低于 O(n2)。

是随机串生成验证程序。 此代码还满足最多 2 个 alpha 要求。

import random
def code():
    acceptedChars=["a","b","c","d","e","f"]
    first = "00"
    second = random.choice(acceptedChars)
    third = ""
    fourth = random.choice(acceptedChars) 

    # for third part

    slot = random.randint(0,2)
    if (slot == 2):
        number = str(random.randint(100000,1000000))
        alpha1 = random.choice(acceptedChars)
        alpha2 = random.choice(acceptedChars)
        part1 = random.randint(0,6)
        part2 = random.randint(part1,6)
        third = number[:part1] + alpha1 + number[part1:part2] + alpha2 + number[part2:]
    elif (slot == 1):
        number = str(random.randint(1000000,10000000))
        alpha = random.choice(acceptedChars)
        slot = random.randint(0,8)
        third = number[:slot] + alpha + number[slot:]
    else:
        third = str(random.randint(10000000,100000000))

    
    return first + second + third + fourth


print(code())

希望对你有帮助。

输出如下:

00d65262056f
00a317c8015e
00a334564ecf
00e14a657d9c

【讨论】:

  • 第三部分的任何位置都不需要字母字符。您正在强制代码使 2 个 alpha 字符彼此相邻。
【解决方案4】:
import string
import random

allowed_chars = string.ascii_letters[:6]

expression = ''.join(random.choices(allowed_chars + string.digits, k=8))
print(f"The generator is 00{str(expression)}")

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 2017-08-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多