python - 如何将函数中的字符串转换为“垂直”或列显示?
问题描述
我是新手,这可能是一个新手问题。我正在使用 Python。所以这里是代码:
def student_list (names):
unique = [ ]
for name in names:
if name not in unique:
unique.append(name)
return unique
print(name)
def listToString(names):
str1 = " "
return (str1.join(names))
names = ["Adam", "Ben", "Aaron", "Clyde", "Alex", "Billy", "Chris", "Adam", "Clyde"]
names.sort()
print(listToString(student_list(names)))
该代码将导致:
亚伦亚当亚历克斯本比利克里斯克莱德
但我想要实现的是:
亚伦
亚当
亚历
克斯
本
比利克里斯克
莱德
还有一个附带问题,嵌套函数中是否可以有两个 for 循环?
谢谢你。
解决方案
只需像这样将列表写入字符串函数
def listToString(names):
str1 = "\n"
return (str1.join(names))
注意“\n”作为连接字符而不是“”。
要回答第二个问题,是的,一个函数或一个嵌套函数内部可以有任意数量的嵌套循环。
推荐阅读
- c# - 以下代码实际上是如何工作的?
- sql - 在 SQL Server 中转换 MS Access 更新查询
- amazon-web-services - 从 ECR 拉取镜像到 Kubernetes 部署文件
- python - 重新格式化从 html 中提取的 line/div
- vba - 防止 ms 访问表中的重复条目
- google-app-engine - 尝试使用 Sails Js 将 Gcloud App 引擎与 Cloud Sql 实例连接,但出现适配器错误
- angular - 如何动态过滤角度json文件?
- ios - 具有多个摄像头的无人机上的 shootPhotoDistanceInterval (DJIWaypoint) 摄像头源
- ruby-on-rails - 在 RoR API 中检查 JSON 字符串
- node.js - React Router 与生产环境中的 path = "/" 不匹配