python的容器型数据方法实现

list 的相关方法(append,clear,copy,count,extend,index,insert,pop,remove,reverse,sort)

list.append(item) - 在末尾添加指定元素

1
2
3
4
list1 = [1,2,3,4]
item = 5
new_list1 = list1 + [item]
print(new_list1)

list.clear() - 清空列表

1
2
3
list2 = [1,2,3,4]
list2 = []
print(list2)

list.copy() - 产生一个一模一样的新列表

1
2
3
4
5
list3 = [1,2,3,4]
new_list3 = []
for item in list3:
new_list3 += [item]
print(new_list3)

list.count(item) - 获取指定元素在列表中的个数

1
2
3
4
5
6
7
list4 = [1,1,2,1,3,4,3,4]
count = 0
item = 1
for i in list4:
if i == item:
count+=1
print(item,"在list4中出现的次数为",count)

list.extend(序列) - 在列表后面添加一个序列

1
2
3
4
5
list5 = [1,2,3]
list6 = [4,5,6]
for item in list6:
list5+=[item]
print(list5)

list.index(item) - 获取指定元素在列表中第一次出现的下标,元素不存在报错

1
2
3
4
5
6
7
8
9
10
list7 = [5,1,3,4,3]
count = 0
item = 3
for i in list7:
if i == item:
print(count)
break
count += 1
else:
print("错误警告,元素不在列表中")

list.insert(index,item) - 列表指定位置插入指定元素,位置超过列表长度会在末尾追加

1
2
3
4
5
list8 = [1,2,3,4]
index = 2
item = 5
list8 = list8[:index] + [item] + list8[index:]
print(list8)

list.pop(index) - 取出列表指定位置的元素,如果不使用则没有意义,相当于删除操作

如果没有index,默认为列表的最后一个元素

1
2
3
4
5
list9 = [1,2,3,4]
index = 2
aa = list9[index]
list9 = list9[:index] + list9[index+1:]
print(list9)

list.remove(item) - 删除指定元素在列表中出现的第一个,如果元素不存在会报错

1
2
3
4
5
6
7
8
9
10
11
list10 = [1,2,3,4]
item = 3
count = 0
for i in list10:
if i == item:
list10 = list10[:count] + list10[count + 1:]
print(list10)
break
count += 1
else:
print("错误警告,元素不在列表中")

list.reverse() - 将整个列表翻转

1
2
3
list11 = [1,2,3,4]
list11 = list11[::-1]
print(list11)

list.sort() - 对整个列表进行升序排序

1
2
3
4
5
6
7
list12 = [1,3,4,2,1,5,6]
count = len(list12)
for i in range(count):
for j in range(i + 1, count):
if list12[i] > list12[j]:
list12[i], list12[j] = list12[j], list12[i]
print(list12)

dict 相关方法 (clear,copy,get,items,keys,setdefault,update,values,pop,popitem)

dict.clear() - 清空整个字典

1
2
3
dict1 = {'a':10,'b':20,'c':30}
dict1 = {}
print(dict1)

dict.copy() - 产生一个一模一样的新字典

1
2
3
4
5
dict2 = {'a':10,'b':20,'c':30}
new_dict2 = {}
for key in dict2:
new_dict2[key] = dict2[key]
print(new_dict2)

dict.get(key,default=None) - 获取指定键的的值,如果键不存在默认返回None,可以在逗号后面输入指定返回值

1
2
3
4
5
6
7
8
dict3 = {'a':10,'b':20,'c':30}
key1 = 'c'
for key in dict3:
if key == key1:
print(dict3[key1])
break
else:
print(None)

dict.items() - 以列表返回一个视图对象,列表中每一个元素对应字典每一个键值对

1
2
3
4
5
dict3 = {'a':10,'b':20,'c':30}
list1 = []
for key in dict3:
list1.append((key,dict3[key]))
print(list1)

dict.keys() - 以列表返回一个视图对象,列表中每一个元素对应字典每一个键

1
2
3
4
5
dict4 = {'a':10,'b':20,'c':30}
list1 = []
for key in dict4:
list1.append(key)
print(list1)

dict.setdefault(key, default=None) - 和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default

