day 04 list tuple range

Posted by Heatmizer20 on Thu, 16 May 2019 05:09:26 +0200

First, list

Included in [], each element is separated by "," and various data types can be stored.

Compared with strings, lists can store not only different data types, but also large amounts of data. 32-bit python can store: 536870912 elements, 64-bit can store: 1152921504606846975 elements.

Lists are ordered (in order of preservation), indexed, and sliced for easy access to values.

Lists belong to variable data types.

1. index

lst = ["python", "java", "c",  "c++", [11, 22,33]]

print(lst[0])  # python
print(lst[4][0]) # 11

2. slice

Is the list sliced or tabulated?

lst = [11, 22, 33, 44, 55, 66]

print(lst[0:3]) # [11, 22, 33] 
print(lst[1::2]) # [22, 44, 66]
print()lst[2::-1] # [33,22,11]

3. Addition, deletion, modification and examination

1) increase.

  • append

Add elements at the end of the list

lst = [11, 22, 33, 44]
lst.append(55)
print(lst) # [11, 22, 33, 44, 55]

 

  • insert
lst = [11, 22,33]
lst.insert(1, 55)
print(lst) # [11, 55, 22, 33]
  • extend

Iterative addition, the added elements must be iteratable objects, and the adding position is at the end

lst = [11, 22, 33]
lst.extend([55, 66]) # [11, 22, 33, 55, 66]
lst.extend("python") # [11, 22, 33, 'p', 'y', 't', 'h', 'o', 'n']
lst.extend(44) # Report errors

 

2). Delete

  • pop

By default, you can delete elements at the end of the list or at the specified location, provided that an index is provided. The deletion has a return value and the return value is the deleted element.

lst = [11, 22, 33,  44, 55]
el = lst.pop() # 55
print(lst) # [11, 22, 33, 44]

lst.pop(2)
print(lst) # [11, 22, 44]

 

  • remove

Delete according to the content of the element. If there are multiple identical elements in the list, the first element is deleted by remove operation.

lst = [11, 22, 33, 44]

lst.remove(44)
print(lst) # [11, 22, 33]
  • del

Slice deletion

lst = [11, 22, 33, 44, 55, 66]

del lst[0] 
print(lst) # [22, 33, 44, 55, 66]

del[1:4:2] 
print(lst) # [22, 44, 66]

 

  • clear

Clear lists

lst = [11, 22, 33, 44, 55]

lst.clear()
print(lst) # []

3).

Specified Index Modification

lst = [11, 22, 33, 44]

lst[2] = 55
print(lst) # [11, 22, 55, 44]

According to the slice modification, if the step size is not 1, the number of elements modified must match the number of elements provided. If the slice does not have a step size or a step size of 1, you do not need to care about the number of slices.

lst = [11, 22, 33, 44, 55]

lst[1:4] = 'programming'
print(lst) # [11, "Edit", "Cheng", 55]

lst = [11, 22, 33, 44, 55, 66]
lst[1:5:2] = "programming"
print(lst) # [11,'Bian', 33,'Cheng', 55, 66]

4).

Lists are a beatable iteration object, so you can do for loops

for i in lst:
    print(i)

4. Other operations

  • count

Query the number of occurrences of an element

lst = [11, 22, 11, 44, 55, 66]

c = lst.count(11)
print(c) # 2

 

  • sort

By default, they are arranged in ascending order, and if descending order is required, just add reverse=True in parentheses.

lst = [11, 44, 33, 22, 55]

lst.sort()
print(lst) # [11, 22, 33, 44, 55]

lst.sort(reverse=True) print(lst) # [55, 44, 33, 22, 11]

 

  • reverse

Flip List

lst = [11, 33, 22, 55, 44]

lst.reverse()
print(lst) # [44, 55, 22, 33, 11]

5. List nesting

Dimension reduction operation

lst = [11, 22, [33, ["python", 55]], 66]

print(lst[1])  # 22

print(lst[2][0]) # 33

print(lst[2][1][0]) # p

lst[2][1][0] = lst[2][1][0].replace("p", "P")

print(lst) # [11, 22, [33, ['Python', 55]], 66]

lst[2].append(77)
print(lst) # [11, 22, [33, ["Python", 55], 77], 66]

 

Two, tuple

Commonly known as immutable lists, also known as read-only lists

Enclosed in parentheses, data of any data type can be placed inside

Query, loop, slice (after slicing, tuples), but not modify elements

If there is only one element in a tuple, a comma must be added, otherwise it is not a tuple.

t = (11, 22, 33, 'python', 44, 55, 66)

print(t[0]) # 11

print(t[1:3]) # (22, 33) 

for i in t:
  print(i) # 11 22 33 python 44 55 66

t = (1,)
print(type(t)) # <class 'tuple'>

t = (1)
print(type(t)) # <class 'int'>

Amendment

Tuple immutability refers to the immutability of the memory address of the first element in a tuple.

t = (11, 22, 33, [], 44)

t[3] = [55,]  # Report errors

t[3].append(55) # (11, 22, 33, [55], 44)

Tuples also have count, index, len and other methods

Three, range

  • range(start, end, step)
  • Get the index and element of the element
lst = ["Chinese", "Mathematics", "English","High number","Sports"]
for i in range(len(lst)):
        print(i, lst[i])

Topics: Python Programming Java