Learning Python notes --- List Brief

Lists: a series arrangement of elements in a certain order. You can create all bear with alphabet letters, numbers 0 to 9 or a list of all the names of family members; also anything that can be added to the list, there is no relationship between its elements.

List

In Python, square brackets ([]) to indicate the list by commas to separate the elements therein.

Such as:

bicycles=['trek','cannondale','redline','spcialized']

Access Control Lists:

List is an ordered collection, so to access any element of the list, only the position or index elements need to tell Python can be. To access the list elements, it may indicate the name of the list, and then pointed index of the element, and place it in square brackets.

Such as:

bicycles=['trek','cannondale','redline','specialized']
print(bicycles[0])

Index starts at 0 instead of 1. -1 designated index allows Python returns the last list element; so, returns -2 penultimate index list element, the index returns -3 last third list element.

Modify the list of elements

Modify the list of elements of the syntax elements in the access list syntax is similar. To modify the list of elements, you can specify a list of names and index you want to modify elements, and then specify a new value of the element.

Such as:

motorcycles=['honda','yamaha','suzuki']
motorcycles[0]='ducati'

Add an element in the list

When you add a new element in the list, the easiest way is to attach elements to the end of the list.

The method append () to add an element to the end of the list.

Such as:

motorcycles=['honda','yamaha','suzuki']
motorcycles.append('ducati')

The method insert () to add a new element in any position in the list.

Such as:

motorcycles=['honda','yamaha','suzuki']
motorcycles.insert(2,'ducati')

Remove elements from the list

If you know where you want to delete the elements in the list, use the del statement.

Such as:

guests=['wang','zhang','chen','meng']
del guests[1]

Method pop () to remove an item at the end of the list, and then allow you to use it . The term pop (pop) from this analogy: like a stack list, and delete the end of the list of elements equivalent to pop the top element.

Such as:

motorcycles=['honda','yamaha','suzuki']
popped_motorcycles=motorcycles.pop()
print(popped_motorcycles)

pop () can be used to delete the list of elements in any location, simply specify the element index to be deleted in parentheses.

PS: If you delete an element from the list, no longer used in any way to cut it, use the del statement; if you can continue to use it after you delete elements, you use pop ().

 

If you only know the value of elements to be deleted, use the method remove ().

Such as:

motorcycles=['honda','yamaha','suzuki']
motorcycles.remove('yamaha')

Use remove () When you remove an element from the list, you can then use its value, but requires advance deposit to the value of the variable.

Such as:

motorcycles=['honda','yamaha','suzuki']
too_expensive='yamaha'
motorcycles.remove(too_expensive)

PS: method remove () deletes only the first specified value. If you want to remove multiple values ​​may appear in the list, you need to use a loop to determine whether to delete all such values.

 

Sort the list permanently

Python method sort () can more easily sort the list . This sort is permanently changed the order of the list of elements, can no longer return to the original sort order.

Such as: 

cars=['bmw','audi','toyota','subaru']
cars.sort()

May be performed with the alphabetical list of elements arranged in the reverse order, therefore, only () method to pass parameters to Sort Reverse = True .

Such as:

cars=['bmw','audi','toyota','subaru']
cars.sort(reverse=True)

Temporary sort the list

To retain the original order in the list of elements, while they are presented in a specific order, the function may be used the sorted () .

Such as:

cars=['bmw','audi','toyota','subaru']
print(cars)
print('Here is the original list:')
print(sorted(cars))

To carry out the letter display list in reverse order, may be () function is passed to a function sorted reverse = True

Such as:

cars=['bmw','audi','toyota','subaru']
print(sorted(cars,reverse=True))

Backwards Print List

To reverse the order of the list of elements, the method may be used Reverse () . Note: reverse () does not refer to the letter arranged opposite to the order of the elements, but merely reversing the order list elements.

Such as:

cars=['bmw','audi','toyota','subaru']
cars.reverse()

The method reverse () permanently modify the order of the list of elements, but can be restored to its original sort order at any time, do this, simply call to reverse the list again () can be.

 

Determining the length of the list

Use len () function can be quickly informed of the length of the list.

Such as:

cars=['bmw','audi','toyota','subaru']
print(len(cars))

1 hour from the start, it is determined length Python calculations list listing the number of elements, the difference does not encounter an error.

Guess you like

Origin www.cnblogs.com/Slayers-Z/p/11789676.html