【问题标题】:Python - Remove brackets, commas and quotation marks in a representation of a listPython - 删除列表表示中的括号、逗号和引号
【发布时间】:2015-09-29 20:16:28
【问题描述】:

我查看了以前的条目,由于人们使用的代码,它们相对难以理解。我已经使用了 join 功能,但它不起作用。我正在尝试将列表列表加入单个字符串,输出应该有每个加入的子列表用空格分隔:

Five = 5

Converted = [chr(i) for i in addof]

#converts the numbers into letters of a not mentioned here variable(addof)

grouped = [converted[i:i+five] for i in range(0, len(converted), five)]

#groups the letters into groups of five

print("".join(map(str, grouped)))

#trying to use the join function in order to remove brackets etc. but it doesnt work

####################### OUTPUT ######################

['V', 'r', 'p', 'h', 'z']['k', 'h', 'u', 'h', 'l']['q', 'o', 'd', 'P', 'd']['q', 'f', 'k', 'd', '/']['l', 'q', 'd', 's', 'o']['d', 'f', 'h', 'z', 'k']['r', 'v', 'h', 'q', 'd']['p', 'h', 'L', 'g', 'r'] 

有人可以帮忙吗? 谢谢

【问题讨论】:

  • 括号是什么意思,它们是从哪里来的?
  • 在输出中你有方括号,但我不想要那些。
  • 你的意思是你有一个列表列表并且你想要一个列表或者究竟是什么?如果您添加原始结构,则更容易理解您想要的内容
  • 您意识到在列表上调用map(str 会给您一个列表的字符串表示形式吗?
  • 那行得通,非常感谢:)

标签: python join dictionary


【解决方案1】:

尝试:

[''.join(x) for x in grouped]

【讨论】:

  • 有效,但不会删除前端和末尾的方括号,也不会删除引号输出:['Vrphz', 'khuhl', 'qodPd', 'qfkd/', 'lqdso'、'dfhzk'、'rvhqd'、'phLgr'、'qrwfd'、'uhwru'、'hphpe'、'hu/dj'、'hqwoh'、'pdqol'、'yhgqr'、'worqj'、 'djr/r'、'qhriw'、'krvhz'、'krkdv'、'dodqf'、'hdqgd'、'qflhq'、'wvklh'、'ogrqd'、'vkhoi'、'dqgnh'、'hsvdv'、 'nlqq|', 'qdjdq', 'gdjuh', '|krxq', 'giruu', 'dflqj', '1']
  • 您需要逐行输出不带括号和引号的内容吗?尝试:for i in [''.join(x) for x in grouped]: print i
  • 感谢您的帮助,但我现在从 Padraic Cunningham 那里得到了解决方案 :)
猜你喜欢
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多