Day four, lists, and tuples

First, list

Lists: ordered modifiable sequences
Format:

a = [1, 3, 4 ]
b = [1 , ]#Comma is required for an element

How to do this? How to do this?

  • 1. Length len() and traversal list based on length
a = [1, 3, 4, ]
for i in range(len(a)):
   print('The first%d The element is:%d' % (i + 1, a[i]))
print('The length is:', len(a))

Result:

  • Splicing and repetition of lists
a = [1, ]
b = [2, 3]
print(a + b)
print(a * 3)
print(a[0]+b[0])#Numbers follow mathematical addition and subtraction

Result:

  • 2. Slice and output in reverse order
a = [1, 2, 3, 4, 5,]
for i in range(1,len(a)+1):
    print(a[-i],end=' ')

Result:

3. Three added methods append, extend and insert

1)append()
The main implementation of append() for list operation is to add an element at the end of a specific list, and only one element can be added at a time, and only at the end of the list;
Format: m.append (element A)
2)extend()
The operation of extend() on the list mainly implements the expansion and growth of a specific list. Multiple elements can be added at one time, but only at the end of the list.
Format: m.extend([element A, element B,...... )
3)insert()
insert () is mainly used to add specific elements to the list at specific positions. The specific positions here refer to the index numbers of the positions in the list where the elements are located. It should be noted that the index numbers here start from 0, not from 1. This requires special attention.
Format: m.insert(A, element B): indicates adding element B at A+1 in list M

# --------------------Add ------ append(), extend(), and insert().
a = [1, 2, 3, 4, 5,]
a.append(7)#Append one at the end
print(a)
b=['a','b']
a.extend(b)#extend to insert another list
print(a)
a.insert(1,'x')#Add an 'x' to index one
print(a)

Result:

4. Three methods of deletion: pop, del, remove

a = [1, 2, 3, 4, 5,]
a.pop()#The last number will be deleted, and the deleted element will be output.
a.pop(2)#The number with index 2 will be deleted, and the deleted element will be output.

a = [1, 2, 3, 4, 5,]
a.remove(3)#Deleted the first 3 encountered, not the index location
print(a)

a = [1, 2, 3, 4, 5,]
del a[2]#Delete the corresponding index or the entire list
print(a)
  • 5. amendment
# ----------------Modification--------------------------------
a = [1, 2, 3, 4, 5,]
a[2]='xx'#Directly change the corresponding index to the modified value.
print(a)

Result;

-6. reverse ()

a = [1, 2, 3, 4, 5, ]
a.reverse()
print(a)

Result;

-7. sort ()

a=[1,3,5,2,5]
a.sort()#Changed a's thing, ascending
print(a)
b=sorted(a)#Unchanged value of a, return changed value to b
print(b)
a.sort(reverse=True)#Descending order
print(a)

Result:

·8. count ()

# -------------------------------count ()
a = [1, 2, 3, 1, 1]
b = a.count(1)
print('1 The number of:', b)

Result:

9. index ()

#-------------------------------index ()
a=[1,2,3,1,1]
b=a.index(1)#Returns the first occurrence of a value index
print(b)

Result:

10. List nesting (similar to 2D array)

#------------------------------List nesting
a=[[1,2,3],[4,5,6],[7,8,9]]
for i in a:
    for j in i:
        print(j,end=' ')
    print()
print(a[0][2])
print(a[1][2])
print(a[2][2])

Result:

11.id () method: difference between '= =' and is:

#-----------------------------id() and is,==
a=3
b=3
print(id(a))#Two memory addresses are the same, so their data is the same
print(id(b))
print(a is b)#Because the data is the same, a is b.

a=[1,2,3]
b=[1,2,3]
print(a==b)#True, = = is a value
print(a is b)#False. Is is is the memory address. a and b are two lists. I don't believe they can be verified with id().

Result:

12. Depth copy and equal to

import copy
a=[1,2,3,[4,5],6]
b=a
c=copy.copy(a)#Shallow copy: test the list roughly. The list inside the list will change with the list inside a, that is, the internal list also points to [4,5].
d=copy.deepcopy(a)#Deep copy, take a away completely, create a new address, keep the original a, it will not change
a.append('00000')
a[3].append('x')
print(a,id(a))
print(b,id(b))
print(c,id(c))
print(d,id(d))

Result:

Two, tuple

Posted by ksukat on Tue, 22 Oct 2019 07:38:22 -0700