This task is feasible only when a certain condition changes: the words will be the keys, and the language will be the value. The fact is that (as far as I know) the Python language does not support more than one value for one key. Here below is the code that is the solution to your problem, under the new condition:
WordsList = ["word", "music","sky","компьютер","жизнь"] #Список изначальных слов WordsDict = {} #Требуемый словарь EnglishLetters = list ("bdDfFgGhiIjJklLmnNqQrRsStuUvVwWYzZ") #Англиские буквы, которые внешне отличаются от русских RussianLetters = list("бБвгГдДёЁжЖзЗиИйЙклЛмМнпПтУфФцЦчЧшШщЩъЪыЫьЬэЭюЮяЯ") #Русские буквы, которые внешне отличаются от англиских EnglishWords = [] RussionWords = [] Eng = 0 #Это 2 служебные переменные для подсчета русских и англиских букв Rus = 0 for i in WordsList: #Поочередный скан элементов списка Eng = 0 Rus = 0 LettersList = [] for t in i: # Поочередный скан букв слова if t in EnglishLetters: #Проверка буквы -- является ли англиской? Eng += 1 if t in RussianLetters: #Проверка буквы -- является ли русской? Rus += 1 if Eng > 0 and Rus == 0: EnglishWords.append(i) if Rus > 0 and Eng == 0: RussionWords.append(i) WordsDict = dict.fromkeys(RussionWords, "rus") WordsDict.update(dict.fromkeys(EnglishWords, "eng")) print(WordsDict)
The advantage of this program is that new words can be entered into the initial list and it will work. The most important thing is that the word can be defined externally in one of the two languages. Otherwise, the word simply will not be in the dictionary. By the way, I want to say that all words of the type: "312312" or "%" or "ttyutffyffyffE" will also not be reflected in the dictionary. Let's analyze the program:
1. At the beginning, all necessary lists, dictionaries and variables are created. I left comments in the code to make it clearer. By the way, the list function creates a list of text by letter. For example, after executing l = list("LIST") . As a result, the list is equal to ["L","I","S", "T"] .
2. Scan our list. To understand the scanning, look at the materials about the cycles (and specifically about for ) 3. We scan each letter of the element, check it for availability in the EnglishLetters and RussianLetters , change variables depending on the result (in order to understand what language it belongs to).
4. Next, we check the sizes of the Eng and Rus variables, then, based on the results of the verification by condition, we place the word in one of two lists ( EnglishWords , RussionWords )
5. After this, elements of one list are alternately inserted as keys with the value "rus". (command dict.fromkeys() ). We do the same with the second list, but the dict.fromkeys() command will be located in another one - update() , which is responsible for adding new values with their keys to the dictionary. Plus, instead of "rus" we write "eng". In conclusion, we display the dictionary on the screen.
@ Maker, I recommend to juggle articles by:
cycles
lists
conditions
dictionaries
and card
Also I will ask to understand the code, read the explanation and comments.
Thanks for the interesting question.
wordand theкомпьютерthat were in the dictionary, and notskyandжизнь. - andreymal