如何从列表中的字符串中删除字母?

时间:2019-01-27 22:45:08

标签: python

我必须解决以下问题: -编写一个程序,将文本作为条目,并打印出不带字母“ r”的相同文本。老师说,我们必须使用列表将所有单词都放入其中,然后删除字母“ r”从他们。这是我的开始方式:

 text = input("Enter some text: ")
 l=text.split(" ")
 for i in range(len(l)): #I want to use for to run the elements of the list

我不知道下一步该怎么做,应该使用哪种方法,我认为方法remove()可能有用,但是我不知道如何在列表的每个项目中使用它。

2 个答案:

答案 0 :(得分:2)

欢迎来到

这是我的版本。

fetchTokenAuthA = async () => {
  const redirect_uri = AuthSession.getRedirectUrl()
  const responseA = await AuthSession.startAsync({
    authUrl:
      `https://slack.com/oauth/authorize?` +
      `&client_id=${client_id}` +
      `&scope=admin` +
      `&redirect_uri=${encodeURIComponent(redirect_uri)}`
  })
  const code = responseA.params.code
  const responseB = await AuthSession.startAsync({
    authUrl:
      `https://slack.com/api/oauth.access?` +
      `&client_id=${client_id}` +
      `&client_secret=${client_secret}` +
      `&code=${encodeURIComponent(code)}` +
      `&redirect_uri=${encodeURIComponent(redirect_uri)}`
  })
  this.setState({ responseB })
}

首先-它从输入文本中获取所有单词作为列表。 然后遍历所有单词,如果有“ r”,则将其从单词中删除并更新列表。在每一端,它都会从列表中产生回信。

请注意-这是超级冗长的代码。它不使用诸如列表理解之类的Python功能,但更易于理解。我故意添加了调试语句。

请告诉我这是否适合您。

谢谢

答案 1 :(得分:0)

对于列表:str.split()将给出由空格包围的每个项目的列表(您可以指定任意分隔符)

让我们从一个单独的列表项开始...我们将使用“过山车”。

我们可以使用in关键字检查子字符串是否包含在较大的字符串中。

text = input("Enter some text: ")
result = ""
for ch in text.lower():
    if ch == 'r':
        continue
    else:
        result += ch
print(result)

返回

Enter some text: rollercoster
ollecoste

Process finished with exit code 0