码迷,mamicode.com
首页 > 编程语言 > 详细

python基础复习 - 列表基本操作

时间:2018-11-10 12:41:13      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:ide   real   长度   div   war   error:   display   转化   ace   

技术分享图片
 1 class list(object):
 2     """
 3     Built-in mutable sequence.
 4     
 5     If no argument is given, the constructor creates a new empty list.
 6     The argument must be an iterable if specified.
 7     """
 8     def append(self, *args, **kwargs): # real signature unknown
 9         """ Append object to the end of the list. """
10         pass
11 
12     def clear(self, *args, **kwargs): # real signature unknown
13         """ Remove all items from list. """
14         pass
15 
16     def copy(self, *args, **kwargs): # real signature unknown
17         """ Return a shallow copy of the list. """
18         pass
19 
20     def count(self, *args, **kwargs): # real signature unknown
21         """ Return number of occurrences of value. """
22         pass
23 
24     def extend(self, *args, **kwargs): # real signature unknown
25         """ Extend list by appending elements from the iterable. """
26         pass
27 
28     def index(self, *args, **kwargs): # real signature unknown
29         """
30         Return first index of value.
31         
32         Raises ValueError if the value is not present.
33         """
34         pass
35 
36     def insert(self, *args, **kwargs): # real signature unknown
37         """ Insert object before index. """
38         pass
39 
40     def pop(self, *args, **kwargs): # real signature unknown
41         """
42         Remove and return item at index (default last).
43         
44         Raises IndexError if list is empty or index is out of range.
45         """
46         pass
47 
48     def remove(self, *args, **kwargs): # real signature unknown
49         """
50         Remove first occurrence of value.
51         
52         Raises ValueError if the value is not present.
53         """
54         pass
55 
56     def reverse(self, *args, **kwargs): # real signature unknown
57         """ Reverse *IN PLACE*. """
58         pass
59 
60     def sort(self, *args, **kwargs): # real signature unknown
61         """ Stable sort *IN PLACE*. """
62         pass
list class

插入

#插入 insert
a = [i for i in range(3)]
a.insert(1,4)
print(a) #[0, 4, 1, 2]
#extend
a = [0,1]
a.extend([2,3])
print(a) #[0,1,2,3]

 

删除

#pop
a = [3,4,5]
deleted_element = a.pop(1)
print(deleted_element) #4
print(a) #[3,5]
#pop默认删除最后一个元素
a = [1,2,3]
deleted_item = a.pop()
print(deleted_item) #3
print(a) #[1,2]
#remove 按元素删除
a = [1,2,2,3]
a.remove(2)
print(a) #[1,2,3]
#clear 清空
a = [1,2,3]
a.clear()
print(a) #[]
#del 删除
a = [1,2,3]
del a
print(a) #NameError: name ‘a‘ is not defined
#del 切片删除
a = [1,2,3,4]
del a[1:]
print(a) #[1]

 

将字符串转化为列表

#list
a = 12
a = list(a)
print(a) #[‘1‘,‘2‘]

#切片
a = []
a[0:2] = 12
print(a) #[‘1‘,‘2‘]

#以上就算序号超过字符串长度也没关系
a = []
a[0:3] = 12
print(a) #[‘1‘,‘2‘]
#字符串长度超过所给序号也没关系g
a = []
a[0:2]=1234
print(a) #[‘1‘, ‘2‘, ‘3‘, ‘4‘]

 

#对于列表赋值也是一样
a = []
a[0:1] = [i for i in range(10)]
print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 

公共方法

len, count

排序

#sorted 不改变原字符串顺序,sort改变原字符串顺序,其他一样
import random
a = [i for i in range(10)]
random.shuffle(a)
print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3]

print(sorted(a)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3]
print(sorted(a, reverse = True)) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

print(a.sort()) #None
print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.sort(reverse = True)
print(a) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

 

反转

reverse
a = [7, 4, 5, 9, 0, 1, 2, 8, 6, 3]
a.reverse()
print(a) #[3, 6, 8, 2, 1, 0, 9, 5, 4, 7]

 

python基础复习 - 列表基本操作

标签:ide   real   长度   div   war   error:   display   转化   ace   

原文地址:https://www.cnblogs.com/kaezah/p/9910335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!