python - 在 python 3 中水平而不是垂直显示列表结果

标签 python python-3.x jupyter-notebook

我在 python 中创建了一个函数来计算单词的长度,同时忽略标点符号。例如,如果我有这句话:“今天没有 3 次停电吗? 结果应该如下:[6,5,4,1,9,5] 我可以使用我创建的以下函数获得这些结果:

import string
def word_length_list(given_string):
        clean_string = given_string.translate(str.maketrans('','',string.punctuation))
        word_lenght_list = list(map(len, clean_string.split()))
        return word_lenght_list
    given_string = "Haven't there been 3 blackouts today?"
    word_length_list(given_string)

这个函数给了我预期的结果:[6, 5, 4, 1, 9, 5] 但是,如果我给它一个很长的字符串,例如:

given_string = "Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function.Testing the output of the function."

它以下列方式给我结果:

[7,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 15,
 3,
 6,
 2,
 3,
 8]

有没有人知道如何让我的列表水平返回短字符串和长字符串的结果?任何建议将不胜感激。

我正在使用 jupyter notebook 将代码作为 .ipynb 运行

最佳答案

在一个单独的单元格中运行命令 %pprint

这会关闭 pretty-print 。该列表现在将水平显示。

关于python - 在 python 3 中水平而不是垂直显示列表结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46800269/

相关文章:

python-3.x - 如何搜索 jupyter notebook python 文件?

python - 如何将控制台脚本/入口点添加到调用类函数 python 的 setup.py

python - 为什么我使用 rstrip() 得到这些结果?

用 return 结束函数时加载 JSON 时出现 Python 错误

python-3.x - 带有MSG_DONTWAIT的Python socket.recv

python-3.x - 导入错误: No module named 'mstamp_stomp'

python - 使用 Vim 编辑 python 时出错

Python 3 按与特定模式匹配的名称过滤目录

git - 如何 git 忽略存储库中任何位置的 ipython 笔记本检查点

python-3.x - 如何修复 'C extension not loaded, training will be slow. Install a C compiler and reinstall gensim for fast training.'