首页 > 代码库 > 列表的系列操作(python)

列表的系列操作(python)

除了定义和切片外,这里总结下系列的操作:

# hanbb come on!
names = ["hbb",tian,bao,cheng]

#Add
names.append("new")
print(names)             #[‘hbb‘, ‘tian‘, ‘bao‘, ‘cheng‘, ‘new‘]

# insert
#names.insert(1,before 2)              #invalid syntax
names.insert(1,"before 1")
print(names)            #[‘hbb‘, ‘before 1‘,‘tian‘,  ‘bao‘, ‘cheng‘, ‘new‘]
names.insert(2,behind 1)                                                #写在哪,插在哪
print(names)            # [‘hbb‘, ‘behind 1‘, ‘before 2‘, ‘tian‘, ‘bao‘, ‘cheng‘, ‘new‘]

# revise
names [0] = "忍你很久了"
print(names)            # [‘忍你很久了‘, ‘before 1‘, ‘behind 1‘, ‘tian‘, ‘bao‘, ‘cheng‘, ‘new‘]

# delete
del names [1]
print(names)            # [‘忍你很久了‘, ‘behind 1‘, ‘tian‘, ‘bao‘, ‘cheng‘, ‘new‘]
# del names[‘bao‘]                    #list indices must be integers or slices, not str

# names.del                          # no have this operation
# names.remove(2)                    # list.remove(x): x not in list
names.remove("tian")
print(names)            # [‘忍你很久了‘, ‘behind 1‘, ‘bao‘, ‘cheng‘, ‘new‘]

names.pop(2)            # [‘忍你很久了‘, ‘behind 1‘, ‘cheng‘, ‘new‘]
print(names)
names.pop()                          # delete the last one
print(names)            # [‘忍你很久了‘, ‘behind 1‘, ‘cheng‘]

# extend
names_2 = ["cao","hu","zhuo"]

names.extend(names_2)
print(names)             # [‘忍你很久了‘, ‘behind 1‘, ‘cheng‘, ‘cao‘, ‘hu‘, ‘zhuo‘]

# copy
names_3 = names.copy()
print(names_3)           # [‘忍你很久了‘, ‘behind 1‘, ‘cheng‘, ‘cao‘, ‘hu‘, ‘zhuo‘]

# count
# names.count()                            # count() takes exactly one argument (0 given)

#print(names.count(cao))                   #   name ‘cao‘ is not defined
print(names.count("cao"))                 #  统计出现的次数


# sort
names.insert(-1,"666")
names.insert(-1,"b88")
print(names)              # [‘忍你很久了‘, ‘behind 1‘, ‘cheng‘, ‘cao‘, ‘hu‘, ‘666‘, ‘b88‘, ‘zhuo‘]
names.sort()
print(names)              # [‘666‘, ‘b88‘, ‘behind 1‘, ‘cao‘, ‘cheng‘, ‘hu‘, ‘zhuo‘, ‘忍你很久了‘]
# Reverse
names.reverse()
print(names)              # [‘忍你很久了‘, ‘zhuo‘, ‘hu‘, ‘cheng‘, ‘cao‘, ‘behind 1‘, ‘b88‘, ‘666‘]

#  获取下标(位置)
#  names.index()                             # return first index of value. Raises ValueError if the value is not present.
print(names.index("hu"))  # 2

names.insert(2,"hu")      
print(names)              # [‘忍你很久了‘, ‘zhuo‘, ‘hu‘, ‘hu‘, ‘cheng‘, ‘cao‘, ‘behind 1‘, ‘b88‘, ‘666‘]
print(names.index("hu")) # 2

 

可以分为两种情况进行总结:

总结1:单元元素操作

names = ["hbb",tian,bao,cheng]
# the operation of single element:
names.append("xiaoqi")        # add to the last
names.insert(2,"bb8")         # inset one element to target location.

names.remove("bao")           # remove one element
names.pop()                   # remove the last element
names.pop(1)                  # remove element according to it location
print(names.count("cheng"))   # 统计某个元素出现的次数
print(names.index("cheng"))   # 获取某个元素的位置(下标),第一次出现

names [2] = "我要上位"         # 将某位置的元素换掉
names ["hbb"] = "HBB" # 只能根据位置来操作,很忧伤 del names [1] # 根据位置删除

总结1:整个列表的操作

# the operation of entired list
names_2 = names.copy()        # 复制列表
names_3 = [1,2] names.extend(names_3) # 扩展列表 names.reverse() # 翻转列表 names.sort() # 排序

总结2:多数情况均是以names.XXXX()进行操作,有几个不是:

names = ["hbb",tian,bao,cheng]
# the operation of single element:

names [2] = "我要上位"      # 更换操作
del names [1] # 可以用 names.remove( )代替 names.pip (1)
# the operation of entired list names_3 = [1,2] # difine

 

列表的系列操作(python)