+ 6
Help me python3(solved)
Can someone help me, i want to make the alphabet enter in dictionary instead of put in one by one,whats wrong in this code? https://code.sololearn.com/c9iD0fJhj105/?ref=app
12 Answers
+ 2
For dictionary!
https://code.sololearn.com/cIv49jon2ulo/?ref=app
+ 6
As already mentioned, a dictionary has keys and values and looks like this:
alphabet = {
'first_letter': 'a',
'second_letter': 'b',
...
}
This is a list:
alphabet = ['a', 'b', 'c', ...]
To convert a string into a list in python, you have to do - nothing! In python, a "string" is a list of characters per se. That's why you can do things like string[5] to access the 6th character of the string or
for letter in string:
print(letter)
to iterate over all letters/characters of the string.
So, x = 'abcdef...' already is a list! You don't need lines 3 to 5. The only potential issue here is that this kind of list is immutable, so you can't use x[3] = 'm' to assign a new value to the 4th list entry. But I think you don't want to do this here anyway...
P.S. Better don't parenthesize your variables like in line 1. In some situations, they might be (mis)interpreted as tuples or some other data type you don't want
+ 4
You need no loop, you can just write:
xdic.extend(x)
+ 3
alph={i:chr(i+96) for i in range(1,27)}
print(alph)
+ 1
Well, I can not understand what exactly your code is doing with range function but:
xdic= [] - declares an empty list, not dictionary.
that is why you chould add an item with append method, since your list is empty:
xdic.append(i)
+ 1
strawdog Yeah, i always change the names (dictionary and list)
+ 1
@Arthur
1. Append method perfectly works with strings.
2. You can not assign a new value to a list by index if there is no any value with such index. That is why i told you you declared an _empty_ list. And that is why youy need to append/extend the list with new value.
As far as I understand, you just want to have a list of all alphabet letters.
It could be easily obtained with this code:
>>> x = "abcdefghijklmnopqrstuvwxyz"
>>> my_alpha_list=list(x)
>>> print(my_alpha_list)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
+ 1
Louis i arealdy solved..
+ 1
takes two iterables and joins them , typically the result is tuples, but here I generate a dictionary by a range and str and zip the two together
0
ord(i) - ord('a')+1 # no magic number đ post taking on life of it's own?
0
Davod what does 'zip(s,y)' do in your code?
0
Pro_me.py It's like a zipperđ