-3

Trying to count the count of string where lst=('abcdefgabc')

lst=('abcdefgabc')
for i in lst:
    lst.count(i)
    print(i,(lst.count(i)))

output should be a=2, b=2, c=2, d=1,e=1,f=1,g=1

rafaelc
  • 52,436
  • 15
  • 51
  • 78
PP Sinha
  • 11
  • 1
  • 1
    How is this different from the output you are getting? – Scott Hunter Sep 13 '19 at 18:34
  • 1
    if you need the `=`, just add it as one more arg in the `print` function – rafaelc Sep 13 '19 at 18:35
  • Related: [How can I count the occurrences of a list item?](https://stackoverflow.com/q/2600191/4518341) – wjandrea Sep 13 '19 at 18:38
  • See answers referring to `Counter` in duplicate. – Sayse Sep 13 '19 at 18:39
  • Try `collections.Counter("abcdefgabc")` – pylang Sep 13 '19 at 18:40
  • Welcome to StackOverflow. Please follow the posting guidelines in the help documentation, as suggested when you created this account. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. We expect you to make a reasonable search for a solution before you post. – Prune Sep 13 '19 at 18:43
  • Side-note: Those parens around your string are pointless; `lst = 'abcdefgabc'` would mean the same thing, and not risk confusing people (e.g. with the name `lst`, I skimmed over it reading it as `list('abcdefgabc')` and thought you were pointlessly converting to `list`, rather than assigning a string to the name `lst`). – ShadowRanger Sep 13 '19 at 20:15

1 Answers1

0

Is this what you are looking for?

lst=('abcdefgabc')
new_list = []
for i in lst:
    a = i+"="+str((lst.count(i)))
    new_list.append(a)
b = sorted(set(new_list))
print(b)
for x in b:
    print(x)

Output:

['a=2', 'b=2', 'd=1', 'c=2', 'f=1', 'e=1', 'g=1']
a=2
b=2
d=1
c=2
f=1
e=1
g=1
Celius Stingher
  • 14,458
  • 5
  • 18
  • 47