1
2
3
4
5
6
7
8
9
dict5 = {'a':10,'b':20,'c':30}
key1 = 'd'
for key in dict5:
if key == key1:
print(dict5[key1])
break
else:
dict5[key1] = None
print(dict5)

dict.update(dict2) - 把字典dict2的键/值对更新到dict里 - 键相同修改值,键不存在添加键值对

1
2
3
4
5
dict6 = {'a':10,'b':20,'c':30}
dict7 = {'d':40,'c':20}
for key in dict7:
dict6[key] = dict7[key]
print(dict6)

dict.values()

1
2
3
4
5
dict8 = {'a':10,'b':20,'c':30}
list1 = []
for key in dict8:
list1.append(dict8[key])
print(list1)

dict.pop(key) - 删除字典给定键所对应的值,返回值为被删除的值。键值必须给出,键不存在则报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dict9 = {'a':10,'b':20,'c':30}
key1 = 'b'
new_dict9 = {}
count = 0
for key in dict9:
if key == key1:
return_value = dict9[key1]
count+=1
continue
else:
new_dict9[key] = dict9[key]
if count != 0:
print(new_dict9)
else:
print("错误警告,键不存在")

dict.popitem() - 删除字典中的最后一对键和值。

1
2
3
4
5
6
7
8
9
dict9 = {'a':10,'b':20,'c':30}
len_dict9 = len(dict9)-1
new_dict9 = {}
for key in dict9:
new_dict9[key] = dict9[key]
len_dict9-=1
if len_dict9 ==0:
break
print(new_dict9)

tuple 相关方法 (count,index)

tuple.count(item) - 统计指定元素在元组中的个数

1
2
3
4
5
6
7
tuple1 = (1,2,2,3,4)
item = 2
count1 = 0
for i in tuple1:
if i == item:
count1+=1
print(count1)

tuple.index(item) - 获取指定元素在元组中的下标,元素不存在会报错

1
2
3
4
5
6
7
8
9
10
tuple2 = (1,2,3,4,5,2,4)
item = 4
index1 = 0
for i in tuple2:
if item == i:
print("元素的下标是:", index1)
break
index1+=1
else:
print("错误警告,元素不在元组中")

set 方法 (add,clear,copy,difference,difference_update,discradintersection,intersection_update,isdisjoint,issubset,issuperset,pop,remove,union,update,symmetric_differencesymmetric_difference_update)

set.add(元素) - 集合添加元素,如果添加的元素在集合中已存在,则不执行任何操作。

1
2
3
4
set1 = {4,2,5,7}
item = 8
set1 = set1 | {item}
print(set1)

set.update() - 修改当前集合,可以添加新的元素或集合到当前集合中,

##如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。

1
2
3
4
set1 = {4,2,5,7}
item = 8
set1 = set1 | {item}
print(set1)

add()只可以添加不可变的元素,如数字,字符串,元组,否则报错;

而update()可以添加字符串、列表、元组、字典等可迭代的元素,若添加数字类型会报错。

add()把元素整体性的添加进去,update()会把元素拆分再添加进去。

set.clear() - 移除集合中的所有元素。

1
2
3
set2 = {1,2,3,4}
set2 = set2 & set()
print(set2)

set.copy() - 产生一个一模一样的新集合

1
2
3
4
5
set3 = {5,6,7,8}
set4 = set()
for item in set3:
set4 = set4 | {item}
print(set4)

set.difference(set) - 返回的集合元素包含在第一个集合中,但不包含在第二个集合中

1
2
3
4
set5 = {1,2,3,4,5,6,7}
set6 = {5,6,7,8,9,10,11}
set7 = set5-set6
print(set5,set6,set7)

set.difference_update(set)

1
2
3
4
5
set5 = {1,2,3,4,5,6,7}
set6 = {5,6,7,8,9,10,11}
for item in set6:
set5 -= {item}
print(set5)

difference_update() 方法与 difference() 方法的区别在于

difference() 方法返回一个移除相同元素的新集合,

而 difference_update() 方法是直接在原来的集合中移除元素,没有返回值。

set.discard(value) - 移除指定的集合元素。元素不存在时不会报错

1
2
3
4
5
6
7
set7 = {1,2,3,4,5}
item = 4
for i in set7:
if i == item:
set7 -= {item}
break
print(set7)

set.remove(value) - 移除指定的集合元素。元素不存在时会报错

