Home > other >  How do modify the dictionary key to the first letter of the word in the list?
How do modify the dictionary key to the first letter of the word in the list?

Time:04-23

I'd like to modify the dictionary key I went to the stage where I changed the key of the dictionary to a value I don't know how to approach it

[enter image description here]

Question) Use the list below to create a dictionary

english_word_list = ['black', 'history', 'blood', 'campaign', 'image', 'kid', 'kill',
                        'can', 'eye', 'faceblue', 'camera', 'future', 'game', 'kind', 'kitchen']

Specify the first letter of the English word as the dictionary key. f the key is duplicated, specify the key as a combination of English first letters and numbers.

ex)

{'b': 'black', 'h': 'history', 'b2': 'blood', 'c': 'campaign', 'i': 'image', 
 'k': 'kid', 'k2': 'kill', 'c2': 'can', 
 'e': 'eye', 'f': 'faceblue', 'c3': 'camera', 
 'f2': 'future', 'g': 'game', 'k3': 'kind', 'k4': 'kitchen'}

CodePudding user response:

You are probably talking of programming this via python I guess.

This is a simple solution to the problem you are asking for

english_word_list = ['black', 'history', 'blood', 'campaign', 'image', 'kid', 'kill',
                     'can', 'eye', 'faceblue', 'camera', 'future', 'game', 'kind', 'kitchen']

dictionary = {}       # create an empty dictionary for lookup
for word in english_word_list:
    letter = word[0]  # assume no empty words
    key = letter
    i = 2
    while dictionary.get(key) is not None:   # get will return None if no key is present
        key = letter   str(i)
        i  = 1
    dictionary[key] = word                   # add the word with the free key

print(dictionary)

Hope this helps, please accept the answer if it is ok for you.

CodePudding user response:

You can use an auxiliary dictionary to keep track of how many of each starting letter you've seen so far, adding the appropriate key depending on the count.

import string

counters = dict.fromkeys(string.ascii_lowercase, 1)
result = {}

for word in english_word_list:
    if counters[word[0]] == 1:
        result[word[0]] = word
    else:
        result[f"{word[0]}{counters[word[0]]}"] = word
    
    counters[word[0]]  = 1
    
print(result)

This outputs:

{'b': 'black', 'h': 'history', 'b2': 'blood',
'c': 'campaign', 'i': 'image', 'k': 'kid', 'k2': 'kill',
'c2': 'can', 'e': 'eye', 'f': 'faceblue',
'c3': 'camera', 'f2': 'future', 'g': 'game',
'k3': 'kind', 'k4': 'kitchen'}

CodePudding user response:

While this seems more complicated, I'd argue you should do this in two passes:

ls = ['black', 
      'history', 
      'blood', 
      'campaign', 
      'image', 
      'kid', 
      'kill',
      'can', 
      'eye', 
      'faceblue', 
      'camera', 
      'future', 
      'game', 
      'kind',
      'kitchen']

# Group by first letter
dt = dict()
for i in ls:
    letter = i[0]
    lx = dt.get(letter, [])
    lx.append(i)
    dt[letter] = lx

print(dt)

# Enumerate keys based on group size
dx = dict()
for k, v in dt.items():
    # For a given list, assign a key to each item
    for idx, item in enumerate(v):
       key = f"{k}{idx}" if idx > 0 else f"{k}"
       dx[key] = item

print(dx)

The first for loop groups all the words by their first letter. print(dt) demonstrates this. This is a useful intermediary step to ensure you're doing things correctly. Then, given dt, we can construct your desired outcome, dx easily - because we're operating on a list of similar items, rather than over an entire raw dictionary.

  • Related