Python,样本大于人口

时间:2018-05-08 16:14:22

标签: python

好的,这是奇怪的,我想写一个学习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>

编辑:我最终发现了这个错误,我正在追加不合并列表,替换为+=,现在工作正常:)

0 个答案:

没有答案