Home > Net >  How to separate elements in a list to put them as value in a dictionary
How to separate elements in a list to put them as value in a dictionary

Time:11-03

I want to display words that have the required character in a dictionary. For example :

Write a sentence: Mister Jack, you are a good coder

{'a': ['are', 'jack,'], 'b': [], 'c': ['coder', 'jack,'], 'd': ['coder'], 'e': ['coder', 'mister',], 'f': [], 'g': [], 'h': [], 'i': ['mister'], 'j': ['jack,'], 'k': ['jack,'], 'l': [], 'm': ['mister', ], 'n': [], 'o': ['coder', 'you', 'good'], 'p': [], 'q': [], 'r': ['coder', 'mister'], 's': ['mister', ], 't': ['mister'], 'u': ['you'], 'v': [], 'w': [], 'x': [], 'y': ['you'], 'z': []}

Here is my code :

import string

alphabet = string.ascii_lowercase
sentence= (input("Write your sentence: " )).lower()
letter_notfound=(sorted(list((set(string.ascii_lowercase) - set("".join(sentence.split()))))))
print (letter_notfound)

letter_list=list(alphabet)
unique_letter = (sorted(list((set(letter_list)-set(letter_notfound)))))

dico={}

for key in (letter_list):
    for char in sentence:
        if char in unique_letter:
         dico[char] = [sentence]
    for char in letter_notfound:
        dico[char] = []
print(dict(sorted(dico.items())))

Input: Mister Jack, you are a good coder

Output:

{'a': ['mister jack, you are a good coder'], 'b': [], 'c': ['mister jack, you are a good coder'], 'd': ['mister jack, you are a good coder'], 'e': ['mister jack, you are a good coder'], 'f': [], 'g': ['mister jack, you are 
a good coder'], 'h': [], 'i': ['mister jack, you are a good coder'], 'j': ['mister jack, you are a good coder'], 'k': ['mister jack, you are a good coder'], 'l': [], 'm': ['mister jack, you are a good coder'], 'n': [], 'o': ['mister jack, you are a good coder'], 'p': [], 'q': [], 'r': ['mister jack, you are a good coder'], 's': ['mister jack, you are a good coder'], 't': ['mister jack, you are a good coder'], 'u': ['mister jack, you are a good coder'], 'v': [], 'w': [], 'x': [], 'y': ['mister jack, you are a good coder'], 'z': []}

Desired output:

{'a': ['are', 'jack,'], 'b': [], 'c': ['coder', 'jack,'], 'd': ['coder'], 'e': ['coder', 'mister',], 'f': [], 'g': [], 'h': [], 'i': ['mister'], 'j': ['jack,'], 'k': ['jack,'], 'l': [], 'm': ['mister', ], 'n': [], 'o': ['coder', 'you', 'good'], 'p': [], 'q': [], 'r': ['coder', 'mister'], 's': ['mister', ], 't': ['mister'], 'u': ['you'], 'v': [], 'w': [], 'x': [], 'y': ['you'], 'z': []}

CodePudding user response:

This makes a dictionary with the alphabet and then turns input into a list separated by words. Then we loop over the letters and check if it is in the alphabet dictionary if so we append the word.

import string

alphabet = string.ascii_lowercase
alphabet_dict = {alphabet[i]: [] for i in range(len(alphabet))}
input_list = list(input("Enter a string: ").split(" "))
for word in input_list:
    for letter in word:
        if letter in alphabet_dict:
            alphabet_dict[letter].append(word)
print(alphabet_dict)

CodePudding user response:

First thing I would recommend is to iterate through words, as only them are important for you. You can get them with split

words = sentence.split()

Second thing is you nowhere append new word to existing key. You should start with preparing the dict.

dico = {letter: [] for letter in alphabet}

Now it should be easy to check if letter is in word.

for letter im letter_list:
    for word in words:
        if letter in word:
            dico[letter].append(word)

CodePudding user response:

More 'Pythonic' to use list/dictionary comprehensions:

words = sentence.split(' ') # Assuming already lowercase
alphabet_dict = {x: [w for w in words if x in w] for x in ascii_lowercase}

Comprehensions are not just more concise but are more declarative rather than imperative, and so in most cases are easier to understand as it more readily states the what rather than the how.

I recommend reading When to Use a List Comprehension in Python

  • Related