gpt4 book ai didi

python - 在 Python (2.7.3) 中,如果 str(x) 中的任何字符在 str(y) 中(或 str(y) 在 str(x) 中),我如何编写一个函数来回答?

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:03 26 4
gpt4 key购买 nike

def char_check(x,y):

if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):

return True

else:

return False

print "You will enter two words that you think use some of the same letters."

x = raw_input('Enter one of the words: ')

y = raw_input('Enter the other word: ')

print char_check(x,y)

我想做的是输入两个字符串,例如 str(x) 的“terrible”和 str(y) 的“bile”,然后返回“True”,因为字符 'b'、'i'、' l' 和 'e' 由两个字符串共享。

我是新手,正在努力学习,但我似乎无法独自解决这个问题。谢谢大家。

最佳答案

集合几乎肯定是可行的方法。

>>> set1 = set("terrible")
>>> set2 = set("bile")
>>> set1.issubset(set2)
False
>>> set2.issubset(set1) # "bile" is a subset of "terrible"
True
>>> bool(set1 & set2) # at least 1 character in set1 is also in set2
True

关于python - 在 Python (2.7.3) 中,如果 str(x) 中的任何字符在 str(y) 中(或 str(y) 在 str(x) 中),我如何编写一个函数来回答?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com