How could this be done easier? My output is correct and the code is working fine, but it is not very efficient. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 3

How could this be done easier? My output is correct and the code is working fine, but it is not very efficient.

https://code.sololearn.com/cguxGjDsJ96g/?ref=app

2nd Jan 2023, 6:34 PM
Vic ten Bokum
Vic ten Bokum - avatar
9 ответов
+ 6
Vic ten Bokum The following code is also a more efficient solution: text = input() my_dict = {} for t in text: my_dict.update({t:text.count(t)}) print(my_dict) In the above code, the update function updates 'my_dict' with key:value pairs. As the for loop is iterating through 'text', my_dict is simultaneously being updated with the current value of 't' as the key, and the amount of times 't' occurs in 'text', as the value. As you can see, the update function is used to update a dictionary with key:value pairs.
2nd Jan 2023, 9:23 PM
Natan 🇹🇹
Natan 🇹🇹 - avatar
+ 3
Python can be super short, and very useful for that are the so-called "comprehensions". dictio = {letter: text.count(letter) for letter in text} This line alone creates the full dictionary and thereby replaces lines 6 to 29. Here you can read more about the topic: https://code.sololearn.com/ck6HicJ6Z8jG/?ref=app
4th Jan 2023, 3:29 PM
HonFu
HonFu - avatar
+ 2
Use the stored digit as a counter because your program only works if the input has 10 repeated letters. So, what you want to do is: if you find a new letter, its value must be 1, else, if the letter is repeated, increment the stored value. That sould work.
2nd Jan 2023, 7:14 PM
Edgar Sabido
Edgar Sabido - avatar
+ 2
HonFu that's amazing, thank you!
4th Jan 2023, 4:12 PM
Vic ten Bokum
Vic ten Bokum - avatar
+ 1
What are you trying to do? If you're trying to count the number of characters of a word/words entered by the user, you can use for loop, or len() function, both ways would be correct. for loop: https://code.sololearn.com/cTTS7EoNGwaP/?ref=app len() function: https://code.sololearn.com/cwFJADdKlxBa/?ref=app
2nd Jan 2023, 6:39 PM
Lamron
Lamron - avatar
0
Lamron I am trying to have a dictionary as output that has the letters of the input as keys and the number of times those letters are in the input string as values. The output of my code is exactly how I want it to be, but I was wondering if it coud be done in a different, more efficient, way.
2nd Jan 2023, 7:00 PM
Vic ten Bokum
Vic ten Bokum - avatar
0
I don't know how to do this, soz 🤷‍♂️
2nd Jan 2023, 7:10 PM
Lamron
Lamron - avatar
0
Edgar Sabido Thanks, I'll try that :)
2nd Jan 2023, 9:07 PM
Vic ten Bokum
Vic ten Bokum - avatar
- 1
Hello my name is Vincent aye I'm a beginner in sololarner and I have questions please 1 how to Change my profile picture because I have tried and it doesn't work 2 if send some one message after the first message I will not be able to send any one again 3 if I come online I will see notification Like ten to seven if i press notification bar I will see only 3 notification 4 please I am very confused here if I go to settings and go to account connection I will see connect to Facebook, Gmail , and gutbit if I login my Gmail and Facebook account to it it will not work please if you know what is happening please try to tell me here is my number:08107368096🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🥺🥺
4th Jan 2023, 12:36 PM
Aye Vincent