Author: Jiang Xia
| zhihu: www.zhihu.com/people/1024…
| GitHub:github.com/JiangXia-10…
| CSDN:blog.csdn.net/qq_4115394…
| the nuggets: juejin. Cn/user / 651387…
| public no. : 1024 notes
This article contains 1169 words and takes 8 minutes to read
Built-in data structures in Python include list, tuple, dict, and set. These four data structures and basic data Types (integers, floating point numbers, etc.) are collectively referred to as built-in Types. Lists and tuples are ordered lists and are called sequences. A List is a comma-separated set of data enclosed in square brackets. The elements of a list can be of any type.
This summarizes the use of lists in Python, and the comments and analysis are all in the code.
Declare a list... >>> person=[" name "," name "," name "] >>> > > > print (person) [' zhang ', 'bill', 'Cathy'] > > > # output the length of the list... >>> print(" this company has %s people "%len(person)) >>> # add element to list, add element to last... >>> Person.append (" person.append ") >>> # print the added list... > > > print (person) [' zhang ', 'bill', 'Cathy', 'zhao six] > > > # output after adding the length of the list... >>> print(" this company now has %s individuals "%len(person)) >>> print(" this company now has %s individuals" %len(person)) >>> print(" this company now has %s individuals "%len(person)) >>> person. Insert (2," lib ") >>> > > > print (person) [' zhang ', 'bill', 'liu bei', 'Cathy', 'zhao six] > > > # output after inserting the length of the list... >>> print(" this company now has %s person "%len(person)) >>> print(" this company now has %s person" %len(person)) >>> >>> print(person[1]) print(person[1]) >>> print(person[len(person)-1]) print(person[len(person)-1]) >>> print(person[-1]) print(person[-1]) print(person[-1]) > > > person [0] = "zhu yuanzhang" > > > print (person) [' zhu ', 'bill', 'liu bei', 'Cathy', 'zhao six] > > > > > > print (len (person)) 5 > > > # remove elements, with the pop (), Delete the last element, Zhao Six... > > > print (person. Pop (), zhao six > > > print (person) [' zhu ', 'bill', 'liu bei, 'Cathy'] > > > > > > print (len (person)) 4 > > > # use the remove (specified element) method to delete the elements in the list... > > > person. Remove (" zhu ") > > > print (person) [' bill ', 'liu bei, >>> >>> print(len(person)) 3 >>> # print(len(person)) >>> del person[0] >>> >>> print(len(person)) 2 >>> #pop(n)n For example, delete the second element, Li Si... >>> print(person) [' lib '] >>> >>> print(len(person)) 1 >>> You can also nest lists... > > > person = [23,23.9, "zhang", [" liu ", "guan yu", "zhang fei"],] > > > > > > print (person) [' zhang, 23, 23.9, [' liu bei, guan yu, 'zhang Fei ']] >>> # again we can do this assignment... > > > s = "liu2 bei4" > > > a = 23 > > > b = 23.8 > > > l = [" liu ", "guan yu", "zhang fei"]] > > > person = (s, a, b, l) > > > print (person) [' liu, 23, 23.8, [' liu bei, "Guan yu", "zhang fei"]] > > > > > > print (len (person)) 4 > > > # output list... > > > print (person) [1] [' liu bei, "guan yu", "zhang fei"]] > > > # output person l a list of the first element of the list Liu bei... Print (person[-1][0]) print(person[-1][0]) > > > person [1]. Append (" zhaoyun ") > > > print (person) [' liu, 23, 23.8, [' liu bei, guan yu, zhang fei, 'zhaoyun]] > > > person [1]. The insert (1, "d") > > > > > > print (person) [' liu, 23, 23.8, [' liu bei', 'd', 'guan yu, zhang fei, The length of the personList is always the same regardless of how you operate on the list element in person... >>> print(len(person)) 4 >>> > > > person1 = [" liu ", "zhang fei"] > > > person2 = [1, 2] > > > > > > print (person1 + person2) [' liu bei, zhang fei, 1, 2] > > > # judge whether an element in a list of true... >>> print(2 in [1,2,3]) True >>> > > > person1 = [" liu ", "zhang fei"] > > > > > > print (person1 * 4) [' liu bei, zhang fei, liu bei, zhang fei, liu bei, zhang fei, liu bei, zhang fei '] > > > flip # element output [' zhaoyun, 'Guan Yu ',' Zhang Fei ', 'Liu Bei'... = > > > person1 [" liu ", "zhang fei", "guan yu", "zhaoyun"]] > > > person1. The reverse () > > > > > > print (person1) [' zhaoyun, guan yu, zhang fei, >>> # iterate over all elements in the output list... = > > > person [" zhang ", "li si", "detective"] > > > > > > for the item in person:... print(item) ... Three, three, four, fiveCopy the code
That’s how lists are used in Python!
Finally, welcome to pay attention to the public number: 1024 notes, free access to massive learning resources (including video, source code, documents)!
Other recommendations:
- Getting started with Python (3) : Using tuples
- Getting started with Python (4) : Using sets
- Getting Started with Python (5) : Dict usage
- Getting Started with Python 6: Calling custom functions
- Getting started with Python (I) : String formatting