0

Here in the program how can you find the second repetitive character in the string. for ex:abcdaabdefaggcbd"​ Output : d (because 'd' occurred 3 times where 'a' occurred 4 times)​ how can I get the output, please help me.

Given below is my code:

s="abcdaabdefaggcbd"
d={}
for i in s:
    d[i] = d.get(i,0)+1
print(d,"ddddd")
max2 = 0
for k,v in d.items():
     if(v>max2 and v<max(d.values())):
            max2=v
            if max2 in d.values():
                print k,"kkk"

4 Answers 4

3

The magnificent Python Counter and its most_common() method are very handy here.

import collections

my_string = "abcdaabdefaggcbd"
result = collections.Counter(my_string).most_common()
print(result[1])

Output

('b', 3)

In case you need to capture all the second values (if you have more than one entry) you can use the following:

import collections

my_string = "abcdaabdefaggcbd"
result = collections.Counter(my_string).most_common()
second_value = result[1][1]
seconds = []
for item in result:
    if item[1] == second_value:
        seconds.append(item)
print(seconds)

Output

[('b', 3), ('d', 3)]

I also wanted to add an example of solving the problem using a methodology more similar to the one that you showed in your question:

my_string="abcdaabdefaggcbd"
result={}
for character in my_string:
    if character in result:
        result[character] = result.get(character) + 1
    else:
        result[character] = 1

sorted_data = sorted([(value,key) for (key,value) in result.items()])
second_value = sorted_data[-2][0]

result = []
for item in sorted_data:
    if item[0] == second_value:
        result.append(item)

print(result)

Output

[(3, 'b'), (3, 'd')]

Ps
Please forgive me if I took the freedom to change variable names but I think that in this way my answer will be more readable for a broader audience.

Sign up to request clarification or add additional context in comments.

4 Comments

Both b and d can be considered as the second most repetitive. Would it not be better to compare the values to find the second highest value and then check the keys associated with that value ? This way, both b& dwill be displayed.
There is no specific requirement for this so I just returned the 1st value (the lower letter in the alphabet)
@Gary I've also added a solution in case you want to get multiple entries
Duh ! I forgot about Counter.most_common() :facepalm:
1

Sort the dict's items on their values (descending) and get the second item:

>>> from collections import Counter
>>> c = Counter("abcdaabdefaggcbd")
>>> vals = sorted(c.items(), key=lambda item:item[1], reverse=True)
>>> vals
[('a', 4), ('b', 3), ('d', 3), ('c', 2), ('g', 2), ('e', 1), ('f', 1)]
>>> print(vals[1])
('b', 3)
>>> 

EDIT:

or just use Counter.most_common():

>>> from collections import Counter
>>> c = Counter("abcdaabdefaggcbd")
>>> print(c.most_common()[1])

2 Comments

But you beat me on time in any case! +1 :)
@Pitto better a late good answer than an early not that good one...
0

Both b and d are second most repetitive. I would think that both should be displayed. This is how I would do it:

Code:

s="abcdaabdefaggcbd"
d={}
for i in s:
    ctr=s.count(i)
    d[i]=ctr

fir = max(d.values()) 
sec = 0
for j in d.values(): 
     if(j>sec and j<fir): 
            sec = j
for k,v in d.items():
    if v == sec:
        print(k,v)

Output:

b 3
d 3

Comments

0

in order to find the second most repetitive character​ ​in string you can very well use collections.Counter()

Here's an example:

import collections
s='abcdaabdefaggcbd'
count=collections.Counter(s)
print(count.most_common(2)[1])

Output: ('b', 3)

You can do a lot with Counter(). Here's a link for a further read: More about Counter()

I hope this answers your question. Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.