How to count number of duplicates of item in string? - python

Suppose I have an string: s = "hello2020"
How can I create a program that returns the number of duplicates in the string? In this instance, the program would return 3 as the letter "l" appears more than once, and the numbers "2" and "0" also appear more than once.
Thanks in advance.
EDIT: So far, I have tried: return len([x for x in set(s) if s.count(x) > 1]), but it fails two of the testcases. Therefore, I am looking for an alternative solution.

from collections import Counter
def do_find_duplicates(x):
dup_chars = 0
for key,val in Counter(x).items():
if val > 1: dup_chars += 1
print(dup_chars)
do_find_duplicates('hello2020')

One Liner Solution, convert string to set then subtract length of string by converted set string
def duplicate_count(string):
return len(string) - len(set(string))
print(duplicate_count("hello2020"))
#3

Related

How do I count up the number of letter pairs in python?

I am making a program in python that count up the number of letter pairs.
For example ------> 'ddogccatppig' will print 3, 'a' will print 0, 'dogcatpig' will print 0, 'aaaa' will print 3, and 'AAAAAAAAAA' will print 9.
My teacher told me to use a for loop to get the i and i+1 index to compare. I do not know how to do this, and I am really confused. My code:
def count_pairs( word ):
pairs = 0
chars = set(word)
for char in chars:
pairs += word.count(char + char)
return pairs
Please help me!!! Thank you.
The for loop is only to iterate through the appropriate values of i, not directly to do the comparison. You need to start i at 0, and iterate through i+1 being the last index in the string. Work this out on paper.
Alternately, use i-1 and i; then you want to start i-1 at 0, which takes less typing:
for i in range(1, len(word)):
if word[i] == word[i-1]:
...
Even better, don't use the counter at all -- make a list of equality results and count the True values:
return sum([word[i] == word[i-1] for i in range(1, len(word))])
This is a bit of a "dirty trick" using the fact that True evaluates as 1 and False as 0.
If you want to loop over indices instead of the actual characters, you can do:
for i in range(len(word)):
# do something with word[i] and/or word[i+1] or word[i-1]
Converting the string to a set first is counterproductive because it removes the ordering and the duplicates, making the entire problem impossible for two different reasons. :)
Here is an answer:
test = "ddogccatppig"
def count_pairs(test):
counter = 0
for i in range(0,len(test)-1):
if test[i] == test[i+1]
counter+=1
return counter
print(count_pairs(test))
Here you iterate through the length of the string (minus 1 because otherwise you will get an index out of bounds exception). Add to a counter if the letter is the same as the one in front and return it.
This is another (similar) way to get the same answer.
def charPairs(word):
word = list(word)
count = 0
for n in range(0, len(word)-1):
if word[n] == word[n+1]:
count +=1
return count
print(charPairs("ddogccatppig"))
print(charPairs("a"))
print(charPairs("dogcatpig"))
print(charPairs("aaaa"))
print(charPairs("AAAAAAAAAA"))

Return Alternating Letters With the Same Length From two Strings

there was a similar question asked on here but they wanted the remaining letters returned if one word was longer. I'm trying to return the same number of characters for both strings.
Here's my code:
def one_each(st, dum):
total = ""
for i in (st, dm):
total += i
return total
x = one_each("bofa", "BOFAAAA")
print(x)
It doesn't work but I'm trying to get this desired output:
>>>bBoOfFaA
How would I go about solving this? Thank you!
str.join with zip is possible, since zip only iterates pairwise up to the shortest iterable. You can combine with itertools.chain to flatten an iterable of tuples:
from itertools import chain
def one_each(st, dum):
return ''.join(chain.from_iterable(zip(st, dum)))
x = one_each("bofa", "BOFAAAA")
print(x)
bBoOfFaA
I'd probably do something like this
s1 = "abc"
s2 = "123"
ret = "".join(a+b for a,b in zip(s1, s2))
print (ret)
Here's a short way of doing it.
def one_each(short, long):
if len(short) > len(long):
short, long = long, short # Swap if the input is in incorrect order
index = 0
new_string = ""
for character in short:
new_string += character + long[index]
index += 1
return new_string
x = one_each("bofa", "BOFAAAA") # returns bBoOfFaA
print(x)
It might show wrong results when you enter x = one_each("abcdefghij", "ABCD") i.e when the small letters are longer than capital letters, but that can be easily fixed if you alter the case of each letter of the output.

