如何使用字典理解来限制它的创建?

How to use dictionary comprehension to limit its creation?

所以我想制作一个根据输入创建字典的方法。 3 个不同的字典,其中键 0-9 表示十进制,键 0-7 表示八进制,键 0-F 表示十六进制。我使用带有项目 0-F 的字符串作为创建键的方式,并且根据用于创建的文件(不同的数字系统使用不同的文件),它将使用适当的键创建字典。

我的问题是,对于八进制和十进制文件,我如何使用字典理解来限制要创建的键的数量?所以 fileDec 将在键 9 处停止,fileOct 将在键 7 处停止。现在它无论如何都会创建一个包含所有 16 个键的字典。

def create_dictionary(file):
    if file == fileDec:
        sortBins = {item : [] for item in dictionIndexes}
        return sortBins
    elif file == fileOct:
        print("Oct")
    elif file == fileHex:
        print("Hex")

mainBin = []
dictionIndexes = "0123456789ABCDEF"
sortBins = {}

fileDec = open("Number Lists/random_numbers10.txt")
fileDec.close()
fileOct = open("Number Lists/random_numbers4.txt")
fileOct.close()
fileHex = open("Number Lists/random_numbers3.txt")
fileHex.close()

sortBins = create_dictionary(fileDec)

for item in dictionIndexes:
    print(item, sortBins[item])

print(len(dictionIndexes))

方法一:

创建 3 个 dictionIndexes 并使用它们来获得不同的 sortbins

def create_dictionary(file):
if file == fileDec:
    sortBins = {item : [] for item in dictionIndexesDEC}
    return sortBins
elif file == fileOct:
    sortBins = {item : [] for item in dictionIndexesOCT}
    return sortBins
elif file == fileHex:
   sortBins = {item : [] for item in dictionIndexesHEX}
    return sortBins

mainBin = []
dictionIndexesHEX = "0123456789ABCDEF"
dictionIndexesDEC = "0123456789"
dictionIndexesOCT = "01234567"
sortBins = {}

fileDec = open("Number Lists/random_numbers10.txt")
fileDec.close()
fileOct = open("Number Lists/random_numbers4.txt")
fileOct.close()
fileHex = open("Number Lists/random_numbers3.txt")
fileHex.close()

sortBins = create_dictionary(fileDec)

for item in dictionIndexes:
    print(item, sortBins[item])

print(len(dictionIndexes))

方法二:

只循环到所需的长度:

def create_dictionary(file):
if file == fileHex:
    sortBins = {item : [] for item in dictionIndexes}
    return sortBins
elif file == fileOct:
    lst = []
    for i in range(8):
       lst.append(dictionIndexes[i]);
    sortBins[item] = lst;
    return sortBins;
elif file == fileDec:
    lst = []
    for i in range(10):
       lst.append(dictionIndexes[i]);
    sortBins[item] = lst;
    return sortBins;
    print("Hex")

mainBin = []
dictionIndexes = "0123456789ABCDEF"
sortBins = {}

fileDec = open("Number Lists/random_numbers10.txt")
fileDec.close()
fileOct = open("Number Lists/random_numbers4.txt")
fileOct.close()
fileHex = open("Number Lists/random_numbers3.txt")
fileHex.close()

sortBins = create_dictionary(fileDec)

for item in dictionIndexes:
    print(item, sortBins[item])

print(len(dictionIndexes))

由于 dictionIndexes 已排序,您可以只为 decimal/octal 键使用子列表(好吧,子字符串))

def create_dictionary(file):
    if file == fileDec:
        sortBins = {item : [] for item in dictionIndexes[:10]}
        return sortBins
    elif file == fileOct:
        sortBins = {item : [] for item in dictionIndexes[:8]}
        return sortBins
    elif file == fileHex:
        sortBins = {item : [] for item in dictionIndexes}
        return sortBins

既然你喜欢听写 ;)

def create_dictionary(file):
    alldigits="0123456789ABCDEF"
    nbdigits={FileHex:16,FileDec:10,FileOct:8}
    sortBins = {item : [] for item in alldigits[:nbdigits[file]]}
    return sortBins

如果对于要处理的每个文件,您都知道输入的性质,则可以使用以下接受指定数字编码的整数参数的函数

def mkd(code):
    if code in (8, 10, 16):
        return {c:[] for c in '0123456789ABCDEF'[:code]}
    else:
        raise ValueError('Received code is %r. Valid codes are 8, 10, 16.'%code)