1

So I want to make a method that creates a dictionary based on input. 3 different ones, a dictionary with keys 0-9 for decimal, keys 0-7 for octal and keys 0-F for hexadecimal. I use a string with items 0-F as a way of creating the keys and depending on which file is used for the creation (different ones for different number systems) it'll create the dictionary with the appropriate keys.

My question is, for the octal and decimal files, how do I use dictionary comprehension to to limit the amount of keys to be created? So fileDec will stop at key 9 and fileOct will stop at key 7. Right now it creates a dictionary with all 16 keys regardless.

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))
user2390182
  • 72,016
  • 6
  • 67
  • 89
Scottie Joe
  • 97
  • 1
  • 10
  • Possible duplicate: http://stackoverflow.com/questions/2437617/limiting-the-size-of-a-python-dictionary Basically you need to overwrite dict, since no such function exists in the standard library – Jan Apr 05 '16 at 08:04

4 Answers4

2

since the dictionIndexes is sorted, you can just use a sublist (well, substring) for the decimal/octal keys)

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
umläute
  • 28,885
  • 9
  • 68
  • 122
1

If, for each file you want to process, you know the nature of the input you can use the following function that accepts an integer argument specifying the encoding of numbers

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)
gboffi
  • 22,939
  • 8
  • 54
  • 85
0

Method 1:

Create 3 dictionIndexes and use them to get different 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))

Method 2:

Loop till the required length only:

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))
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
0

Since you like dicts ;)

def create_dictionary(file):
    alldigits="0123456789ABCDEF"
    nbdigits={FileHex:16,FileDec:10,FileOct:8}
    sortBins = {item : [] for item in alldigits[:nbdigits[file]]}
    return sortBins
B. M.
  • 18,243
  • 2
  • 35
  • 54