Python Forum
is any character in s1 in s2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is any character in s1 in s2
#1
i wrote a function to find if any character in string one is in string two. is there anything in Python to do this without that character loop or even better, without making a function?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Use sets.
Reply
#3
perhaps sets makes the logic work better. but i get strings as my data source. how do you suggest that i use sets?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Aug-26-2024, 01:57 AM)Skaperen Wrote: perhaps sets makes the logic work better. but i get strings as my data source. how do you suggest that i use sets?

>>> string1 = "some characters"
>>> string2 = "other characters"
>>> string3 = "bwgfz"
>>> bool(set(string1) & set(string2))
True
>>> bool(set(string1) & set(string3))
False
Skaperen likes this post
Reply
#5
Just add the strings, then do the magic? I presumed you want to keep the common letters.

from string import ascii_letters

string1 = ascii_letters[0:10]
string2 = ascii_letters[5:15]
string3 = string1 + string2
# if there is overlap, len(res) will be < len(string3), (< 20 here)
res = ''.join(sorted(list(set(string3))))
len(res) # 15
Reply
#6
OP - Back to your original question, are you wanting to know IF there are duplicate characters (yes/no which is well answered here) or WHAT characters are duplicated?
Reply
#7
(Aug-26-2024, 02:50 PM)jefsummers Wrote: OP - Back to your original question, are you wanting to know IF there are duplicate characters (yes/no which is well answered here) or WHAT characters are duplicated?

i want to know IF ... if i recall the project, now.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 6,938 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 4,194 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 3,597 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 8,555 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 20,459 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020