字符串排序
列表增加元素 | append() vs extend()
核查列表元素是否完全相同
列表拼接
列表去重
列表转换为字符串
使用索引或者值剔除列表元素 | remove() vs pop() vs del
列表中剔除多个元素
首先定义列表
1In [1]: # 初始化列表
2 ...: listOfStrings = ['hi' , 'hello', 'at', 'this', 'there', 'from']
3 ...: print(listOfStrings)
4['hi', 'hello', 'at', 'this', 'there', 'from']
永久排序,默认按照字母表升序排列
1In [2]: # 永久排序,默认按照字母表顺序排列
2 ...: listOfStrings.sort()
3 ...: print(listOfStrings)
4['at', 'from', 'hello', 'hi', 'there', 'this']
逆序排序
1In [3]: # 永久排序,reverse 参数控制排序方式, True 逆序
2 ...: listOfStrings.sort(reverse=True)
3 ...: listOfStrings
4Out[3]: ['this', 'there', 'hi', 'hello', 'from', 'at']
按照元素长度排序
1In [4]: # 永久排序,按照 key 关键字 列表元素长度
2 ...: listOfStrings.sort(key=len)
3 ...: print(listOfStrings)
4['hi', 'at', 'this', 'from', 'there', 'hello']
按照数值大小,升序排列
1In [5]: # 永久排序,按照列表元素数值大小,升序排列
2 ...: listOfNum = ['55' , '101', '152', '98', '233', '40', '67']
3 ...: listOfNum.sort(key=int)
4 ...: listOfNum
5Out[5]: ['40', '55', '67', '98', '101', '152', '233']
按照数值大小,逆序排列
1In [6]: # 永久排序,按照列表元素数值大小,降序排列
2 ...: listOfNum.sort(reverse=True, key=int)
3 ...: listOfNum
4Out[6]: ['233', '152', '101', '98', '67', '55', '40']
列表默认追加元素,append()
1In [7]: # 列表末尾追加元素 append()
2 ...: wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
3 ...: wordList.append("from")
4 ...: wordList
5Out[7]: ['hi', 'hello', 'this', 'that', 'is', 'of', 'from']
列表默认追加列表,append()
1In [8]: # 列表末尾追加列表 append()
2 ...: wordList.append(["one", "use", "data"])
3 ...: wordList
4Out[8]: ['hi', 'hello', 'this', 'that', 'is', 'of', 'from', ['one', 'use', 'data']]
列表拼接,extend()
1In [9]: # 列表拼接,extend()
2 ...: wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']
3 ...: wordList.extend(["one", "use", "data"])
4 ...: wordList
5Out[9]: ['hi', 'hello', 'this', 'that', 'is', 'of', 'one', 'use', 'data']
初始化列表
1In [10]: # 初始列列表
2 ...: listOfStrings = ['Hello'] * 10
3 ...: print(listOfStrings)
4['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello']
使用 all() 核查列表元素是否完全相同
1In [11]: # 使用 all() 核查列表元素是否完全相同
2 ...: result = False;
3 ...: if len(listOfStrings) > 0 :
4 ...: result = all(elem == listOfStrings[0] for elem in listOfStrings)
5 ...:
6 ...: if result:
7 ...: print("All Elements in List are Equal")
8 ...: else:
9 ...: print("All Elements in List are Not Equal")
10 ...:
11All Elements in List are Equal
使用 count() 核查列表元素是否完全相同
1In [12]: # 使用 count() 核查列表元素是否完全相同
2 ...: result = False;
3 ...: if len(listOfStrings) > 0 :
4 ...: result = listOfStrings.count(listOfStrings[0]) == len(listOfStrings)
5 ...:
6 ...: if result:
7 ...: print("All Elements in List are Equal")
8 ...: else:
9 ...: print("All Elements in List are Not Equal")
10 ...:
11All Elements in List are Equal
使用集合 set 核查列表元素是否完全相同
1In [13]: # 使用集合 set 核查列表元素是否完全相同
2 ...: result = len(set(listOfStrings)) == 1
3 ...:
4 ...: if result:
5 ...: print("All Elements in List are Equal")
6 ...: else:
7 ...: print("All Elements in List are Not Equal")
8 ...:
9All Elements in List are Equal
初始化列表
1In [14]: # 初始化列表
2 ...: list1 = ["This" , "is", "a", "sample", "program"]
3 ...: list2 = [10, 2, 45, 3, 5, 7, 8, 10]
使用运算符 + 拼接
1In [15]: # 使用运算符 +
2 ...: finalList = list1 + list2
3 ...: print("Merged List : " , finalList)
4Merged List : ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
使用 extend() 拼接
1In [16]: # 使用 extend()
2 ...: list1 = ["This" , "is", "a", "sample", "program"]
3 ...: list2 = [10, 2, 45, 3, 5, 7, 8, 10]
4 ...: list1.extend(list2)
5 ...: print("extended list1 : " , list1)
6extended list1 : ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10]
使用 + 运算符拼接多个列表
1In [17]: # 使用 + 运算符拼接多个列表
2 ...: list1 = ["This" , "is", "a", "sample", "program"]
3 ...: list2 = [10, 2, 45, 3, 5, 7, 8, 10]
4 ...: list3 = [11, 12, 13]
5 ...: finalList = list1 + list2 + list3
6 ...: print("Merged List : " , finalList)
7Merged List : ['This', 'is', 'a', 'sample', 'program', 10, 2, 45, 3, 5, 7, 8, 10, 11, 12, 13]
初始化列表
1In [18]: # 初始化列表1
2 ...: listOfNums = [10, 2, 45, 3, 5, 7, 2, 10, 45, 8, 10]
3 ...: print("Original List : " , listOfNums)
4Original List : [10, 2, 45, 3, 5, 7, 2, 10, 45, 8, 10]
使用集合 set 对列表去重
1In [19]: # 使用集合 set 对列表去重
2 ...: listOfNums = list(set(listOfNums))
3 ...: print("List with unique elements : ", listOfNums)
4List with unique elements : [2, 3, 5, 7, 8, 10, 45]
使用自定义函数对列表去重
1In [21]: # 使用自定义函数对列表去重
2 ...: def removeDuplicates(listofElements):
3 ...:
4 ...: # Create an empty list to store unique elements
5 ...: uniqueList = []
6 ...:
7 ...: # Iterate over the original list and for each element
8 ...: # add it to uniqueList, if its not already there.
9 ...: for elem in listofElements:
10 ...: if elem not in uniqueList:
11 ...: uniqueList.append(elem)
12 ...:
13 ...: # Return the list of unique elements
14 ...: return uniqueList
15 ...:
16
17In [22]: listOfNums = [10, 2, 45, 3, 5, 7, 2 , 10, 45, 8, 10]
18 ...: print("Original List : " , listOfNums)
19 ...: listOfNums = removeDuplicates(listOfNums)
20 ...: print("List with unique elements : ", listOfNums)
21Original List : [10, 2, 45, 3, 5, 7, 2, 10, 45, 8, 10]
22List with unique elements : [10, 2, 45, 3, 5, 7, 8]
初始化列表
1In [23]: # 初始化列表
2 ...: listOfwords = ["This" , "is", "a", "sample", "program"]
3 ...: listOfwords
4Out[23]: ['This', 'is', 'a', 'sample', 'program']
使用 str.join() 将列表元素转换为字符串,空格作为分隔符
1In [24]: # 使用 str.join() 将列表元素转换为字符串,空格作为分隔符
2 ...: # 语法:seperator_string.join(iterable sequence)
3 ...: fullStr = ' '.join(listOfwords)
4 ...: print("Joined String : ", fullStr)
5Joined String : This is a sample program
使用 str.join() 将列表元素转换为字符串,','作为分隔符
1In [25]: # 使用 str.join() 将列表元素转换为字符串,','作为分隔符
2 ...: fullStr = ','.join(listOfwords)
3 ...: print("Joined String : ", fullStr)
4Joined String : This,is,a,sample,program
使用 str.join() 将包含数字的列表元素转换为字符串,','作为分隔符
1In [26]: # 使用 str.join() 将包含数字的列表元素转换为字符串,','作为分隔符
2 ...: mixList = ["This" , "is", "a", "sample", 44, 55, 66, "program"]
3 ...: fullStr = '_'.join([str(elem) for elem in mixList ])
4 ...: print("Joined String : ", fullStr)
5Joined String : This_is_a_sample_44_55_66_program
使用 remove() 删除
1In [27]: # 使用 remove() 删除元素值
2 ...: # 语法:list.remove(value)
3 ...: # 注意:是删除首次出现的元素
4 ...: listOfnum = [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56]
5 ...: print("Original List : " , listOfnum)
6 ...: listOfnum.remove(56)
7 ...: print("Modified List : " , listOfnum)
8Original List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56]
9Modified List : [12, 44, 45, 34, 3, 56, 4, 33, 44, 56]
加入容错机制
1In [28]: # 加入容错机制 1 -- if
2 ...: if 99 in listOfnum:
3 ...: listOfnum.remove(99)
4 ...: else:
5 ...: print("Given Element Not Found in List")
6 ...:
7Given Element Not Found in List
8
9In [29]: # 加入容错机制 2 -- try / except
10 ...: try :
11 ...: listOfnum.remove(99)
12 ...: except ValueError:
13 ...: print("Given Element Not Found in List")
14 ...:
15Given Element Not Found in List
使用 pop() 删除
1In [30]: # 使用 pop() 删除指定索引的元素,且删除后该元素仍可使用,默认删除列表末尾元素
2 ...: # 语法:list.pop(index)
3 ...: listOfnum = [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56]
4 ...: print("Original List : ", listOfnum)
5 ...: value = listOfnum.pop(2)
6 ...: print("Deleted Element : ", value)
7 ...: print("Modified List : ", listOfnum)
8Original List : [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56]
9Deleted Element : 12
10Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56]
pop() 默认删除末尾元素
1In [31]: # 默认删除末尾元素
2 ...: value = listOfnum.pop()
3 ...: print("Deleted Element : ", value)
4 ...: print("Modified List : ", listOfnum)
5Deleted Element : 56
6Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44]
加入容错机制
1In [32]: # 加入容错机制 1 -- if
2 ...: if len(listOfnum) >= 20:
3 ...: listOfnum.pop(20)
4 ...: else:
5 ...: print("Index Out of Range")
6 ...:
7Index Out of Range
8
9In [33]: # 加入容错机制 2 -- try / except
10 ...: try :
11 ...: listOfnum.pop(20)
12 ...: except IndexError:
13 ...: print("Index Out of Range")
14 ...:
15Index Out of Range
使用关键字 del 删除
1In [34]: # 使用关键字 del 删除指定元素
2 ...: listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
3 ...: print("Original List : ", listOfnum)
4 ...: del listOfnum[2]
5 ...: print("Modified List : ", listOfnum)
6Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44]
7Modified List : [12, 44, 45, 34, 3, 4, 33, 44]
加入容错机制
1In [35]: # 加入容错机制 1 -- if
2 ...: if len(listOfnum) >= 20:
3 ...: del listOfnum[20]
4 ...: else:
5 ...: print("Index Out of Range")
6 ...:
7Index Out of Range
8
9In [36]: # 加入容错机制 2 -- try / except
10 ...: try :
11 ...: del listOfnum[20]
12 ...: except IndexError:
13 ...: print("Index Out of Range")
14 ...:
15Index Out of Range
使用 while 循环删除
1In [37]: # 使用 while 删除多个元素
2 ...: listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
3 ...: print("Original List : " , listOfnum)
4 ...: for elem in listOfnum:
5 ...: if elem % 3 == 0:
6 ...: listOfnum.remove(elem)
7 ...: print("Modified List : " , listOfnum)
8Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44]
9Modified List : [44, 56, 34, 4, 44]
使用列表推导式删除
1In [38]: # 使用列表推导式删除多个元素
2 ...: listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
3 ...: print("Original List : " , listOfnum)
4 ...: listOfnum = [ elem for elem in listOfnum if elem % 3 != 0]
5 ...: print("Modified List : " , listOfnum)
6Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44]
7Modified List : [44, 56, 34, 4, 44]
使用 del 索引范围 删除多个元素
1In [39]: # 使用 del 索引范围 删除多个元素
2 ...: listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44]
3 ...: print("Original List : " , listOfnum)
4 ...: del listOfnum[1:4]
5 ...: print("Modified List : " , listOfnum)
6Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44]
7Modified List : [12, 34, 3, 4, 33, 44]