sách gpt4 ai đã đi

Trong Python (2.7.3), làm thế nào tôi có thể viết một hàm để trả lời xem bất kỳ ký tự nào trong str(x) có nằm trong str(y) hay không (hoặc nếu str(y) có nằm trong str(x))?

In lại 作者:太空宇宙 更新时间:2023-11-03 13:46:03 28 4
mua khóa gpt4 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

khác:

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' 由两个字符串共享。

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

1 Câu trả lời

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

>>> set1 = set("terrible")
>>> set2 = set("bile")
>>> set1.issubset(set2)
SAI
>>> set2.issubset(set1) # "bile" is a subset of "terrible"
ĐÚNG VẬY
>>> bool(set1 & set2) # at least 1 character in set1 is also in set2
ĐÚNG VẬY

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

28 4 0
Bài viết được đề xuất: python - 使用 enaml 时如何获取按键事件?
Bài viết được đề xuất: java - 机器人 : adding SearchView inside a Custom ListView with Custom Adapter
Bài viết được đề xuất: java - RxJava 2 在新线程中创建列表
Bài viết được đề xuất: python - Python 中的 BMI 计算器
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com