I am working on the folowing code, which basically counts every letter in a given text.
I want to store the numbers in letter names:
A=1
B=3
and so on...
I guess it stores the number underthe name 'letter', but not under the corresponding letter 'A'.
It would be great if someone can help.
thanks

text='ARARAKHTLROGKFMBLFKKGOHMFDGRKLRNGROGINFBLMNFBAEKJGFGOQEITHRGLFDKBN'
abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
textletters= list(text)
abcletters= list(abc)
for letter in abcletters:
      letter=textletters.count(letter)
print A

If you use a Python container called a dictionary, you can come close to what you aspire ...

text = 'ARARAKHTLROGKFMBLFKKGOHMFDGRKLRNGROGINFBLMNFBAEKJGFGOQEITHRGLFDKBN'

abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# create a letter dictionary with character:frequency pairs
letter = {}
for c in text:
    letter[c] = letter.get(c, 0) + 1

print letter['A']  # 4
print letter['R']  # 7

for c in abc:
    if c in letter:
        print c, letter[c]

"""my output -->
A 4
B 4
D 2
E 2
F 7
G 8
H 3
I 2
J 1
K 7
L 5
M 3
N 4
O 4
Q 1
R 7
T 2
"""

If you use a Python container called a dictionary, you can come close to what you aspire ...

text = 'ARARAKHTLROGKFMBLFKKGOHMFDGRKLRNGROGINFBLMNFBAEKJGFGOQEITHRGLFDKBN'

abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# create a letter dictionary with character:frequency pairs
letter = {}
for c in text:
    letter[c] = letter.get(c, 0) + 1

print letter['A']  # 4
print letter['R']  # 7

for c in abc:
    if c in letter:
        print c, letter[c]

"""my output -->
A 4
B 4
D 2
E 2
F 7
G 8
H 3
I 2
J 1
K 7
L 5
M 3
N 4
O 4
Q 1
R 7
T 2
"""

Thanks a lot for the quick reply.

Another way you can look at.

text = 'ARARAKHTLROGKFMBLFKKGOHMFDGRKLRNGROGINFBLMNFBAEKJGFGOQEITHRGLFDKBN'
abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

textletters = list(text)
abcletters = list(abc)

my_list = []
for letter in abcletters:
      letter = textletters.count(letter)
      my_list.append(letter)

#Now we have a list,but not easy to understand
print my_list

'''zip demo-->
>>> l = ['a','b','c']
>>> n = [1,2,3]
>>> zip(l,n)
[('a', 1), ('b', 2), ('c', 3)]
'''

#We can use zip in a for loop to par it up
for item, letters in zip(my_list, abcletters):
    print item,letters

'''Out-->
[4, 4, 0, 2, 2, 7, 8, 3, 2, 1, 7, 5, 3, 4, 4, 0, 1, 7, 0, 2, 0, 0, 0, 0, 0, 0]
4 A
4 B
0 C
2 D
2 E
7 F
8 G
3 H
2 I
1 J
7 K
5 L
3 M
4 N
4 O
0 P
1 Q
7 R
0 S
2 T
0 U
0 V
0 W
0 X
0 Y
0 Z
'''

And there is the dict counting as comes from string and sorting the found letters in end:

text = 'ARARAKHTLROGKFMBLFKKGOHMFDGRKLRNGROGINFBLMNFBAEKJGFGOQEITHRGLFDKBN'

letterc={}
for i in text:
    if i in letterc:
        letterc[i]+=1
    else:
        letterc[i]=1

for  i in sorted(letterc.keys()):
    print(i,letterc[i])
"""('A', 4)
('B', 4)
('D', 2)
('E', 2)
('F', 7)
('G', 8)
('H', 3)
('I', 2)
('J', 1)
('K', 7)
('L', 5)
('M', 3)
('N', 4)
('O', 4)
('Q', 1)
('R', 7)
('T', 2)"""

I guess though that vegaseat tried to find nice solution nearest to posters code, which for me is good way to answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.