1. 直接相加
str1 = [1, 2, 3]
str2 = ['a', 'b', 'c']
str3 = ['z', 6, 'f']
str = str1 + str2 + str3
print(str)
结果为:[1, 2, 3, 'a', 'b', 'c', 'z', 6, 'f']
2. extend()方法
str1 = [1, 2, 3]
str2 = ['a', 'b', 'c']
str3 = ['z', 6, 'f']
str1.extend(str2)
str1.extend(str3)
print(str1)
结果为:[1, 2, 3, 'a', 'b', 'c', 'z', 6, 'f']
3. 列表
str1 = [1, 2, 3]
str2 = ['a', 'b', 'c']
str3 = ['z', 6, 'f']
str = [str1, str2, str3]
print(str)
out = [y for x in str for y in x]
print(out)
结果为:
[[1, 2, 3], ['a', 'b', 'c'], ['z', 6, 'f']]
[1, 2, 3, 'a', 'b', 'c', 'z', 6, 'f']
拓展练习:
[1]. 快乐的LeetCode — 寻找两个正序数组的中位数
[2]. 快乐的LeetCode — 合并两个有序数组
[3]. 快乐的LeetCode — 合并排序的数组