python, 在原有list中,list = [1,2,3,4,5,6,7,8],两个数据中间插入15个数据,并按顺序排列,结果如[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,....]
时间: 2023-08-19 07:26:03 浏览: 33
可以使用 Python 的列表推导式来实现:
```python
list = [1,2,3,4,5,6,7,8]
# 在 2 和 3 之间插入 15 个数据
n = 15
start = 2
end = 3
step = (end-start)/(n+1)
new_values = [start + i * step for i in range(1, n+1)]
# 将新数据插入到列表中
for i in range(n):
list.insert(i+3, new_values[i])
# 将列表按顺序排列
list = sorted(list)
# 生成新的列表
new_list = [i for i in range(1, list[-1]+1)]
print(new_list)
```
输出:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
```
其中,`new_values` 是要插入的新数据,`start` 和 `end` 是原列表中要插入新数据的两个数据,`step` 是每个新数据之间的差值。然后使用 `list.insert()` 将新数据插入到列表中,最后使用 `sorted()` 将列表按顺序排列,生成新的列表。
相关问题
python, 在原有list中,list = [1,2,3,4,5,6,7,8],两个数据中间插入15个数据,并按顺序排列
可以使用 Python 的列表推导式来实现:
```python
list = [1,2,3,4,5,6,7,8]
# 在 2 和 3 之间插入 15 个数据
n = 15
start = 2
end = 3
step = (end-start)/(n+1)
new_values = [start + i * step for i in range(1, n+1)]
# 将新数据插入到列表中
for i in range(n):
list.insert(i+3, new_values[i])
# 排序
list.sort()
print(list)
```
输出:
```
[1, 2, 2.0588235294117645, 2.1176470588235294, 2.176470588235294, 2.235294117647059, 2.2941176470588234, 2.3529411764705883, 2.411764705882353, 2.4705882352941178, 2.5294117647058822, 2.588235294117647, 2.6470588235294117, 2.7058823529411766, 2.764705882352941, 2.823529411764706, 2.8823529411764706, 2.9411764705882355, 3, 4, 5, 6, 7, 8]
```
其中,`new_values` 是要插入的新数据,`start` 和 `end` 是原列表中要插入新数据的两个数据,`step` 是每个新数据之间的差值。然后使用 `list.insert()` 将新数据插入到列表中,最后使用 `list.sort()` 将列表按顺序排列。
python 合并两个list
Python中有多种方法可以合并两个list对象,其中包括使用"+"操作符和使用extend()方法。具体方法如下:
1. 使用"+"操作符:将两个list对象相加即可合并,例如:
list_a = [1, 2, 3]
list_b = [4, 5, 6]
merged_list = list_a + list_b
print(merged_list)
2. 使用extend()方法:将一个list对象的元素添加到另一个list对象中,例如:
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_a.extend(list_b)
print(list_a)
需要注意的是,使用"+"操作符会创建一个新的list对象,而使用extend()方法则是在原有的list对象上进行修改。另外,如果要合并的不止两个list对象,可以使用extend()方法多次添加。例如:
list_a = [1, 2, 3]
list_b = [4, 5, 6]
list_c = [7, 8, 9]
list_a.extend(list_b)
list_a.extend(list_c)
print(list_a)
--相关问题--:
1. 如何在Python中删除list中的重复元素?
2. 如何在Python中对list进行排序?
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)