gpt4 book ai didi

Python bit.ly 链接词表生成器

转载 作者:行者123 更新时间:2023-12-04 14:53:15 29 4
gpt4 key购买 nike

我一直在开发一个 Python 工具来生成 bit.ly wordlist。以下是 bit.ly 链接的特殊性:

  • 包含 7 个实体
  • 以数字开头(通常为 3 或 2)
  • 以字母结尾
  • 不能并排放置相同的实体

我已经完成了前 3 个条件,但我找不到完成最后一个的方法。

from itertools import product

def firstN(chars, length):
for firstNumber in product(chars, repeat=length):
yield ''.join(firstNumber)

def combiwords(chars, length):
for letters in product(chars, repeat=length):
yield ''.join(letters)

def lastL(chars, length):
for lastLetter in product(chars, repeat=length):
yield ''.join(lastLetter)

def main():
firstNumber = "32"
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
lastLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

for wordlen1 in range(1, 2):
for first in firstN(firstNumber, wordlen1):
for wordlen2 in range(6, 7):
for combo in combiwords(letters, wordlen2):
for wordlen3 in range(1, 2):
for word in lastL(lastLetter, wordlen3):
print('https://bit.ly/' + first + combo + word)

if __name__=="__main__":
main()

最佳答案

解决方案

我写了几个函数可以解决你的问题:

import random
import string

def generate_alphanumeric_string_without_sequential_repetitions():
# string.ascii_lowercase means 'abcdefghijklmnopqrstuvwxyz'. If you need it to be case unsensitive, change this to string.ascii_lowercase
allowed_letters = string.ascii_lowercase
allowed_chars = allowed_letters + '0123456789'

result = random.choice(allowed_chars) # init first char of the sequence

# generate characters (which must be alphanumeric) in positions [1,5]
for i in range(1, 5):
random_char = random.choice(allowed_chars)

# avoid sequential repetitions by re-generating a char if is the same of the previous one
while random_char == result[i-1]:
random_char = random.choice(allowed_chars)

result = result + random_char

# generate last char (which must be a letter)
last_char = random.choice(allowed_letters)

# avoid sequential repetitions by re-generating last char if is the same of the previous one
while last_char == result[-1]: # result[-1] is a trick in python for getting the last char of a string
last_char = random.choice(allowed_letters)

return result + last_char


def generate_string():
return str(random.randint(0, 9)) + generate_alphanumeric_string_without_sequential_repetitions()

结果

执行以下代码

random.seed(10)

result = list()

for i in range(0, 10):
result.append(generate_string())

print(result)

我得到了以下结果:

['9c14ano', '7rkc75k', '1pxc0it', '5y0sq3f', '4xi3p2t', '6capimj', '8xpu92n', '7eu6kon', '3c5te8c', '2yxjhgo']

如有任何疑问,请在评论中提问。希望对您有所帮助!

关于Python bit.ly 链接词表生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68674270/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com