Python basic data types (c) list list

3.4 List list []

Format of the list

lst_l = [1,'123',[1,'www',2],'包青天']

There is also a list of index

print(lst_l[0])
print([-1][0:2])        #包青 

# Slice or a slice out of the list

print(lst[1:3])
print(lst[start:end:步长])    #开始:结束:步长

List of additions and deletions to change search

list and str is not the same list of changes can occur

increase

1. Adding .append

lst[]
lst.append('要增加的内容')  #.append (在最后增加)

2. Insert .insert

lst=['刘德华','古天乐','张家辉']
lst.insert(2,'陈小春')         #在第二个位置插入陈小春 插在你定位的索引之前

3. Add .extend iteration

lst=['李文浩','浩文李']
lst.extend(['刘能','赵四'])
lst.extend('刘能`')

4. list merge

l1=[1,2,3]
l2=['ww','www']
l3= l1+l2       
print(l3)[1,2,3,'ww','www']

delete

Except pop return value returned is removed elements

1 .pop () to delete the last, you can also delete according to index

1st = ['a','b','古天乐']
s1 =lst.pop()       #默认删除最后一个,删除古天乐
s1 =lst.pop(2)      #按照索引去删除

2.remove () to delete pressing element

lst.remove('a')     #按照元素去删除

3.clear () Clear List

lst = ['语文','数学','英语']
lst.clear           #清空列表
print(lst)      

4.del lst [2] index delete del lst [:: 2] sections deleted

del lst[0:2]            #切片去删除
del lst         #将整个列表删除

change

1. The index change directly to

lst = ['大话西游','功夫']
lst[1] = '美人鱼'

The index sliced ​​into iterative modification #

lst[0:2]=['无间道']

3. step - you take a few, not to put a few more, no less

check

Iterable list is a for loop to iterate him

for i in lst:
    print(i)

Related operations

method meaning Remark
.count('') count list.count ( 'the content to be counted')
.sort() Sequence the list.sort () Default positive sequence ordering list.sort ( 'reverse = True') # flashback
.reverse() Overturn list.reverse () Flip a whole
len (list) Seek length len (list)

Nested list

list = [1,2,['古天乐','李文浩'],1]
list = [2][0]           #古天乐

Guess you like

Origin www.cnblogs.com/llwwhh/p/10991097.html