Home > Enterprise >  Write a function to add key-value pairs
Write a function to add key-value pairs

Time:05-25

I'd like to write a function that will take one argument (a text file) to use its contents as keys and assign values to the keys. But I'd like the keys to go from 1 to n: {'A': 1, 'B': 2, 'C': 3, 'D': 4... }.

I tried to write something like this:

Base code which kind of works:

filename = 'words.txt'

with open(filename, 'r') as f:
    text = f.read()

ready_text = text.split()

def create_dict(lst):
    """ go through the arg, stores items in it as keys in a dict"""
    dictionary = dict()
    for item in lst:
            if item not in dictionary:
                dictionary[item] = 1
            else:
                dictionary[item]  = 1
    return dictionary

print(create_dict(ready_text))

The output: {'A': 1, 'B': 1, 'C': 1, 'D': 1... }.

Attempt to make the thing work:

def create_dict(lst):
    """ go through the arg, stores items in it as keys in a dict"""
    dictionary = dict()
    values = list(range(100)) # values
    for item in lst:
            if item not in dictionary:
                for value in values:
                    dictionary[item] = values[value]
            else:
                dictionary[item] = values[value]
    return dictionary

The output: {'A': 99, 'B': 99, 'C': 99, 'D': 99... }.

My attempt doesn't work. It gives all the keys 99 as their value.

Bonus question: How can I optimaze my code and make it look more elegant/cleaner?

Thank you in advance.

CodePudding user response:

You can use dict comprehension with enumerate (note the start parameter):

words.txt:

colorless green ideas sleep furiously

Code:

with open('words.txt', 'r') as f:
    words = f.read().split()

dct = {word: i for i, word in enumerate(words, start=1)}
print(dct)
# {'colorless': 1, 'green': 2, 'ideas': 3, 'sleep': 4, 'furiously': 5}

Note that "to be or not to be" will result in {'to': 5, 'be': 6, 'or': 3, 'not': 4}, perhaps what you don't want. Having only one entry out of two (same) words is not the result of the algorithm here. Rather, it is inevitable as long as you use a dict.

CodePudding user response:

You can use this code. If an item has been in the lst more than once, the idx is considered one time in dictionary!

def create_dict(lst):
    """ go through the arg, stores items in it as keys in a dict"""
    dictionary = dict()
    idx = 1
    for item in lst:
        if item not in dictionary:
            dictionary[item]=idx
            idx  = 1
    return dictionary

CodePudding user response:

Your program sends a list of strings to create_dict. For each string in the list, if that string is not in the dictionary, then the dictionary value for that key is set to 1. If that string has been encountered before, then the value of that key is increased by 1. So, since every key is being set to 1, then that must mean there are no repeat keys anywhere, meaning you're sending a list of unique strings.

So, in order to have the numerical values increase with each new key, you just have to increment some number during your loop:

num = 0
for item in lst:
    num  = 1
    dictionary[item] = num

There's an easier way to loop through both numbers and list items at the same time, via enumerate():

for num, item in enumerate(lst, start=1): # start at 1 and not 0
    dictionary[item] = num
  • Related