A list of Russian and English words is given:

lst=(word, music,sky,компьютер,жизнь) 

It is necessary to create a dictionary, where the keys are the names of languages, and the values ​​are words from the list.

Like that:

 dict={'eng': word, 'rus':компьютер} 

for each word respectively.

  • First, I never managed to figure out by what algorithm such a dictionary should make such a dictionary - where did the words music, sky, life disappear? Secondly, it looks like a learning task - what did you try to do and why do you fail? - andreymal
  • This is just an example of how the result should look like, there are no attempts. So far my head is unable to figure out how to do it. - Kettle
  • one
    Then let's start with the basics. Have you read any Python tutorial? - andreymal
  • Yes, everything is not so bad, I calmly build graphics, I work with arrays, strings, file records, and so on. I am in my second year, but the teacher does not convey the necessary amount of information, alas. I will not refuse your advice on what you can read. But with I don’t know what to do with this task. And yes, you are right: it’s educational :) - Teapot
  • 2
    In this case, it is necessary to clarify what the problem is. Can't set the list? Can't create a dictionary? You do not know how to distinguish the Russian word from English? You do not know how to consistently process each word in the list? If you cannot do all of the above, you need to read the textbooks. If one thing - add the appropriate clarification in the question. // And give a more distinct example, because I still do not understand why it was the word and the компьютер that were in the dictionary, and not sky and жизнь . - andreymal

1 answer 1

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.

  • This is something wonderful! Thank you, you are a genius :) - Teapot