为什么当我在第 4 行中放入方括号时输出发生变化?def x(a, b): d = [] for i in range(0, a): d += str(i) + b return dc = x(3, "7")print(c)输出:['0', '7', '1', '7', '2', '7']def x(a, b): d = [] for i in range(0, a): d += [str(i) + b] return dc = x(3, "7")print(c)输出:['07', '17', '27']为什么输出会这样改变?
1 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
这很简单,
d += 'a' + 'b' # is same as [] + list('ab') = ['a', 'b']
相似地
d += ['a' + 'b'] # is same as [] + ['ab'] = ['ab']
当您键入将字符串转换为列表时,它会创建一个包含单个字符的列表作为列表的元素。
例如:
a = "Good" b = list(a) print(b) # will print as --> ['G', 'o', 'o', 'd']
添加回答
举报
0/150
提交
取消