How to compare words in two lists according to same order in python?(I have def a function)

Recently, I def a function which can compare two words in each wordlist. However, I also found some problems here.
def printcorrectletters():
x=0
for letters in correctanswer:
for letters2 in userinput:
if letters == letters2:
x = x+1
break
return x
In this function, if the correctanswer='HUNTING', and I input 'GHUNTIN', it will show 6 letters are correct. However, I want it compare words' letters 1 by 1. So, it should march 0. For example, 'H' will match first letter of userinput.. and so on.
I also think another function which can solve it by using 'zip'. However, our TA ask me to finish it without things like 'zip'.
If the strings are different lengths, you want to compare each letter of the shorter string:
shortest_length = min(len(correctanswer), len(userinput))
min just gives you the minimum of two or more values. You could code it yourself as:
def min(a, b):
return a if a < b else b
You can index a character in a string, using [index]:
>>> 'Guanfong'[3]
n
So you can loop over all the letter indices:
correct = 0
for index in range(shortest_length):
if correctanswer[index] == userinput[index]:
correct += 1
If you did use zip and sum:
correct = sum(1 for (correct_char, user_char) in zip(correctanswer, userinput)
if correct_char == user_char)
Python provides great facilities for simplifying ideas and for communicating with the computer and programmers (including yourself, tomorrow).
Without zip you can use enumerate() to loop over elements of correctanswer , and get index and element at the same time. Example -
def printcorrectletters():
x=0
for i, letter in enumerate(correctanswer):
if i < len(userinput) and letter == userinput[i]:
x = x+1
return x
Or if even enumerate() is not allowed, simply use range() loop till len(correctanswer) and get elements from each index.

Counting the number of times a character in string1 is found in string2?

I'm trying to create a function which takes two strings and then returns the sum total of how many times every character in the first string is found in the second string under the condition that duplicate characters in the first are ignored.
e.g. search_counter('aabbaa','a') would mean a count of 1 since the the second string only has one a and no bs and we only want to search for a once despite there being four as.
Here's my attempt so far:
def search_counter(search_string, searchme):
count = 0
for x in search_string:
for y in searchme:
if x == y:
count = count + 1
return count
The problem with my example is that there is no check to ignore duplicate characters in search_string.
So instead of getting search_counter('aaa','a') = 1 I get 3.
for x in search_string:
You can get a list of characters without duplicates by converting the string to a set.
for x in set(search_string):
You can eliminate repetitions from a string by transforming it into a set:
>>> set("asdddd")
set(['a', 's', 'd'])
Preprocess your string this way, and the rest of the algorithm will remain the same (set objects are iterables, just like strings)
You can use iteration to do this
def search_counter(string, search):
count = 0
for i in range(len(string)):
count += string[i:].startswith(search)
return count
Or this one-liner
search_counter = lambda string, search: sum([string[i:].startswith(search) for i in range(len(string))])

python count repeating characters in a string by using dictionary function

I have a string
string = 'AAA'
When using the string.count('A') the output is equal to 3
and if it is string.count('AA') the output is equal to 1
However, there are 2 'AA's in the string.
Is there any method to count repeated string like above by using dictionary function?
I'd like to hear your helpful suggestions.
Thank you all in advance.
The problem is Count return the number of (non-overlapping) occurrences of substring sub in string.
try this as you can see at this post:
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count+=1
else:
return count
Alternative for "not huge" strings
>>> s, sub = 'AAA', 'AA'
>>> sum(s[x:].startswith(sub) for x in range(len(s)))
2
I find this a little more readable.
Yeah! You can use a dictionary
def count(input):
a = {}
for i in input:
a[i] = a.get(i,0)+ 1
return a
print(count('AA')) #Would return 2

Categories