好的,基本上我需要检查一个字符串是否与另一个字符串具有相同的字母。这两个字符串都是通过 input() 获得的。
我不想重复检查一个字母是否在另一个字符串中,所以如果我已经检查过该字母,我想跳到下一个字母。
我现在的代码是这样的:
str1, str2 = list(input()), list(input())
if len(str1) > len(str2):
str = str1
else:
str = str2
for x in str:
c = 0
if x in str2:
c += 1
if c != 0:
print("Both have the same letters!")
else:
print("Nope there are some letters missing..")
我不知道是否应该使用列表而不是计数器,如果能提供详细的解决方案或高质量的指导,将不胜感激! <3
str1 ='foo',str2 ='fbar'
。输出结果为**Both have the same letters!**。一种方法是将两个字符串的字母分别存储在两个不同的集合中,并检查这两个集合是否相等。 - Mahesh