好的,这是奇怪的,我想写一个学习python的目的的脚本,我坚持这个错误,这里是代码:
def get_segment(_len,dic):
word_segment = ''
dictionary = []
while dic > 0:
if dic >= 8:
dictionary.append(DICTIONARY_SYMBOLS)
dic -= 8
elif dic >= 4:
dictionary.append(DICTIONARY_NUMBERS_DEC)
dic -= 4
elif dic >= 2:
dictionary.append(DICTIONARY_LETTERS_LOWER)
dic -= 2
else:
dictionary.append(DICTIONARY_LETTERS_UPPER)
dic -= 1
dictionary = scramble_list(dictionary)
word_segment = ''.join(random.sample(dictionary,_len))
return word_segment
错误正好是ValueError: Sample larger than population or is negative
,但我的列表dictionary
包含的元素多于_len
值,同时考虑到_len
可以填充重复的元素。< / p>
编辑:我最终发现了这个错误,我正在追加不合并列表,替换为+=
,现在工作正常:)