宾馆客房管理系统sql文件
时间: 2023-11-18 08:05:38 浏览: 27
Python中的list是一种可变的有序集合,可以包含任意类型的数据,包括其他list。以下是list的常用用法:
1. 创建list:用方括号[]括起多个元素,用逗号分隔。
```
list1 = [1, 2, 3, 4, 5]
list2 = ['apple', 'banana', 'cherry']
list3 = [1, 'apple', True, 3.14]
```
2. 访问list元素:使用索引,从0开始计数。
```
print(list1[0]) #输出1
print(list2[2]) #输出'cherry'
```
3. 切片:可以通过切片语法获取list的子集。
```
print(list1[1:3]) #输出[2, 3]
print(list2[:2]) #输出['apple', 'banana']
print(list3[2:]) #输出[True, 3.14]
```
4. 修改list元素:使用索引或切片语法修改。
```
list1[0] = 6
list2[1:3] = ['orange', 'mango']
print(list1) #输出[6, 2, 3, 4, 5]
print(list2) #输出['apple', 'orange', 'mango']
```
5. 添加元素:使用append()方法在list末尾添加元素,使用insert()方法在指定位置插入元素。
```
list1.append(6)
list2.insert(1, 'peach')
print(list1) #输出[1, 2, 3, 4, 5, 6]
print(list2) #输出['apple', 'peach', 'orange', 'mango']
```
6. 删除元素:使用del语句或remove()方法删除元素。
```
del list1[0]
list2.remove('orange')
print(list1) #输出[2, 3, 4, 5, 6]
print(list2) #输出['apple', 'peach', 'mango']
```
7. 其他操作:使用len()函数获取list长度,使用in关键字判断元素是否在list中。
```
print(len(list1)) #输出5
print('apple' in list2) #输出True
```
相关推荐
![](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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)