List Python- learning knowledge base (list or [])

List: list or may be [] to represent the tuples of different length may vary, each of the elements may be modified.

lista = [2,3,5] #定义了一个列表

tup = ('foo', 'bar', 'baz')

listb = list(tup) #通过元组创建一个列表

print(listb)
#打印
['foo', 'bar', 'baz']

#修改列表第二项
listb[1] = 'peek'

print(listb)
#打印
['foo', 'peek', 'baz']

Another way to generate a list, or by the iterator generator conversions:

g = range(10) 
print(g)
#打印
    range(0, 10)

list_a = list(g)
pring(list_a)
#打印
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The method has the following list of commonly used:

append - add data

insert - insert data

remove - remove data

pop - Removes and returns data

lista = ['hello', 'world']
lista.append('day') #添加
print(lista)
#打印
    ['hello', 'world', 'day']

lista.insert(1, 'month') #在 1 的位置插入一个新数据
print(lista)
#打印
    ['hello', 'month', 'world', 'day']

lista.remove('world') #删除 ‘world’ 
print(lista)
#打印
    ['hello', 'month', 'day']

lista.append('hello') #这时里面有两个‘hello’
lista.remove('hello') #删除第一个‘hello’,留下最新加入的一个
print(lista)
#打印
    ['month', 'day', 'hello'] 

a = lista.pop(1) #移除第二个数据,并且返回
print(a, lista)
#打印
    'day', ['month', 'hello']

Another judge whether an element in the list, the keyword is used in and not in

#接着上面的代码
bln = 'year' in lista
print(bln)
#打印
    False

bln = 'year' not in lista
print(bln)
#打印
    True

Two lists can also be spliced ​​by using the '+' sign, but that price will be larger, due to the temporary create a new list and copy data, we can use the built-in list of functions, extend, it can directly behind the list Add to the list of calls to extend the function, which can reduce the formation of temporary data:

alist = [1, 2, 3]
blist = [4, 5, 6]

clist = alist + blist
print(clist)
#打印
    [1, 2, 3, 4, 5, 6]

alist.extend(blist)
print(alist)
#打印
    [1, 2, 3, 4, 5, 6]

Another important feature list is sorted, sort function, it is sorted in the internal list, does not create a new list:

alist = [5, 4, 7, 8, 1]
alist.sort()
print(alist)
#打印
    [1, 4, 5, 7, 8]

blist = ['saw', 'samll', 'he', 'foxes', 'six']
blist.sort(key=len)
print(blist)
#打印
    ['he', 'saw', 'six', 'small', 'foxes']

bisect is a module that can be implemented binary search and interpolation sorted list. Use bisect.bisect can find the elements should be inserted into position and holding ordered sequence; bisect.insort element may be inserted into the corresponding position:

import bisect #导入bisect模块

alist = [1, 2, 2, 2, 3, 4, 7]
print(bisect.bisect(alist, 2)) #表示 2 这个数,可以插入到列表的第五个位置
#打印
    4

print(bisect.bisect(alist, 5)) #表示 5 这个数,可以插入到列表的第七个位置
#打印
    6

bisect.insort(alist, 6)    #将数字6插入到列表的第七个位置
print(alist)
#打印
    [1, 2, 2, 2, 3, 4, 6, 7] 

bisect here does not check whether the list has been sorted, if not sorted on the use of this module, it may lead to incorrect results, so it should be in use after sorting, re-use this module can quickly insert data into the list to go .

Another powerful feature list is sliced ​​with a start: stop value to be passed in parentheses:

alist = [7, 2, 3, 7, 5, 6, 0, 1]
blist = alist[1: 5]
print(blist)
#打印
    [2, 3, 7, 5]


alist[3:4] = [6, 3]
print(alist)
#打印
    [7, 2, 3, 6, 3, 5, 6, 0, 1]

The number of slices is equal to the stop - start, comprising a start value, does not contain stop, start and stop further may be omitted, if it is omitted from the start position indicates the end of the list to the list of:

#接上面代码
blist = alist[:5]
print(blist)
#打印
    [7, 2, 3, 6, 3]

blist = alist[3:]
print(blist)
#打印
    [6, 3, 5, 6, 0, 1]

#另外索引也可以为负数,表示从列表最后开始:
blist = alist[-4:]
print(blist)
#打印
    [5, 6, 0, 1]

blist = alist[-6: -2]
print(blist)
#打印
    [6, 3, 5, 6]

It can also list the value of intervals, called the step value step, at the start: after a stop plus a colon indicate: start: stop: step:

#接上面代码
blist = alist[::2]
print(blist)
#打印
    [7, 3, 3, 6, 1]

#有一个小技巧,我们如果想讲列表翻转,那么只要将step变为-1即可:
blist = alist[::-1]
print(blist)
#打印
    [1, 0, 6, 5, 3, 6, 3, 2, 7]

 

Guess you like

Origin blog.csdn.net/pz789as/article/details/93731015