1

(If you have a better title, do edit, I couldn't explain it properly! :) So this is my code:

with open('cipher.txt') as f:
    f = f.read().replace(' ', '')

new = []
let = []
for i in f:
    let.append(i)
    if i.count(i) > 1:
        i.count(i) == 1
    else:
        new = sorted([i + ' ' + str(f.count(i)) for i in f])
for o in new:
  print(o)

And this is cipher.txt:

xli uymgo fvsar jsb

I'm supposed to print out the letters used and how many times they are used, my code works, but I need it alphabetical, I tried putting them in a list list(a) and then sorting them, but i didn't quite get it, any ideas? Thanks in advance!

cfi
  • 10,915
  • 8
  • 57
  • 103
samir
  • 759
  • 2
  • 8
  • 13
  • possible duplicate of [Count occurrence of a character in a Python string](http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-python-string) – Lennart Regebro Sep 02 '13 at 08:15
  • sorry no, this is a totally different question, i know how to count an occurrence, but thank you – samir Sep 02 '13 at 08:20
  • OK, so store the results in a dictionary, per letter, and print that out in order. – Lennart Regebro Sep 02 '13 at 08:22
  • Agree with Lennart, that the question is a duplicate, but of two questions: [Sort dict alphabetically](http://stackoverflow.com/questions/15939732/how-can-i-sort-this-dictionary-with-alphabetically) and Lennart's own suggestions [Count occurrence of a character in a Python string](http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-python-string). Plus, there's still an indent error in the code above (will submit edit proposal) – cfi Sep 02 '13 at 10:49

2 Answers2

3

Whenever dealing with counting, you can use collections.Counter here:

>>> from collections import Counter
>>> print sorted(Counter('xli uymgo fvsar jsb'.replace(' ', '')).most_common())
[('a', 1), ('b', 1), ('f', 1), ('g', 1), ('i', 1), ('j', 1), ('l', 1), ('m', 1), ('o', 1), ('r', 1), ('s', 2), ('u', 1), ('v', 1), ('x', 1), ('y', 1)]

If you can't import any modules, then you can append a to a list and then sort it:

new = []
for i in f:
    new.append(i + ' ' + str(f.count(i)) # Note that i is a string, so str() is unnecessary

Or, using a list comprehension:

new = [i + ' ' + str(f.count(i)) for i in f]

Finally, to sort it, just put sorted() around it. No extra parameters are needed because your outcome is alphabetical :).

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • is there no way in doing it without an imported module? – samir Sep 02 '13 at 08:07
  • @samir There's probably a way. Let me try some stuff :) – TerryA Sep 02 '13 at 08:07
  • thank you! And let's say a letter occurs twice, it would print the letter twice and then number of times it's printed, how can I print the letter only once + occurrences? in the example 's' is printed twice :) – samir Sep 02 '13 at 08:09
  • @samir I won't edit my answer to include this, but I'll give you a hint (I hope you don't mind). When you go through each letter, you can add that letter to a list to say that you have come across it. You should then have an `if/else` structure to check if that letter is in the list. `If` it is in the list, then we know that you have already passed that letter beforehand, and that there is no need to continue. `Else`, do the normal proceedure ;) – TerryA Sep 02 '13 at 08:11
  • I edited my code, to what I thought would work, unfortunately not, i can't figure it out – samir Sep 02 '13 at 08:18
0

Here's a oneliner without imports:

{s[i]: n for i, n in enumerate(map(s.count, s))}

And in alphabetical order (if the above is d):

for k in sorted(d): print k, d[k]

Or another version (oneliner alphabetical):

sorted(set([(s[i], n) for i, n in enumerate(map(s.count, s))]))

Andras Nemeth
  • 319
  • 3
  • 10