0

So how would I go about finding a duplicate element of a string from another string in python using a for the most part one-to-two line or a quick fix?

for example,

str1 = "abccde"
str2 = "abcde"
# gets me c

Through the use of str2, finding there was a duplicate element in str1, so detecting that str1 has a duplicate of an element in str2. Not sure if there's a way through .count to do that, like str1.count(str2) or something.

I'm using this contextually for my hangman assignment and I'm a beginner coder, so we are using mostly built-in functions and the basics for the assignments, and there's a piece of my code within my loop that will keep printing because it dings the double letters.

Ex. hello, grinding, concoction.

So I pretty much made a "used" string, and I am trying to compare that to my correct letters list, and the guesses are 'appended' so I can avoid that.

note: they will be inputted, so I won't be able to say or just hardcode the letter c if that makes sense.

Thank you!

Paywolf
  • 11
  • 5
  • What happens if `str2` contains more dups like `abccccde` or letters do not exist in `str1` like `abcdez`? – Chris Nov 04 '19 at 01:16
  • 1
    Maybe you should consider a list or set instead of a string to keep track of guesses. It will make your task much easier! – PartialOrder Nov 04 '19 at 01:17
  • `if "c" in "abccde": print('detected "c",)` – furas Nov 04 '19 at 01:21
  • @PartialOrder how would I go about it if i wanted to do it in just lists? – Paywolf Nov 04 '19 at 01:25
  • @Chris detector will pretty much only care if str 1 has any duplicates of any char element in str 2 so `str1 = "abcdeffghi" str2 = "aaaaaabbbbbbcccccddeeeeefffffggghhiii"` and str1 would ding me the letter f – Paywolf Nov 04 '19 at 01:27
  • When user inputs a letter in your hangman game, rather than appending to a string, you can append to list, e.g., `somelist.append(letter)`. Then if you only want letters guessed excluding duplicates you can use `set(somelist)`. Easier, right? – PartialOrder Nov 04 '19 at 01:28

3 Answers3

0

You are basically searching a diff function between the two strings. Adapting this beautiful answer

import difflib

cases=[('abcccde', 'abcde')] 

for a,b in cases:     
    print('{} => {}'.format(a,b))  
    for i,s in enumerate(difflib.ndiff(a, b)):
        if s[0]==' ': continue
        elif s[0]=='-':
            print(u'The second string is missing the "{}" in position {} of the first string'.format(s[-1],i))
        elif s[0]=='+':
            print(u'The first string is missing the "{}" in position {} of the second string'.format(s[-1],i))    
    print() 

Output

abcccde => abcde
The second string is missing the "c" in position 3 of the first string
The second string is missing the "c" in position 4 of the first string
Dharman
  • 30,962
  • 25
  • 85
  • 135
Antonino
  • 3,178
  • 3
  • 24
  • 39
0

Using set with str.count:

def find_dup(str1, str2):
    return [i for i in set(str1) if str1.count(i) > 1 and i in set(str2)]

Output:

find_dup("abccde", "abcde")
# ['c']
find_dup("abcdeffghi" , "aaaaaabbbbbbcccccddeeeeefffffggghhiii") # from comment
# ['f']
Chris
  • 29,127
  • 3
  • 28
  • 51
0

My guess is that maybe you're trying to write a method similar to:

def duplicate_string(str1: str, str2: str) -> str:
    str2_set = set(str2)
    if len(str2_set) != len(str2):
        raise ValueError(f'{str2} has duplicate!')

    output = ''
    for char in str1:
        if char in str2_set:
            str2_set.remove(char)
        else:
            output += char

    return output


str1 = "abccccde"
str2 = "abcde"

print(duplicate_string(str1, str2))

Output

ccc

Here, we would first raise an error, if str2 itself had a duplicate. Then, we'd loop through str1, either remove a char from the str1_set or append the duplicate in an output string.

Emma
  • 27,428
  • 11
  • 44
  • 69