#我的代码
def sec_soc_nam(friends):
for x in friends:
return "".join(x[0])
print(sec_soc_nam(["tina","andre",'suzie']))
#输出:t
#其他人的解决方案
def society_name(friends):
return ''.join(sorted(i[0] for i in friends))
#输出所需结果:“ast”
注意:忽略排序
迭代不起作用……只输出第一项。我检查了一下,但没有缺少任何组件
Tags:
您正在为每个循环返回项目,而不是加入整个列表
x[0]返回项目的第一个字母
只需打印(“.join([“tina”,“andre”,“suzie]”)即可加入列表
如果你这样写代码
您将只输出第一个字符串(“tina”)的第一个字符(x[0])
因为return指令基本上是告诉函数停止,返回第一个迭代值
只有在收集了所有开始字符后,才应执行返回
这正是另一个函数所做的
如果您仍然想使用join关键字
相关问题 更多 >
编程相关推荐