【问题标题】:how can i remove ' from my output in python [duplicate]如何从 python 的输出中删除 ' [重复]
【发布时间】:2021-10-03 14:50:29
【问题描述】:
msg1 = "   Facebook already uses AI to Filter Fake stories from the feeds of users "  
Desired output = [Facebook , already, uses, AI, to, Filter, Fake, stories, from, the, feeds, of, users ]​

我的代码:

msg1 = "  Facebook already uses AI to Filter Fake stories from the feeds of users"
print(msg1.split())

Actual output = ['Facebook', 'already', 'uses', 'AI', 'to', 'Filter', 'Fake', 'stories', 'from', 'the', 'feeds', 'of', 'users']

我刚刚开始使用 python。我可能使用了错误的功能或遗漏了从输出中删除 ' 的任何内容。 我怎样才能实现这个简单的目标。

【问题讨论】:

  • 既然你是 Python 新手,为什么你(认为)你需要 [Facebook , already, uses,... 作为你想要的输出,而不是 ['Facebook', 'already', 'uses'...?有关解释,请参阅链接副本的第一个答案。您意识到“期望的输出”版本不是列表吗?

标签: python split


【解决方案1】:
msg1 = "  Facebook already uses AI to Filter Fake stories from the feeds of users"
output = str(msg1.split()).replace("'", "")
print(output)

【讨论】:

    【解决方案2】:

    这应该可以工作

    msg1 = "   Facebook already uses AI to Filter Fake stories from the feeds of users ".split()
    print(*msg1, sep=" ")
    

    您可以修改 sep 以便您可以将所需的字符放在每个元素之间的空格中。 sep 的默认值为 " "。

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 2015-09-26
      • 2018-12-26
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多