python - 如何在 Python 3.x 中逐行打印关键字?
问题描述
在 Python 中,我们可以使用打印 35 个关键字print(keyword.kwlist)
结果将如下所示。
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
您能否告诉我,如何逐行打印关键字,如下所示?
('Total number of keywords ',
['and',
'as',
'assert',
'break',
'class',
'continue',
'def',
'del',
'elif',
'else',
'except',
'exec',
'finally',
'for',
'from',
'global',
'if',
'import',
'in',
'is',
'lambda',
'not',
'or',
'pass',
'print',
'raise',
'return',
'try',
'while',
'with',
'yield'])
解决方案
您可以使用列表解包,*
然后将其分开'\n'
key_list=['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
print(*key_list,sep='\n')
推荐阅读
- amazon-web-services - 从 AWS Lightsail 运行 jupyter 笔记本时出现问题
- python - 获取 Keras 模型输出 wrt 输入的梯度,但最后一层是 SVM
- python - 使用查询集将过滤器应用于嵌套的反向外键关系
- linux - 在 Linux 内核中,task_struct 是在哪里初始化的?
- java - 如何使用 MSAL4J 获取守护程序的令牌?
- java - 设置 TextView 颜色的最有效方法
- javascript - 如何使用 observables 来更新上下文提供者以用于提供者组件(React/Firebase)?
- bash - 为什么从 shell 脚本创建 conda 环境会导致脚本提前终止,我该如何解决这个问题?
- javascript - 为什么这个抓取返回未定义?
- c# - WPF - 为什么我的 UserControl 停止工作?