要将一个list的字符串连接起来显示,要求如下:
Create a function that concatenates strings.
join_strings
accepts an argument calledwords
. It will be a list.result
and set it to""
, an empty string.words
list and append each word toresult
.return
the result
.Don't add spaces between the joined strings!
被要求的第三条的“append”误导了,写了这么一个程序:n = ["Michael", "Lieberman"] # Add your function here def join_strings(words): result="" for word in words: result.append(word) return result print join_strings(n)
Traceback (most recent call last): File "python", line 11, in <module> File "python", line 6, in join_strings AttributeError: 'str' object has no attribute 'append'
改了一下程序:
n = ["Michael", "Lieberman"] # Add your function here def join_strings(words): result="" for word in words: result+=word return result print join_strings(n)
MichaelLieberman
append是用于list的,比如将两个list的数据连起来:
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]] # Add your function here def flatten(lists): results=[] for numbers in lists: for number in numbers: results.append(number) return results print flatten(n)
[1, 2, 3, 4, 5, 6, 7, 8, 9]