Python Basics Four

Source: Internet
Author: User

List: Python one of the underlying data types

Other languages also have a list of concepts,js--> arrays, indexable, can be sliced, can be added in step

A list can store a large amount of data 32-bit Python is limited to 536,870,912 elements, and 64-bit Python is limited to 1.,152,921,504,606,85e,+18 elements.

li = [1, 'txt', {'name': 'python', 'author':'龟叔'}, [2, 3, 4], (5, 6)]
First: Index, slice, slice + step.
>>> li[2]{'name': 'python', 'author': '龟叔'}>>> li[2:][{'name': 'python', 'author': '龟叔'}, [2, 3, 4], (5, 6)]>>> li[-2::-1][[2, 3, 4], {'name': 'python', 'author': '龟叔'}, 'txt', 1]
Second: Adding and deleting changes
li2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Add Append end Append
>>> li2.append('13')>>> li2['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '13']
Insert Insertion
>>> li2.insert(6, 'Jul')>>> li2['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '13']
Extend iterative Append
>>> li2.extend('abc')>>> li2['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '13', 'a', 'b', 'c']>>> li2.extend(['aa', 'bb'])>>> li2['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '13', 'a', 'b', 'c', 'aa', 'bb']
Delete Pop by index to delete, return the deleted element
li3 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']>>> li3 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']>>> li3.pop(0)'Jan'>>> li3['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Remove succeeds without a return value by element deletion, unsuccessful error
li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3.remove('Feb')>>> li3['Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3.remove('Feb')Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: list.remove(x): x not in list
Clear clears the original variable to an empty array
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3.clear()>>> li3[]
Del by index, slice, and can delete entire array in memory
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> del li3[0]>>> li3['Mar', 'Apr', 'May', 'Jun', 'Jul']>>> del li3[-2:]>>> li3['Mar', 'Apr', 'May']>>> del li3>>> li3Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'li3' is not defined
Change by index
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3[0] = 'February'>>> li3['February', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
By slicing: The original array may be expanded or may be scaled down
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3[:2] = [1, 2, 3, 4]>>> li3[1, 2, 3, 4, 'Apr', 'May', 'Jun', 'Jul']>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3[1:4] = [1, 2]>>> li3['Feb', 1, 2, 'Jun', 'Jul']
According to Slice + step: Only one by one corresponds
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3[1:4] = [1, 2]>>> li3['Feb', 1, 2, 'Jun', 'Jul']
Check: index, slice, slice + step for loop
>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> for i in li3:...     print(i)...FebMarAprMayJunJul
Other methods
# len>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> len(li3)6# count>>> li3.count('Feb')1# index 如同str一样:没有会报错>>> li3.index('bad')Traceback (most recent call last):  File "<stdin>", line 1, in <module>ValueError: 'bad' is not in list>>> li3.index('Mar')1# sort()-->从小到大,当有reverse=True时-->从大到小>>> li3 = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']>>> li3.sort()>>> li3['Apr', 'Feb', 'Jul', 'Jun', 'Mar', 'May']>>> li3.sort(reverse=True)>>> li3['May', 'Mar', 'Jun', 'Jul', 'Feb', 'Apr']

Python Basics Four

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.