python learning Notes (c) list

python learning Notes (c) list

1, the list has shown signs of
a list of a series of arranged in a specific sequence elements, elements of the same type in the list is arbitrary, without any connection between each other, which we know to be more flexible than an array in C ++, thus function It will be more powerful.

languages = ['c', 'python', 'java', 'ruby']

python, a ([]) using the flags list square brackets, elements separated by commas (,).

2, access lists and access an array of similar, usually accessed through position or index of the element to remind here that the novice, the index is starting from 0 (most programming languages ​​follow this principle), so if you want to access list The first element, should be

languages = ['c', 'python', 'java', 'ruby']
element_first = languages[0]

After the turn of the elements can be pushed back. (Tips: element will need access arrangement position is the index minus one).
python also provides a special syntax elements for the end of the visit, the unknown is too long or when we want to visit the wonders length of the list.

languages = ['c', 'python', 'java', 'ruby']
element_last_first = languages[-1]	#倒数第一个元素
element_last_second = languages[-2]	#倒数第一个元素
element_last_third = languages[-3]	#倒数第一个元素

And so on.

3, modify the list of elements in
the list is dynamic, powerful python function is also reflected in the list after you create, you can run the program with the additions and deletions of elements.
Similar modify elements and access list syntax correction value directly assigned to index can be modified elements.

languages = ['c', 'python', 'java', 'ruby']
languages[0] = 'c++'

4, the list of elements added
python added list element has two main functions:
A. .append () will be appended to the end of the list element

languages = ['c', 'python', 'java', 'ruby']
languages.append('c++')
# 列表变为:
# ['c', 'python', 'java', 'ruby', 'c++']

.append () function is typically applied to create a list dynamically

languages = []	#先创建一个空列表,留待使用
# 动态修改列表
languages.append('c')
languages.append('python')
languages.append('java')
languages.append('ruby')

B. .insert () is inserted in the list of elements
which can be added at any position in the list of the new element (because the list is dynamic, static array is different from a predetermined size), and only need to provide an index value to be added elements, i.e. can.

languages = ['c', 'python', 'java', 'ruby']
languages.insert(1, 'c++')
# 列表变为:
# ['c', 'c++', 'python', 'java', 'ruby']

Value 'c ++' is inserted into a second position in the list, the list except for the first element, the other elements of the original right by one position.

5, delete the list of elements
python has three functions can be used to delete elements
A. del
long as we know the index of the element, del list elements can be deleted at any position, but the result is irreversible, ie once deleted, you can not access it a.

languages = ['c', 'python', 'java', 'ruby']
del languages[1]
# 列表变为:
# ['c', 'java', 'ruby']

.Pop B. ()
POP is the meaning of pop, pop-up will usually assign a value to a new variable value in the list will also be deleted.
In the index using .pop () need to pass parameters to a function, the parameters need to be ejected elements, if there is no mass participation, the default pop-up end of the element. Common usage is:

languages = ['c', 'python', 'java', 'ruby']
language_popped = languages.pop(1)
# 列表变为:
# ['c', 'java', 'ruby']
# language_popped 中存储 'python' 以供继续使用
# languages.pop() 将弹出 'ruby'

C. .remove () to remove elements based on the value
when we know the value of the element to be deleted without knowing its index, you can use .remove () function, but the function is the default start traversed from the first element of the list, when there when the specified element, delete, and then stop, that is, if there is a list of the same value back, .remove () function when not removed (this solution will be described in the following sections).
And since we already know the value of the element, after we remove the element can still use this value.

languages = ['c', 'python', 'java', 'ruby']
to_remove_language = 'python'
languages.remove(to_remove_language)	
# languages.remove('python')
# 列表变为:
# ['c', 'java', 'ruby']

6, sort the list by
A. .sort () sort of permanent (irreversible ordering)

languages = ['c', 'python', 'java', 'ruby']
languages.sort()
# 列表变为:
# ['c', 'java', 'python', 'ruby']

Obviously, lexicographically sort the list permanent, if you want to reverse lexicographical ordering, we need to introduce a parameter reverse = True

languages = ['c', 'python', 'java', 'ruby']
languages.sort(reverse=True)
# 列表变为:
['ruby', 'java', 'python', 'c']

Or using .reverse () function, but also a permanent order.

languages = ['c', 'python', 'java', 'ruby']
languages.reverse()
# 列表变为:
['ruby', 'java', 'python', 'c']

B. sorted () temporary sorting line
we can use this function to sort the list by temporary assigned to a new variable, this will not affect the order of the list.

languages = ['c', 'python', 'java', 'ruby']
sorted_languages = sorted(languages)
print(languages)
print(sorted_languages)
# 运行结果如下:
# ['c', 'python', 'java', 'ruby']
# ['c', 'java', 'python', 'ruby']

The introduction of an inverse lexicographic order parameter reverse = True

7, the list of determined length
len () function returns the length of the list

languages = ['c', 'python', 'java', 'ruby']
length_languages = len(languages)
print(length_languages)
# 输出为:4
发布了3 篇原创文章 · 获赞 3 · 访问量 258

Guess you like

Origin blog.csdn.net/weixin_44650011/article/details/104083989