Task03: a list of tuples (2day)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/NumberOneStudent/article/details/102691142

We are ready to use 17 days time, the Python-based deliberate practice divided into the following tasks:

Task01: variables, operators and the data type (1day)
Task02: cyclic conditions (1day)
Task03: a list of tuples (2Day)
Task04: string sequence (1day)
Task05: Function and Lambda (2Day)
Task06: dictionaries and collections (1day)
Task07: file with the file system (2Day)
Task08: Exception handling (1day)
Task09: the else and with the statement (1day)
Task10: classes and objects (2Day)
Task11: magic method (2Day)
Task12: module ( 1day)

Task03: a list of tuples (2day)

1. List

"List" is defined syntax is [element 1, element 2, ..., element n]

The key point is "in brackets []" and "comma"

  • Brackets all the elements tied together
  • Comma separated one by one each element
  • List is an ordered collection, there is no fixed size, can save any number of any type of Python object.

1. Create a list

Creating a common list

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week, type(week))
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] <class 'list'>

number = [2, 3, 4, 5, 6, 7]
print(number, type(number))
# [2, 3, 4, 5, 6, 7] <class 'list'>

Creating a mixed list

mix = [1, 'lsgo', 3.14, [1, 2, 3]]
print(mix)  # [1, 'lsgo', 3.14, [1, 2, 3]]

Create an empty list

empty = []
print(empty)  # []

Unlike the tuple list, the content list can be changed (the mutable), so an additional (append, extend), insertion (INSERT), delete (remove, pop) These operations can be used on it.

2. Add to the list of elements

append (obj) added at the end of the list of new objects, it takes one parameter, the parameter can be any data type, is added to maintain the original structure of the element type in the list.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.append('Thursday')
print(week)  
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday']
print(len(week))  # 6

If this element is a list, then the list will be appended as a whole , attention append () and extend () difference.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.append(['Thursday', 'Sunday'])
print(week)  
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', ['Thursday', 'Sunday']]
print(len(week))  # 6

extend (seq) a plurality of additional one-time value in the other sequence at the end of the list (with a new list expansion original list)

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.extend(['Thursday', 'Sunday'])
print(week)  
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']

Strictly speaking append is added, the whole thing added after the list, and extend an extension, put a thing in all the elements added after the list.

insert (index, obj) obj inserted before position index number.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.insert(0, 'Sunday')
print(week)  
# ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

3. Get element from the list

Like arrays, we can get through the index values ​​of the elements of a single element from the list, note that the list of the index value is zero-based.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[0])  # Monday

4. Remove the element from the list

remove (obj) to remove the list a value of the first match

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
week.remove('Monday')
print(week)  # ['Tuesday', 'Wednesday', 'Thursday', 'Friday']

pop ([index = -1]) removes an element in the list (default to the last element), and returns the value of the element

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
day = week.pop()
print(day)  # Friday
day = week.pop(0)
print(day)  # Monday
day = week.pop(-2)
print(day)  # Wednesday

remove and pop elements can be removed, the former is specific to delete the specified element, which is to specify a location number.
del var1 [, var2 ......] statement: delete a single or multiple objects

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
del week[0], week[1]
print(week)  # ['Tuesday', 'Thursday', 'Friday']

The list slicing

GM is written slice start: stop: step

Case 1 - "start:"
at step 1 (default) to the end of the list from No. slice start.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[3:])  # ['Thursday', 'Friday']
print(week[-3:])  # ['Wednesday', 'Thursday', 'Friday']

Case 2 - ": stop"
at step 1 (default) from the head of the list of numbers to stop slicing.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[:3])  # ['Monday', 'Tuesday', 'Wednesday']
print(week[:-3])  # ['Monday', 'Tuesday']

Case 3 - "start: stop"
at step 1 (default) from number to number stop start slicing.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[1:3])  # ['Tuesday', 'Wednesday']
print(week[-3:-1])  # ['Wednesday', 'Thursday']

Case 4 - "start: stop: step "
a concrete step number from the numbers start to stop slicing. Note that the last step is set to -1, corresponding to the list reverse order.

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[1:4:2])  
# ['Tuesday', 'Thursday']

print(week[:4:2])  
# ['Monday', 'Wednesday']

print(week[1::2])  
# ['Tuesday', 'Thursday']

print(week[::-1])  
# ['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']

6. copy

week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[:])  
# week的拷贝 ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

list1 = [123, 456, 789, 213]
list2 = list1
list3 = list1[:]
print(list2)  # [123, 456, 789, 213]
print(list3)  # [123, 456, 789, 213]
list1.sort()
print(list2)  # [123, 213, 456, 789] 
print(list3)  # [123, 456, 789, 213]

7. The common operator list

Comparison Operators
Logical Operators
concatenation operator +
repetition operator *
a member of relational operators in, not in
the same tuple stitching, a listing splicing there are two ways, with "Plus +" and "multiplication sign *", the former head and tail splicing, the latter copy stitching.

list1 = [123, 456]
list2 = [234, 123]
print(list1 > list2)  # False

list3 = [123, 456]
print((list1 < list2) and (list1 == list3))  # True

list4 = list1 + list2  # extend()
print(list4)  # [123, 456, 234, 123]

list5 = list3 * 3
print(list5)  # [123, 456, 123, 456, 123, 456]

list3 *= 3
print(list3)  # [123, 456, 123, 456, 123, 456]

print(123 in list3)  # True
print(456 not in list3)  # False

The first three methods (append, extend, insert) may be added to the list of elements, which have no return value, by directly modifying the original data object.
The sum of two list, you need to create a new list objects, which need to consume extra memory, especially when the list is large, try not to use the "+" to add the list.

