-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
  • 57,686
  • 15
  • 58
  • 82
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
  • 17,835
  • 6
  • 23
  • 53
  • There is no need for two `lst.count(i)`'s. Edit: There is no need for **three** `lst.count(i)`s. Count it once and store it as a local variable if you need it later in the loop. This is looping over `lst` `3*len(lst)` times. 3 times for each element. – SyntaxVoid Sep 13 '19 at 18:40
  • Thank you so much, however we don't required the output double. eg. a=2 is showing twice. It should be displayed once. – PP Sinha Sep 13 '19 at 18:47
  • Edited! I hope this works well for you now. – Celius Stingher Sep 13 '19 at 18:59
  • 1
    Great Thanks to you. Yes that was the same which I was looking for. :) – PP Sinha Sep 13 '19 at 19:43
  • 1
    @PPSinha if this solves your problem, please mark it as the accepted answer! – SyntaxVoid Sep 13 '19 at 19:48
  • 1
    Note that the OP seemed to want the output in order; changing `list(set(new_list))` to `sorted(set(new_list))` would accomplish that. Without sorting, the output order will actually vary from run to run (due to hash salting). – ShadowRanger Sep 13 '19 at 20:18
  • True, thanks for your suggestion, I edited my answer to contemplate it. – Celius Stingher Sep 13 '19 at 20:26