我一直在开发一个Python工具来生成比特。ly词库。这是比特的特点。ly链接:
- 包含7个实体
- 以数字开头(一般为3或2)
- 以字母 结尾
- 同一实体不能并排
我已经完成了前三个条件,但是我找不到方法来完成最后一个条件。
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']
如果你有任何问题,请在评论中提问。我希望这对你有帮助!
标题>这可能就是您要搜索的内容:
https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/
def removeUtil(string, last_removed):
# If length of string is 1 or 0
if len(string) == 0 or len(string) == 1:
return string
# Remove leftmost same characters
# and recur for remaining
# string
if string[0] == string[1]:
last_removed = ord(string[0])
while len(string) > 1 and string[0] ==
string[1]:
string = string[1:]
string = string[1:]
return removeUtil(string, last_removed)
# At this point, the first
# character is definiotely different
# from its adjacent. Ignore first
# character and recursively
# remove characters from remaining string
rem_str = removeUtil(string[1:], last_removed)
# Check if the first character
# of the rem_string matches
# with the first character of
# the original string
if len(rem_str) != 0 and rem_str[0] ==
string[0]:
last_removed = ord(string[0])
return (rem_str[1:])
# If remaining string becomes
# empty and last removed character
# is same as first character of
# original string. This is needed
# for a string like "acbbcddc"
if len(rem_str) == 0 and last_removed ==
ord(string[0]):
return rem_str
# If the two first characters of
# str and rem_str don't match,
# append first character of str
# before the first character of
# rem_str.
return ([string[0]] + rem_str)
def remove(string):
last_removed = 0
return toString(removeUtil(toList(string),
last_removed))
# Utility functions
def toList(string):
x = []
for i in string:
x.append(i)
return x
def toString(x):
return ''.join(x)
将当前输出存储到列表(例如wl
)
wl.append('https://bit.ly/' + first + combo + word)
,循环后对每个条目运行remove
。