8. Other methods of list

count (obj) method: count the number of an element that appears in the list

list1 = [123, 456] * 3
print(list1)  # [123, 456, 123, 456, 123, 456]
num = list1.count(123)
print(num)  # 3

index (obj [, start [, end]]) Method: identify a match value of the first index position from the list

list1 = [123, 456] * 5
print(list1.index(123))  # 0
print(list1.index(123, 1))  # 2
print(list1.index(123, 3, 7))  # 4

reverse () method: reverse elements in the list

list1 = [123, 456, 789]
list1.reverse()
print(list1)  # [789, 456, 123]

sort (key = None, reverse = False) Method: sort the list of the original

list1 = [123, 456, 789, 213]
list1.sort()
print(list1)  # [123, 213, 456, 789]

list1.sort(reverse=True)
print(list1)  # [789, 456, 213, 123]

Tuple

"Tuple" is defined syntax :( element 1, element 2, ..., element n)

Parentheses all the elements tied to
a comma separated one by one each element

1. Create and access a tuple

Python is similar to a list of tuples, except that the tuple is created can not be modified after similar string.

Tuples use parentheses, square brackets list.

t1 = (1, 10.31, 'python')
t2 = 1, 10.31, 'python'
print(t1, type(t1))
# (1, 10.31, 'python') <class 'tuple'>

print(t2, type(t2))
# (1, 10.31, 'python') <class 'tuple'>

tuple1 = (1, 2, 3, 4, 5, 6, 7, 8)
print(tuple1[1])  # 2
print(tuple1[5:])  # (6, 7, 8)
print(tuple1[:5])  # (1, 2, 3, 4, 5)
tuple2 = tuple1[:]
print(tuple2)  # (1, 2, 3, 4, 5, 6, 7, 8)
  • Creating a tuple can use parentheses (), you can do anything, for readability, it was suggested by ().
  • When tuple contains only one element, the element needs to be added after the comma, or brackets will be used as the operator:
temp = (1)
print(type(temp))  # <class 'int'>
temp = 2, 3, 4, 5
print(type(temp))  # <class 'tuple'>
temp = []
print(type(temp))  # <class 'list'>
temp = ()
print(type(temp))  # <class 'tuple'>
temp = (1,)
print(type(temp))  # <class 'tuple'>

Example 2:

print(8 * (8))  # 64
print(8 * (8,))  # (8, 8, 8, 8, 8, 8, 8, 8)

Of course, you can create a two-dimensional tuples:

nested = (1, 10.31, 'python'), ('data', 11)
print(nested)
# ((1, 10.31, 'python'), ('data', 11))

Tuple it can be indexed by integers (Indexing) and slice (slicing), loosely speaking, the former is to obtain a single element, which is to obtain a set of elements. Then the two-dimensional example of the above-tuples, the index look code:

print(nested[0])
# (1, 10.31, 'python')
print(nested[0][0], nested[0][1], nested[0][2])
# 1 10.31 python

Look at the slice code:

print(nested[0][0:2])
# (1, 10.31)

2. Update and delete a tuple

Example 1:

week = ('Monday', 'Tuesday', 'Thursday', 'Friday')
week = week[:2] + ('Wednesday',) + week[2:]
print(week)  # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

Example 2:

t1 = (1, 2, 3, [4, 5, 6])
print(t1)  # (1, 2, 3, [4, 5, 6])

t1[3][0] = 9
print(t1)  # (1, 2, 3, [9, 5, 6])

There tuples can not be changed (immutable) nature, and therefore can not give a direct element of the tuple assignment, but as long as the tuple elements can be changed (mutable), then we can change their elements directly, pay attention to this assignment with its different elements.

3. tuple relational operators

Comparison Operators
Logical Operators
concatenation operator +
repetition operator *
a member of relational operators in, not in
the tuple stitching (concatenate) two ways, with "Plus +" and "multiplication sign *", the former head and tail spliced, The latter copy stitching.

t1 = (2, 3, 4, 5)
t2 = ('老马的程序人生', '小马的程序人生')
t3 = t1 + t2
print(t3)  # (2, 3, 4, 5, '老马的程序人生', '小马的程序人生')

t4 = t2 * 2
print(t4)  # ('老马的程序人生', '小马的程序人生', '老马的程序人生', '小马的程序人生')

4. The built-in method

Tuple is not to change the size and content, so only two ways to count and index.

t = (1, 10.31, 'python')
print(t.count('python'))  # 1
print(t.index(10.31))  # 1

count ( 'python') is a record of the element occurs several times in a tuple t, it is clear that the primary
index (10.31) is to find the index of the element in tuple t is clearly 1

The decompression tuple

Decompression (the unpack) a dimensional set element (Several elements define several variables left parenthesis)

t = (1, 10.31, 'python')
(a, b, c) = t
print(a, b, c)
# 1 10.31 python

Extracting a two-dimensional tuples (a tuple according tuple structure to define a variable)

t = (1, 10.31, ('OK', 'python'))
(a, b, (c, d)) = t
print(a, b, c, d)
# 1 10.31 OK python

If you only want a few elements of tuples with wildcards "*", the English called wildcard, on behalf of one or more elements in a computer language. The following example is a plurality of element threw the rest variables.

t = 1, 2, 3, 4, 5
a, b, *rest, c = t
print(a, b, c)  # 1 2 5
print(rest)  # [3, 4]

If you do not care about rest variable, then use the wildcard "*" underlined "_", examples are as follows:

a, b, *_ = t
print(a, b)  # 1 2

Guess you like

Origin blog.csdn.net/NumberOneStudent/article/details/102691142