Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在列表中换行?_Python_Python 3.x - Fatal编程技术网

Python 如何在列表中换行?

Python 如何在列表中换行?,python,python-3.x,Python,Python 3.x,这是我的密码: user_input = input("Enter a saying or poem: ") words_list = user_input.split() words_list_length = len(words_list) def word_mixer(words_list): words_list.sort() final_list = [] if(len(words_list) <= 5): final_li

这是我的密码:

user_input = input("Enter a saying or poem: ")

words_list = user_input.split()

words_list_length = len(words_list)


def word_mixer(words_list):

    words_list.sort()

    final_list = []

    if(len(words_list) <= 5):

        final_list = words_list 


    else:

        while(len(words_list) > 5):

            first_word_popped = words_list.pop(-5)

            final_list.append(first_word_popped)

            second_word_popped = words_list.pop(0)

            final_list.append(second_word_popped)

            third_word_popped = words_list.pop(-1)

            final_list.append(third_word_popped) 

    return final_list
如您所见,字符串已排序,每行应打印3个单词(当然,如果我在代码中添加了打印语句)。但我找不到办法

这个练习来自我在edx.org页面上学习的Python课程。在这里,他们给出了以下输入/输出示例:


忽略大写/小写的单词。但正如您所看到的,通过在列表中添加“\n”,列表将以3乘3的方式打印。我该怎么做呢?

所以你需要每三个单词加一行,这很简单

LIST = ["or", "brushed", "thy", "not", "little", "though", "me?", "summers?", "thee?"]
# Insert the new line
for i in range(len(LIST)):
    if i%3==0 and i>0:
        LIST.insert(i, "\n")
# Join the sentence
para = " ".join(LIST)
# Print the result
for i in para.split("\n"):
    print(i.strip())
你会得到的

or brushed thy
not little
though me? summers? thee?

如果您只想打印,您可以在循环内部打印,循环末尾带有“\n”。但是,如果您想将其存储为一种可以单独使用每组单词的格式,那么您可以执行以下操作:

words_list = ["Hello", "there", "this", "is", "a", "string", "input", "given", "by", "the", "user"]
final_list_2 = list()
while(len(words_list) > 5):
    final_list = list()
    first_word_popped = words_list.pop(-5)
    final_list.append(first_word_popped)
    second_word_popped = words_list.pop(0)
    final_list.append(second_word_popped)
    third_word_popped = words_list.pop(-1)
    final_list.append(third_word_popped) 
    final_list_2.append(final_list)

for final_list in final_list_2:
    print (" ".join(final_list))
输出:

输入Hello用户

a在那里

请添加一些预期的输入和输出,因为在我看来可能存在一个更简单的解决方案。我看到您已经扩展了代码,但您还没有编写一个输入/输出示例。请添加预期的输入和输出。对此我很抱歉。我就这么做了。我希望这能帮助你理解。如果没有,请让我知道,我将尝试用另一种方式解释。您可以添加更多的输入/输出示例吗?删除某些单词的预期规则是什么?更喜欢文本而不是图像,因为后者更难处理,但您会将“\n”确切地放在上面代码示例中最终列表2的每个元素之后。我在
的final_list
循环中有一个打印,它会自动在换行符上打印每个列表。哦,我明白了,我知道这很愚蠢,但我就是找不到正确的位置来放置它。谢谢
words_list = ["Hello", "there", "this", "is", "a", "string", "input", "given", "by", "the", "user"]
final_list_2 = list()
while(len(words_list) > 5):
    final_list = list()
    first_word_popped = words_list.pop(-5)
    final_list.append(first_word_popped)
    second_word_popped = words_list.pop(0)
    final_list.append(second_word_popped)
    third_word_popped = words_list.pop(-1)
    final_list.append(third_word_popped) 
    final_list_2.append(final_list)

for final_list in final_list_2:
    print (" ".join(final_list))