How is the process of generating a dictionary with a previously known value:

{K:0 for K in 'словарь'} 

How is the string словарь broken down into keys?

  • you can use dict.fromkeys('словарь', 0) -> {'ь': 0, 'с': 0, 'в': 0, 'о': 0, 'л': 0, 'а': 0, 'р': 0} - jfs

2 answers 2

In the loop, you get a character from a string and the value of K (the current character) is indicated by the dictionary key

     stringClass = 'словарь' stringClassMethods = dir(stringClass) print(stringClassMethods) print(hasattr(stringClass, '__iter__')) it = stringClass.__iter__() print(hasattr(it, '__next__')) for a in range(len(stringClass)): print(it.__next__()) 

    [' add ', ' class ', ' contains ', ..., ' iter ']

    True

    True

    vocabulary

    the string is an object of class str, which contains the iter method every time a string is accessed, the next method is called, which returns the next element of the string. After the last element is reached, a StopIteration exception is raised.