Basic operation Python-- array (list)

Array in python is dynamic, so you can freely array insert, delete elements operate.

1. Modify the elements
that have nothing to say, and java is the same, directly modify fine.

arr = ['a','b','c']
arr[0]='ff';
print(arr)


2. At the end of the list append additional elements 
we have said at the beginning of the array in python is dynamic, so-called dynamic is its length is dynamic.

arr = ['a','b','c']
arr.append('d');
print(arr)

3. Insert element INSERT (index)
ARR = [ 'A', 'B', 'C']
arr.insert (. 1, 'AA');
Print (ARR)


4. del delete elements 
ARR = [ 'A', 'B', 'C']
del ARR [0]
Print (ARR)


5. Use the pop delete elements 
pop and del difference is that, pop method returns you delete an element, so that you can continue to use the element is removed, such as:

arr = [ 'a', 'b', 'c']
charm arr.pop = ();
print (r)
print (begging);

If there is pop method parameter index, the index position of the element is deleted, if there is no index parameter, the default delete the list the last element.

6. remove elements remove the element values based on
if we do not know the position of the element we want to delete, and only know the value of the element, then use remove:

arr = ['a','b','c']
arr.remove('b');
print(arr);

But one thing to note, if the value of the list of repeat, and you want to use remove delete it, you can only delete the first value of the index forward.

7. With respect to access the value end of the element
values of how we access the end of the list without knowing the length of the list when it? Or penultimate value. . . . .

We can only get to know the length of the list in java to operate, then the python is how to do it?

arr = ['a','b','c']
print(arr[-1]);
print(arr[-2]);


8. Sort
Python sort () method allows you to more easily sort the list. Suppose you have a list of cars, cars and make them in alphabetical order. To simplify this task, we assume that all values in the list are lowercase.

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

Sort () method to permanently modify the order of the list elements. Now, the car is in alphabetical order, can no longer return to its original sort order.

You can also use reverse = True argument to try to sort in reverse order of the characters:

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


Similarly, modifications to the order list element is permanent. 

The use of temporary sort sorted ():

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


May also be () passing arguments to functions sorted reverse = True.

9. Print List backwards
attention just print the list to show it backwards, rather than ordered, using reverse method

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

 10. Get list length
cars = [ 'BMW', 'Audi', 'Toyota', 'Subaru']
Print (len (cars));

 

Published 352 original articles · won praise 115 · views 130 000 +

Guess you like

Origin blog.csdn.net/Aidam_Bo/article/details/104679856