1
2
3
4
5
6
7
8
9
10
set7 = {1,2,3,4,5}
item = 5
if item in set7:
for i in set7:
if i == item:
set7 -= {item}
print(set7)
break
else:
print("报错,元素不存在集合中")

set.intersection(set1) - 用于返回两个或更多集合中都包含的元素,即交集。

1
2
3
4
set5 = {1,2,3,4,5,6,7}
set6 = {5,6,7,8,9,10,11}
set7 = set5 & set6
print(set7)

set.intersection_update() - 用于获取两个或更多集合中都重叠的元素,即计算交集

1
2
3
4
set5 = {1,2,3,4,5,6,7}
set6 = {5,6,7,8,9,10,11}
set5 &= set6
print(set5)

intersection_update() 方法不同于 intersection() 方法,

因为 intersection()方法是返回一个新的集合,

而intersection_update() 方法是在原始的集合上移除不重叠的元素。

set.isdisjoint() - 判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False

1
2
3
4
5
6
7
8
9
10
set5 = {1,2,3,4}
set6 = {5,6,7,8,9,10,11}
count = 0
for item in set5:
if item in set6:
count+=1
if count != 0:
print(True)
else:
print(False)

set.issubset() 判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False

1
2
3
4
5
6
7
8
set5 = {5,6,7,8}
set6 = {5,6,7,8,9,10,11}
flag = True
for item in set5:
if item not in set6:
flag = False
break
print(flag)

set.issuperset(set) 判断指定集合的所有元素是否都包含在原始的集合中,如果是则返回 True,否则返回 False。

1
2
3
4
5
6
7
8
set5 = {5,6,7,8}
set6 = {5,6,7,8,9,10,11}
flag = True
for item in set5:
if item not in set6:
flag = False
break
print(flag)

set.symmetric_difference() - 返回两个集合中不重复的元素集合,即会移除两个集合中都存在的元素。

1
2
3
4
set5 = {1,2,3,4,5,6,7,8}
set6 = {5,6,7,8,9,10,11}
set7 = (set5 | set6) - (set5 & set6)
print(set7)

set.symmetric_difference_update() 移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中。

1
2
3
4
set5 = {1,2,3,4,5,6,7,8}
set6 = {5,6,7,8,9,10,11}
set5 = (set5 | set6) - (set5 & set6)
print(set5)

set.union() - 返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。

1
2
3
4
set5 = {1,2,3,4,5,6,7,8}
set6 = {5,6,7,8,9,10,11}
set7 = set5 | set6
print(set7)

str 的相关方法

str.capitalize() - 将字符串的第一个字母变成大写,其他字母变小写。

1
2
3
4
5
6
7
8
9
10
str1 = 'wfxMYdfewe'
new_str1 = ''
if 'a' <= str1[0] <= 'z':
new_str1 += chr(ord(str1[0]) - 32)
for index in range(1,len(str1)):
if 'A' <= str1[index] <= 'Z':
new_str1 += chr(ord(str1[index]) + 32)
else:
new_str1 += str1[index]
print(new_str1)

str.center(width,fillchar) - 指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

1
2
3
4
5
6
7
8
9
10
11
str2 = 'abcd'
new_str2 = ''
width = 6
if len(str2) >= width:
new_str2 +=str2
else: ### 下面出现的'+'为填充字符可以自定义,默认空格
if (width - len(str2)) % 2:
new_str2 = '+' * ((width - len(str2)) // 2 + 1) + str2 + '+'*((width - len(str2)) // 2)
else:
new_str2 = '+' * ((width - len(str2)) // 2) + str2 + '+' * ((width - len(str2)) // 2)
print(new_str2)

str.count(str1) - 统计字符串里另一个字符串出现的次数

1
2
3
4
5
6
7
8
9
10
str3 = 'ababcd'
str4 = 'bc'
count = 0
if str4 not in str3:
print('0')
else:
for index in range(len(str3)):
if str4 == str3[index:index+len(str4)]:
count+=1
print(count)

str.endswith(str) - 判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False

1
2
3
4
5
6
7
8
9
str4 = 'ababcdc'
str5 = 'c'
if str5 not in str4:
print(False)
else:
if str5 == str4[-len(str5)::1]:
print(True)
else:
print(False)