【问题标题】:Python - how to remove commas, square brackets and quotation marks from list [duplicate]Python - 如何从列表中删除逗号、方括号和引号 [重复]
【发布时间】:2016-04-29 00:49:06
【问题描述】:

如何从这个结果中删除引号、逗号和括号:

    encrypt = input("enter your string: ")
    encrypt = encrypt.replace(" ","")
    encrypt_list = [encrypt[i:i+5] for i in range(0, len(encrypt), 5)]
    print (encrypt)
    print (encrypt_list)

如果输入是:5 blocks of text test

输出为:['5bloc', 'ksoft', 'extte', 'st']

我需要它是:5bloc ksoft extte st

【问题讨论】:

  • 我不确定我是否得到了这个问题。您是否知道您有一个字符串列表并且这些字符串都不包含逗号、方括号或引号?您的预期结果是语法错误。你的意思是字符串'5bloc ksoft extte st'
  • 简单地说:print(*encrypt_list).
  • 知道基本类型是什么样子的就是基础研究。提问前请多花点功夫。
  • dataToWrite = ' '.join(map(str, vals))

标签: python listview


【解决方案1】:

你可以使用str.join,像这样:

>>> s = ' '.join(['5bloc', 'ksoft', 'extte', 'st'])
>>> print(s)
5bloc ksoft extte st

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多