【问题标题】:Python - Find number of characters that are found in both string1 and string2Python - 查找在 string1 和 string2 中找到的字符数
【发布时间】:2014-08-01 01:51:05
【问题描述】:

我正在尝试查找在第二个字符串中也找到的第一个字符串中的字符数。这是我目前所拥有的:

def main():
    text1 = raw_input("Enter word 1: ")
    text2 = raw_input("Enter word 2: ")

    print("The number of characters that occur in both", text1, "and", text2, "is", count)

def count(text1, text2):
    char = 0
    for i in text1:
        for j in text2:
            if i == j:
                char += 1

    return char

main()

我在使用 def count() 时遇到问题。我不确定如何计算 text1 和 text2 中有多少不会重复的字母。任何帮助表示赞赏!

【问题讨论】:

  • 所以你想找出出现在两者中的唯一字符的数量,没有重复?

标签: python


【解决方案1】:

使用set.intersection:

(set(text1).intersection(item2))

In [22]: len(set("bana").intersection("banana"))
Out[22]: 3

intersection() 将接受任何可迭代作为参数。

def count(text1, text2):
    return len(set(text1).intersection(text2))
In [24]: count("foo","foobar")
Out[24]: 2

如果你想要重复:

def count(text1, text2):
    s = set(text2)
    return len([x for x in text1 if  x in s])
In [29]: count("foo","foobar")
Out[29]: 3

In [30]: count("bana","banana")
Out[30]: 4

【讨论】:

  • 我在这里挑剔,find the number of characters in the first string that are also found in the second string,从技术上讲,答案不应该是 4? ;-)
  • @thefourtheye 如果 OP 想要重复,这还没有说清楚。
  • @thefourtheye 我想这取决于你如何解释这个问题,但无论哪种方式,对集合 a 的介绍都不会造成任何伤害。
【解决方案2】:

主要问题是,您没有调用count 函数,而是打印count 函数对象。应该是的

print("The number of....", text2, "is", count(text1, text2))

此外,如果在第二个字符串中找到该字母,则您需要增加计数器,然后您可以跳到第一个字符串中的下一个字符。所以,你可以像这样跳出循环

for i in text1:
    for j in text2:
        if i == j:
            char += 1
            break

由于我们只考虑itext2中的第一次出现,我们可以检查它是否存在于text2中,像这样

for i in text1:
    if i in text2:
        char += 1

但请记住,它不会考虑原始字符串中的重复项。例如,aaaa 将产生计数 3

【讨论】:

  • 不要忘记函数参数。
  • 啊,我错过了 break 并调用了 count()!现在可以使用了 :) 非常感谢!
【解决方案3】:

您可以通过交叉字符串(获取 2 个字符串中的唯一字符)并获取结果的长度来做到这一点。像这样的:

def strIntersection(s1, s2):
   out = ""
   for c in s1:
     if c in s2 and not c in out:
       out += c
   return out



>>> strIntersection('asdfasdfasfd' , 'qazwsxedc')
'asd'

len(strIntersection('asdfasdfasfd' , 'qazwsxedc'))
3

可能是你的计数函数。

(另外,你需要在 print 语句中调用 count(text1, text2) 而不仅仅是 conut,否则它不会调用该函数)

【讨论】:

    【解决方案4】:

    这将是使用 set 的好时机,它会自动消除重复并允许即时查找。

    def count(text1, text2):
      numchars = 0
      text1 = set(list(text1))
      text2 = set(list(text2))
      for char in text1:
        if char in text2:
          numchars += 1
    
      return numchars
    

    通过将字符串转换为列表,然后转换为集合,您将消除每个字符串中的所有重复字符(不过,仅在函数中,字符串是可以的)。然后您可以检查第一个集合中的每个字符,看看它是否在第二个集合中,这是一个固定时间检查。

    另外注意:如果您不想将大写的A 和小写的a 算作两个不同的字符,请改用text1 = set(list(text1.lower()))

    此外,您没有正确调用该函数。您应该使用count(text1,text2) 而不仅仅是count。虽然如果你要打印出来,你需要使用str(count(text1,text2)),因为count() 函数返回一个整数,你不能只用字符串连接它。

    【讨论】:

    • 我收到此错误:('同时出现的字符数','anna','and','banana','is',
    • @annabananana7 哎呀,我重载了其中一个变量。您还需要使用count() 而不是count
    • 我现在遇到类型错误:TypeError: cannot concatenate 'str' and 'int' objects
    • @annabananana7 啊,是的,这会发生。 count() 返回一个int,您需要将其转换为字符串。使用str(count(text1,text2))调用函数。
    • 实际上,有人使用了我的代码并添加了break,这使它工作起来很简单,哈哈,我想不通。感谢您的帮助!
    【解决方案5】:

    我不确定这是否是您要查找的内容,但是如果您需要将 text1 和 text2 中的字母总数相加并打印结果,那很容易做到。请看下面:

    text1 = raw_input("Enter word 1: ")
    text2 = raw_input("Enter word 2: ")
    
    x = len(text1) + len(text2)
    
    print "The number of characters that occur in both text1 and text2 is " + str(x)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多