Python study notes 04 [list]

Content list can be made of different types, such as list2 = [1,2,3, 'asdf', 'sd']

4.1.1 obtain a single value in the list of subscripts

spam=['cat', 'dog', 'rat', 'elephant']
spam[1]
'dog'

4.1.2 negative subscript

Subscript 0: represents the first value of the previous number

The Table 1: number represents the first value from the post

spam=['cat', 'dog', 'rat', 'elephant']
spam[-1]
'elephant'

spam[-2]
'rat'

4.1.3 The use of sub-sections made the list

. 1 from spam [. 1: 3 ] # is not included in Table 3 of the element
 2 [ ' Dog ' , ' RAT ' ]

4.1.4 Use len () to obtain the length of the list

4.1.5 change the value of the subscript list

spam[1] = 'change'

4.1.6 list of connection and copy the list

[1,2,3] + [5,6,7,8]

[1,2,3]*3

4.1.7 using the del statement to remove values ​​from the list

spam=['cat', 'dog', 'rat', 'elephant']
del spam[3]
spam ###
['cat', 'dog', 'rat']

4.2 List Use

By typing, added to the list

 1 catNames = []
 2 while True:
 3     print('Enter the name of cat #' + str(len(catNames) + 1) + ' Or enter nothing to stop: ')
 4     name = input()
 5     if name == '':
 6         break
 7     catNames = catNames + [name] #注意此处用法!!!
 8 print('The cat names are:')
 9 for catName in catNames:
10     print(' ' + catName)

4.2.1 list for circulation

1 for i in [1,2,3,4,5,6,7,8,9]
2     print(i)

4.2.2 in and not in operators

Determining whether a value in the class table

4.2.3 Multiple Assignment skills

>>> cat = ['fat','black','loud']
>>> size, color, disposition = cat
>>> print(size,color,disposition, sep='::')
fat::black::loud

4.2.4 Enhanced assignment

+=, -=, *=, /=, %=

4.4 Method 

4.4.1 index () method to find the value in the list: spam.index ( 'hello'), a plurality of hello list, returns the index of the first hello

4.4.2 append () and insert () method adds the value in the list

spam=['cat', 'dog', 'rat', 'elephant']
spam.append('moose')
spam.insert(1, 'chicken')

 4.4.3 remove () method removes value from the list

>>> spam.remove('rat')
>>> spam
['cat', 'dog']

Del statement can also be used to remove some value, how to distinguish between:

If you know the value of the index you want to delete the list, use the del statement

If you want to know the value to delete from the list, use the remove () method

 

4.4.4 Using sort () method to sort the list

Numeric or character lists can be sorted

1 >>> spam=['cat', 'dog', 'rat', 'elephant']
2 >>> spam.sort()
3 >>> spam
4 ['cat', 'dog', 'elephant', 'rat']
>>> spam.sort(reverse=True)
>>> spam
['rat', 'elephant', 'dog', 'cat']

 

 

sort () method Caution:

  • The sort method to sort the list on the spot
  • sort method can not contain both digital and contains a sorted list of strings
  • When the sort method sorts the character string, the use of 'ASCII' character sequence, i.e., capital letters before lowercase letters.
    • If you need to sort the order in accordance with the ordinary dictionary, just in the sort called keyword key to str.lower, as follows:
    • . 1 >>> from spam = [ ' Y ' , ' T ' , ' F ' , ' A ' ]
       2 >>> spam.sort ()
       . 3 >>> from spam
       . 4 [ ' A ' , ' T ' , ' F ' , ' Y ' ] # front capital letters
       . 5 >>> spam.sort (Key = str.lower)
       . 6 >>> from spam
       . 7 [ 'A', 'F ' , ' T ' , ' Y ' ] # in lexicographic order, shuffling the case, but does not actually change the value of the list

 Similar type of list 4.6: string and tuple

  • String is a list of individual text characters

4.6.1 variable data types and immutable

4.6.2 tuple data type: the tuple defined using (), instead of [], but still to obtain tuple element by [index]

>>> eggs = ('hello','kitty',2)
>>> eggs[2]
2
>>> eggs[1]
'kitty'
  • Lists are mutable data types
  • String data type is immutable
  • Tuples are immutable

 Contrast tuples and lists of:

  • If you are using unchanging sequence, tuple
  • Python tuple is optimized for use codes tuples than using a list of code more quickly

4.6.3 with the list () and tuple () function to convert the type

1 >>> tuple(['chicken','fish','dogs'])
2 ('chicken', 'fish', 'dogs')
3 
4 >>> list(('chicken', 'fish', 'dogs'))
5 ['chicken', 'fish', 'dogs']

4.7 references

4.7.1 Passing by reference

. 1 >>> spam2
 2 [ ' A ' , ' B ' , ' C ' , ' D ' , [. 1, 2,. 3,. 4 ]]
 . 3 >>> spam3 = spam2 # passed by reference 
. 4 >>> spam3
 . 5 [ ' A ' , ' B ' , ' C ' , ' D ' , [. 1, 2,. 3,. 4 ]]
 . 6 >>> spam3 [. 1] = 'dd ' 
7>>> spam3
 . 8 [ ' A ' , ' dd ' , ' C ' , ' D ' , [. 1, 2,. 3,. 4 ]]
 . 9 >>> spam2 # spam3 changes affecting spam2 
10 [ ' A ' , ' dd ' , ' C ' , ' D ' , [. 1, 2,. 3,. 4]]

copy 4.7.2 copy module () function and the deepCopy () function

Using the copy function, instead of using "=" to be passed by reference, two major undesirable variables point to the same reference, changes do not affect each other mutually.

 1 >>> import copy
 2 
 3 >>> spam = ['A','B','C','D']
 4 >>> cheese = copy.copy(spam)
 5 >>> spam == cheese
 6 True
 7 >>> cheese[1] = 42
 8 >>> spam
 9 ['A', 'B', 'C', 'D']
10 >>> cheese
11 ['A', 42, 'C', 'D']
12 >>> spam == cheese
13 False

After copy.copy (), spam and cheese pointing to a different list, so changes in one another spam and cheese go well together.

Guess you like

Origin www.cnblogs.com/wooluwalker/p/11622801.html