List
Lists are one of the most commonly used data types and can be easily stored, modified, and manipulated by the list.
1. Defining the list
Fruits = [' apple ', ' banana ', ' orange ']
2. Use the subscript to access the elements in the list, and the subscripts count from 0
>>> fruits[0] ' Apple ' >>> fruits[2] ' orange ' >>> fruits[-1] ' orange ' >>> fruits[-2] ' Banana
3. Slicing
>>> fruits = [' apple ', ' banana ', ' orange ', ' peal ', ' Grape ']>>> fruits[1:4] #取下标1到下标4之间的数, Includes 1 but not including 4[' banana ', ' orange ', ' peal ']>>> fruits[1:-1] number between #取下标1至-1, excluding -1[' banana ', ' orange ', ' peal '] >>> Fruits[0:3] #从头开始取, excluding 3[' apple ', ' banana ', ' orange ']>>> fruits[:3] #和上句一样 [' Apple ', ' Banana ', ' orange ']>>> fruits[3:] #从下标3到最后, this is the only way to take [' peal ', ' Grape ']>>> fruits[0::2] # From the beginning, the step is 2, that is, take one [' Apple ', ' orange ', ' Grape ']>>> fruits[::2] #和上句一iy [' Apple ', ' orange ', ' grape ']
4. Append, append ()
>>> fruits[' apple ', ' banana ', ' orange ', ' peal ', ' Grape ']>>> fruits.append (' Newpeach ') >>> fruits[' apple ', ' banana ', ' orange ', ' peal ', ' Grape ', ' Newpeach ']
5. Insert element, insert ()
Insert a watermelon at subscript 1 (watermelon)
[' Apple ', ' banana ', ' orange ', ' peal ', ' Grape ', ' Newpeach ']>>> fruits.insert (1, ' watermelon ') >>> fruits[' apple ', ' watermelon ', ' banana ', ' orange ', ' peal ', ' Grape ', ' Newpeach '
6. Modifying elements in a list
Modify the banana to cherry cherry
>>> fruits[' apple ', ' watermelon ', ' banana ', ' orange ', ' peal ', ' Grape ', ' Newpeach ']>>> fruits[2]= ' Cherry ' >>> fruits[' apple ', ' watermelon ', ' cherry ', ' orange ', ' peal ', ' Grape ', ' Newpeach '
7. Delete
Pop () returns the element after the last element is deleted by default
>>> fruits[' apple ', ' watermelon ', ' cherry ', ' orange ', ' peal ', ' Grape ', ' Newpeach ']>>> del fruits[2]< c0/> #删除第二个元素 >>> fruits[' apple ', ' watermelon ', ' orange ', ' peal ', ' Grape ', ' Newpeach ']>>> Fruits.remove (' orange ') #删除指定的元素 >>> fruits[' apple ', ' watermelon ', ' peal ', ' Grape ', ' Newpeach ']>> > Fruits.pop () #删除最后一个元素 ' Newpeach ' >>> fruits[' apple ', ' watermelon ', ' peal ', ' Grape '
8. Extended Extend ()
>>> fruits[' apple ', ' watermelon ', ' peal ', ' grape ']>>> vegetable = [' radish ', ' cabbage ', ' cucumber '] >>> fruits[' apple ', ' watermelon ', ' peal ', ' Grape ']>>> vegetable[' radish ', ' cabbage ', ' cucumber ' >>> fruits.extend (vegetable) >>> fruits[' apple ', ' watermelon ', ' peal ', ' grape ', ' radish ', ' cabbage ', ' Cucumber ']
9. Copy Copy ()
[' Apple ', ' watermelon ', ' peal ', ' grape ', ' radish ', ' cabbage ', ' cucumber ']>>> fruits2 = fruits.copy () >> > fruits2[' apple ', ' watermelon ', ' peal ', ' grape ', ' radish ', ' cabbage ', ' cucumber '
10. Statistics COUNT ()
>>> fruits.count (' Apple ') 1
11. Sorting sort () and flip reverse ()
>>> fruits[' apple ', ' watermelon ', ' peal ', ' grape ', ' radish ', ' cabbage ', ' cucumber ']>>> fruits.sort () >>> fruits[' apple ', ' cabbage ', ' cucumber ', ' grape ', ' peal ', ' radish ', ' watermelon ']>>> Fruits.reverse () >>> fruits[' watermelon ', ' radish ', ' peal ', ' grape ', ' cucumber ', ' cabbage ', ' apple '
12. Get Subscript Index ()
[' Watermelon ', ' radish ', ' peal ', ' grape ', ' cucumber ', ' cabbage ', ' apple ']>>> fruits.index (' Apple ') 6# Returns only the first subscript found
More Python Basics-List related articles please follow topic.alibabacloud.com!