python list相加
时间: 2023-08-07 11:01:37 浏览: 58
在Python中,你可以使用"+"运算符来将两个列表相加。这将会创建一个新的列表,其中包含了两个原始列表的所有元素。
下面是一个示例代码:
```python
list1 = [1, 2, 3]
list2 = [4, 5, 6]
result = list1 + list2
print(result)
```
输出结果将会是:[1, 2, 3, 4, 5, 6],其中包含了两个列表的所有元素。
相关问题
python list对应元素相加
Python中可以使用循环和条件语句来查找列表中相同的元素。具体方法如下:
1. 定义一个列表,例如:lst = [1, 2, 3, 4, 5, 2, 3, 6, 7, 8, 9, 2, 3]
2. 定义一个空列表,用于存储相同的元素,例如:same_lst = []
3. 使用for循环遍历列表lst中的每个元素,判断该元素是否在same_lst中,如果不在,则继续判断lst中是否有相同的元素,如果有,则将该元素添加到same_lst中,例如:
for i in lst:
if i not in same_lst:
if lst.count(i) > 1:
same_lst.append(i)
4. 最后输出same_lst即可,例如:print(same_lst)
以上就是Python中查找列表中相同元素的方法。
python 两个list相加
可以使用"+"运算符将两个列表相加,这将返回一个新的列表,其中包含两个列表中的所有元素。例如:
```
list1 = [1, 2, 3]
list2 = [4, 5, 6]
new_list = list1 + list2
print(new_list)
```
输出结果为:
```
[1, 2, 3, 4, 5, 6]
```