Learning Python Basics (b)

Hello everyone, continue to learn the basics of Python today, by the way remind you, you learn more than new knowledge, but also timely to recall previously learned knowledge.
Today, the list of major learning-related knowledge:
What is the list
representation list
access list elements
concatenated list of
deleted and added a list of
reverse output
sorted list

1. What is a list

What is a list, the list is a series of arranged in a specific sequence elements. For example, it can be a list of letters, can also be a list of numbers, but also can be no relationship of any list element composed. Given a list comprising a plurality of elements, usually represented by a complex name, such as (letters).

2, representation of the list

Python, with [] to represent the list by commas to separate the elements therein. For example:
Numbers = [1,2,3,4,5]
Letters = [ 'A', 'B', 'C']

3, the access list elements

List is an ordered collection, so to access a list element, the element position and only need to tell python to the index.
In Python, the index of the first element of the list is 0 instead of 1. This is true in most programming languages. Therefore, each element can be found through the index of way, but Python provides a special syntax for accessing the last element of a list. Designated by the index of -1 allows Python returns the last element of a list. Further, the index may be represented by a range represented colon, thereby outputting a new list, for example as follows:
Here Insert Picture Description

4, splicing list

① The "+" operator splicing completion list. As follows:
Here Insert Picture Description
② method using extend, change the above example is as follows:
Here Insert Picture Description

5, delete, and add the list

1) additive element
① add a new element to the end of the list, using the append (new) The method, the following examples:
Here Insert Picture Description
② insert elements in the list, i.e. index and element value required. With insert (index number, the element value), for example:
Here Insert Picture Description
if the index number is inserted over the length of the list, is also automatically filled in the following example, the element 10 is inserted into the index value, the entire length of the list of queries 7, such as a list of output:
Here Insert Picture Description
2) delete list
①del list [index], you can also delete the list itself. Such as:
Here Insert Picture Description
② POP (index); Returns the element is deleted, the default deletes the last element. The following example, delete the index value 2 of the element, the element 2 returns the index value of 3.
Here Insert Picture Description
③ The element value is removed, Remove ( 'element'), i.e. to find the start element list, and then removed from the element. In the following example:
Here Insert Picture Description
④clear (); Clear list. In the following example:
Here Insert Picture Description

6, reverse output

Reverse (); inverted output list element, in the following example:
Here Insert Picture Description

7, sorted list of

1、 使用方法sort()对列表进行永久性排序,永久性的修改了列表元素的排列顺序,再也无法恢复到愿来的顺序。
比如对乱序的字母表进行排序,如下:
Here Insert Picture Description
又比如对一组列表按首字母排序,也可以使用sort()方法,如:
Here Insert Picture Description
2、 使用sorted()对列表进行临时排序。
要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted() 。函数sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。如下例:
Here Insert Picture Description
没有永久性改变顺序,只是临时性改变顺序。
Sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数。其中iterable表示可以迭代的对象,例如可以是dict.items()、dict.keys()等,key是一个函数,用来选取参与比较的元素,reverse则是用来指定排序是倒序还是顺序,reverse=true则是倒序,reverse=false时则是顺序,默认时reverse=false。
1) Sorted函数按key值对字典排序。
Here Insert Picture Description
2) Sorted函数按照value值对字典排序。
要对字典的value排序则需要用到key参数,在这里主要提供一种使用lambda表达式的方法。lambda x:y中x表示输出参数,y表示lambda函数的返回值),所以采用这种方法可以对字典的value进行排序。注意排序后的返回值是一个list,而原字典中的名值对被转换为了list中的元组。
如下例:
Here Insert Picture Description
Person.items where () is a person actually iterative converted into an object, the object is a point down elements ( 'wangxin': 25, ' liula': 21, 'lihui': 29, 'zhaole': 20). items () method of the dictionary elements into a tuple, and where the key parameter corresponding to the lambda expression means selecting tuples in the second element as comparison parameter, such as writing (key = lambda iitem: item [ 0] , then the first element is selected as a comparison object, that is a key for comparison), the method and sorting method of the comparison result the same key, but the latter is a result of output tuples, while the former is based on the result of the order output .
In the following example:
can be clearly seen, the output of both the ordering is the same, but different output parameters.
Here Insert Picture Description
Well, today's share to this end, I wish you all a happy life Oh!
Reference blog a
reference blog two

Released three original articles · won praise 3 · Views 135

Guess you like

Origin blog.csdn.net/sinat_40631989/article/details/104064228