How to count the bell in certain letters in a word with a generated cycle - Python
-
Hello, everyone. For a few days, I'm going to break my head and I can't find a solution. Can someone tell or give the right direction?
Anyway, The essence of the task isI need to find the words in which there are three letters "A."
We still have the following data on our hands:
1. 5 letters [A, B, C, D, E];
2. The cycle that selects the above symbols and generates new words from them;
3. We also know that the words themselves must be composed of eight symbols. I wrote the Circle, it's understandable, and this is how to check the generated cycle of the word "A" - I don't understand (namely three, not four, and above, because the first edited word has eight A letters, and, as a condition of the task, we need to find the words in which they are only three or more. My code (yeah, it may not be perfect, but working, learning, so to say):
## Мой словарь символов symbols = ["A", "B", "C", "D", "E"] ## Мои строковые переменные one = str() two = str() three = str() four = str() five = str() six = str() seven = str() eight = str() all_num = str() ## Мои численные переменные count = 0 ## Ещё одини строковые перемнные, но статичные, т.е. неизменяемые one_str = str("Номер комбинации символов:") two_str = str("комбинация символов:") ## Перебираем символы из словаря и присваиваем переменной "one" for one in symbols: ## Перебираем символы из словаря и присваиваем переменной "two" for two in symbols: ## Перебираем символы из словаря и присваиваем переменной "three" for three in symbols: ## Перебираем символы из словаря и присваиваем переменной "four" for four in symbols: ## Перебираем символы из словаря и присваиваем переменной "five" for five in symbols: ## Перебираем символы из словаря и присваиваем переменной "six" for six in symbols: ## Перебираем символы из словаря и присваиваем переменной "seven" for seven in symbols: ## Перебираем символы из словаря и присваиваем переменной "eight" for eight in symbols: ## Наш счётчик count += 1 ## Наша основаная переменная, в которую мы передаём и, условно говоря, складываем, перебранные циклами значения all_num = one + two + three + four + five + six + seven + eight ## Выводим результат(-ы) print(one_str, count, two_str, all_num)
I thank you in advance for the answer, the tip or the code developed. And please, you can explain why, after all, this task made me feel good.
-
I don't get it, do you something?
words = ['mama', 'papa', 'ararat', 'banana']
res = sum(word.count('a') == 3 for word in words)
print(res) # выведет 2 - 2 слова с 3 буками 'а'
In fact, if you do a long-term code,
res = 0
for word in words: # перебираем все слова из списка
if word.count('a') == 3: # с помощью метода 'count()' определяем сколько букв 'a' встречается в анализируемом слове
res += 1 # увеличиваем счетчик слов у которых 3 буквы 'a' на 1
A short one-stop list contains as many elements as on the list with words where each word is aligned.
True
orFalse
Depending on whether word 3 contains the letters 'a' or not. And then the sum of all elements of the new list is calculated, asTrue
Interpreted when decompositioned as1
♪False
like,0
the calculation of the sum results in three letters of 'a'So does the code.
res = sum(1 for word in words if word.count('a') == 3)
There's only a list of 1 for each word on the list.
words
in which there were three letters 'a'