list - Finding a substring within a string using an array? -


trying head around programming, cannot work out why doesn't work? using "not" , "in" incorrectly?

i trying program print characters appear in both strings. correctly identifies them, can't print 1 set of characters if there more 1 occurrence.

a = input("string1 :") b = input("string2: ") list1 = []  in a:     j in b:         if == j , not in list1:             list1.append([i])             break  print(list1) 

for example if print strings "alexander" , "alex" print characters a, l, e, x, a, e

i know current method works if string1 inputted main string, interested why doesn't work.

the problem instead of adding common letter list1, adding new list containing common letter list1. @ end have list of lists. @ same time checking if single letter in list of lists, false.

you should add common letter "i" list using append:

a = input("string1 :") b = input("string2: ") list1 = []  in a:     j in b:         if == j , not in list1:             list1.append(i)             break  print(list1) 

you convert string , b sets , intersect them:

print set(a) & set(b) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -