【问题标题】:How can I delete the letter that occurs in the two strings using python?如何使用 python 删除两个字符串中出现的字母?
【发布时间】:2015-09-22 05:08:20
【问题描述】:

这是源代码:

def revers_e(str_one,str_two):
    for i in range(len(str_one)):
        for j in range(len(str_two)):
            if str_one[i] == str_two[j]:
               str_one = (str_one - str_one[i]).split()
               print(str_one) 
            else:
               print('There is no relation')  

if __name__ == '__main__':
str_one = input('Put your First String: ').split()
str_two = input('Put your Second String: ')
print(revers_e(str_one, str_two))

如何从第一个字符串中删除两个字符串中出现的字母然后打印它?

【问题讨论】:

  • ''.join(letter for letter in str_one if letter not in str_two)

标签: string python-3.x slice


【解决方案1】:

一个简单的pythonic方法怎么样

def revers_e(s1, s2):
    print(*[i for i in s1 if i in s2])    # Print all characters to be deleted from s1
    s1 = ''.join([i for i in s1 if i not in s2])    # Delete them from s1

This answer 说,“Python 字符串是不可变的(即它们不能被修改)。这有很多原因。使用列表直到你别无选择,然后才将它们转换为字符串。”

【讨论】:

【解决方案2】:

首先,由于字符串是可迭代的,因此您无需使用非常次优的方式使用 rangelen 来迭代字符串,您可以通过简单的循环对其进行迭代。

为了在 2 个字符串中查找交集,您可以使用 set.intersection 返回两个字符串中的所有常见字符,然后使用 str.translate 删除您的常见字符

intersect=set(str_one).intersection(str_two)

trans_table = dict.fromkeys(map(ord, intersect), None)
str_one.translate(trans_table)

【讨论】:

  • 我正在使用 Python 3.4 & intersect 不再是内置的
  • @KnoxRoot 对不起intersection
  • @poke 这实际上是次优的!
  • @KnoxRoot 是。 set('bar').intersection('baz') 适用于 3.4。
  • @KnoxRoot 你怎么说?这是在 python 3.4 >>> {'s','e'}.intersection('sr') {'s'}
【解决方案3】:
def revers_e(str_one,str_two):
    for i in range(len(str_one)):
        for j in range(len(str_two)):
          try:

            if str_one[i] == str_two[j]:
               first_part=str_one[0:i]
               second_part=str_one[i+1:]
               str_one =first_part+second_part
               print(str_one)

            else:
               print('There is no relation')

          except IndexError:
                return


str_one = input('Put your First String: ')
str_two = input('Put your Second String: ')
revers_e(str_one, str_two)

我已经修改了你的代码,去掉了一些,又增加了一些。

str_one = input('Put your First String: ').split()

我删除了.split(),因为所有这些都会创建一个长度为 1 的列表,因此在您的循环中,您会将第一个字符串的整个字符串与第二个字符串的一个字母进行比较。

  str_one = (str_one - str_one[i]).split()

您不能在 Python 中从这样的字符串中删除字符,因此我将字符串拆分为多个部分(您也可以将它们转换为列表,就像我在我删除的其他代码中所做的那样),其中所有字符最多包括匹配字符之前的最后一个字符,然后是匹配字符之后的所有字符,然后将它们附加到一个字符串中。

我使用了异常语句,因为第一个循环将使用原始长度,但这可能会发生变化,因此可能会导致错误。

最后,我只是调用了函数而不是打印它,因为它所做的只是返回一个None 类型。

【讨论】:

  • 它总是返回in
【解决方案4】:

这些适用于 Python 2.7+ 和 Python 3

给定:

>>> s1='abcdefg'
>>> s2='efghijk'

你可以使用一个集合:

>>> set(s1).intersection(s2)
{'f', 'e', 'g'}

然后使用maketrans 中的那个集合来制作一个到None 的转换表来删除这些字符:

>>> s1.translate(str.maketrans({e:None for e in set(s1).intersection(s2)}))
'abcd'

或者使用列表推导:

>>> ''.join([e for e in s1 if e in s2])
'efg'

还有一个正则表达式来生成一个没有常见字符的新字符串:

>>> re.sub(''.join([e for e in s1 if e in s2]), '', s1)
'abcd'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2021-06-05
    • 2023-02-16
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多