我是新来的,这可能是一个新手问题。我正在使用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)))
代码将导致:
亚伦亚当亚历克斯本比利克里斯克莱德
但是我想实现的是:
亚伦
亚当·弗格森
本
比利克莱德Chris
还有一个附带问题,是否可能在嵌套函数中有两个for循环?
谢谢。
就像这样将列表写入字符串函数
def listToString(names):
str1 = "n"
return (str1.join(names))
注意"n"作为连接字符,而不是"。
回答第二个问题,是的,在函数或嵌套函数中可以有任意数量的嵌套循环。