简体   繁体   English

Python:格式化列表(包含列表)以进行打印

[英]Python: Formatting a list (which contains a list) for printing

I'm working on a project that translates input to Pig Latin (yeah, I'm sure you've never seen this one before...) and having trouble formatting my output. 我正在开发一个项目,将输入转换为Pig Latin(是的,我确定你以前从未见过这个 ......)并且无法格式化我的输出。

(for the following, sentence = a list holding user input (phrase), split by phrase.split() ) (对于以下,句子=一个包含用户输入(短语)的列表,由phrase.split()分割)

sentence.remove(split)
final = map(str,sentence)
print "Final is (before formatting:", final
final = [sentence[0].capitalize()] , sentence[1:]
#finalFormat = ' '.join(final)
print "Final is", str(final).strip('[]')
#print "FinalFormat is", finalFormat
print "In Pig Latin, you said \"", ' '.join(map(str, final)), "\". Oink oink!"

What I get is: "In Pig Latin, you said "['Firstword'] ['secondword', 'thirdword'] " 我得到的是:“在Pig Latin中,你说”['Firstword'] ['secondword','thirdword']“

What I am looking for is: "In Pig Latin, you said "Firstword secondword thirdword." 我正在寻找的是:“在Pig Latin中,你说过”Firstword secondword thirdword“。

Based on my debug print statements it looks like my problem is still on the line (5 from the bottom): 基于我的调试打印语句,看起来我的问题仍在线上(从底部开始):

    final = [sentence[0].capitalize()] , sentence[1:]

Thanks in advance! 提前致谢!

Change this line: 改变这一行:

final = sentence[0].capitalize() , sentence[1:]

To this: 对此:

final = [sentence[0].capitalize()] + sentence[1:]

You were mapping a tuple of a string and a list, to strings, rather than a list. 您将字符串和列表的元组映射到字符串,而不是列表。

Note: using 'single"' quotes here will avoid "this\\"" ugliness. 注意:在这里使用'single"'引号将避免"this\\""丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM