Python的增刪改查

2021-10-01 08:59:31 字數 1004 閱讀 6552

```python

#coding:utf-8

# 對列表的增刪改查

namelist=["張三","里斯","王二","張三"]

# 查詢

print(namelist)

# 遍歷列表

for i in namelist:

print(i)

# 增加

names2=["yasuo","timo"]

# extend()將列表新增至另乙個列表的結尾

namelist.extend(names2)

print(namelist)

print(namelist)

# insert增加 在下標2的位置插入乙個元素

namelist.insert(2,"mangseng")

print(namelist)

# 刪除

# remove() 以元素的值刪除

namelist.remove("張三") # 直接刪掉張三這個值

print(namelist)

# pop(index)以元素的下標刪除,不寫下標預設刪除最後乙個

namelist.pop(0)

print(namelist)

# 查詢

# index()查詢是否有指定的元素,如果有返回該元素的下標

print(namelist.index("yasuo"))

# print(namelist.index("wangba"))

# count() 查詢指定元素出現了幾次

print(namelist.count("里斯"))

# 排序

# 以字母公升序排序

namelist.sort()

print(namelist)

# 以字母降序排序

namelist.reverse()

print(namelist)

python 增刪改查

lists aa aa 增lists.insert 0,aa 在索引為0的地方插入 aa lists.insert 5,aa 在索引為5的地方插入 aa 如果索引沒到5則插到尾部 刪lists.remove aa 刪除 第一遇到的 aa 從左向右 del lists 5 刪除指定索引的值 如果索引超...

Python 增刪改查

from pymysql import cursors from tools.config import config import random,string defcreatedatabase db config.db cur db.cursor cursor cursors.dictcurso...

Python中的增刪改查

在python中的增刪改查是如何進行的?insert extend names.pop names.remove del names 下標 names.pop 刪除,每次刪除從最後乙個開始。names.remove 想要刪除的資訊 根據內容從左到右尋找開始刪除,內容一樣則每次只刪除乙個。del na...