检查一个字符串的所有字母是否都包含在另一个字符串中

4

好的,基本上我需要检查一个字符串是否与另一个字符串具有相同的字母。这两个字符串都是通过 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
4个回答

7

将字符串转换为单个符号的集合可以去除重复的符号,因此我们可以简单地进行比较:

if set(str1) == set(str2):
    print("Both have the same letters!") 
else:
    print("Nope there are some letters missing..")

注意:

由于集合中元素的顺序不重要,我们甚至可以进行比较,例如:

if set(str1) <= set(str2):        # <= means "is subset" in this context
    print("All symbols in str1 are in str2, too.")

或者
if set(str1) < set(str2):        # < means "is a proper subset" in this context
    print("All symbols in str1 are in str2, too, "
          "but str2 has at least 1 symbol not contained in str1.")

太完美了!非常感谢!如果我理解set()方法正确,它是用来创建字典的吗?如果问题比较蠢,请原谅,我正在逐步学习。我真的很感激大家的帮助! - Jesus A. Garrido Cordero
不,set()并不会创建一个字典,它创建的是一个集合,这是一种特殊类型的集合,对应于数学中的集合定义。例如:set("abcacab")将会是{"a", "b", "c"}(或者是{"c", "a", "b"},顺序并不重要)。而字典的一个例子可能是{1: "a", 2: "b", 3: "c"} - 它也被包含在相同的一对花括号({})中,但由键值对组成,用冒号(:)分隔。 - MarianD
好的,现在我完全理解了这个区别,非常感谢!你是一个伟大的人,教我这些而不求任何回报! - Jesus A. Garrido Cordero

0

代码中存在一些问题,即使str2比str1长,也总是使用str2进行比较。

for x in str:
    c = 0
    if x in str2:

下面,对于字符串中的每个字符,将c设置为0,而可以使用计数器来计算不在另一个字符串中的字符数量。
这样应该就可以完成任务了。
str1, str2 = list(input()), list(input()) 

if len(str1) > len(str2):
    str = str1
    compared_string = str2
else: 
    str = str2
    compared_string = str1

not_in_other_string = 0
for x in str:
    if x  not in compared_string:
        not_in_other_string += 1
if not_in_other_string == 0:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")

0

听起来你想找到一个字符串中的所有字符是否存在于另一个更大的字符串中。

你可以使用 unique = ''.join(set(substring)) 来获取子字符串中的字符列表,然后使用 列表推导式 获取更大字符串中所有字符。

这是我的示例:

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

现在你可以检查 not_in 是否为空,否则子字符串中有一个字符不在超级字符串中。

例如:

superstring = "hello"
substring = "xllo"

unique = ''.join(set(substring))
not_in = [char for char in unique if char not in superstring]

print not_in
>>>['x']

print not_in == []
>>>False

通常,在Python中做事情的首选方式是使用列表推导或使用一些内置函数,它已经为您检查字符串,而不是C样式,其中您需要通过循环显式地检查每个字母。这是Pythonic编程的惯用方式。
将列表推导分解开来,我们有一个像这样的循环:
   for char in unique: 
        if char not in superstring:
             char #char meets conditions so it is the value used in [char ... 

这里是我要做出的修改

str1, str2 = input(), input()

unique = ''.join(set(str1))
not_in = [char for char in unique if char not in str2]

if not_in == []:
    print("Both have the same letters!") 
else: 
    print("Nope there are some letters missing..")

0
str1, str2 = input().split()

if len(str1) > len(str2):
    string_to_check= str1
    string_from_which_to_check = str2
else: 
    string_to_check = str2

string_from_which_to_check = str1    
not_in_other_string  = set(string_to_check) - set(string_from_which_to_check)
if len(not_in_other_string )==0:
     print("character are present in string")
else:
    print("{} not present in string".format(str(not_in_other_string )))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接