Growth 4-List Series knowledge

Source: Internet
Author: User

Holiday is over, not enough to come back to work, slacking off for a week, knowledge or to be mended back! Don't say much nonsense, just start! List knowledge!

1, create a list, on the example! Note that the brackets type "[]", except for the number of characters to use ' ' ' cause ha ~

>>> a=[3,'kk', 4.3]>>> a['KK  ', 4.3]

2, add the list element!

>>> a.append ('Michelle')>>> a['KK  "'Michelle')

Note Here can only add a single, can not be added more than once, do not believe you look! But I still do not want to forget, I intend to use the form of a list to insert two numbers, the results are still inserted as a list, OK, I still think of another way!

 >>> List1=[8, 7, 6, 4, 2, 1]  >>>  list1[ 8, 7, 6, 4, 2, 1  >>> List1.append (33,22 "  <PYSHELL#79>   ", Line 1, in  <module> List1.append ( 33,22) Typeerror:append () takes exactly one argument ( 2  given)  >>> list1.append ([33,22])  >>> list1[ 8, 7, 6, 4, 2, 1, [+]] 

3, the way is to expand the list command! extend! Come on, you! A blockbuster? I explain to you that the logic of this piece of code is to define the list-add an element-in the list force to extend multiple elements, extend is the solution to add multiple elements, but notice that when I directly enclose multiple characters directly inside the extend, the runtime display is wrong, This is because there is no added list bracket wow ==> "[]"

>>> a=[3, ' kk ', 4.3]
>>> A
[3, ' kk ', 4.3]
>>> a.append (' Michelle ')
>>> A
[3, ' kk ', 4.3, ' Michelle ']
>>> a.extend (' Hello ', ' Hi ')
Traceback (most recent):
File "<pyshell#4>", line 1, in <module>
A.extend (' Hello ', ' Hi ')
Typeerror:extend () takes exactly one argument (2 given)
>>> a.extend ([' Hello ', ' Hi '])
>>> A
[3, ' kk ', 4.3, ' Michelle ', ' hello ', ' Hi ']

4. Insert command, insert! Here's an explanation, the first digit in the insert bracket is the ordinal number of the character in the list, and here, in addition, the Python number ordinal is preceded by 0, that is, if you define the first number in a table, you want to start with zero, once, and so on, followed by the character you want to insert. The following command is a good idea to insert cute in the first place in the list!

>>> a.insert (0,'cute')>>> a['cute  'kk' 'Michelle '  Hello'hi']

5, the list element exchange position, this logic is, when you want to let two elements point to change position, first let one element move to that position and then convert another to another character, text description good around, look at the code!

>>> c=a[3]>>> a[3]=a[4]>>>a['Cute','KK', 3,'Michelle','Michelle','Hello','Hi']>>> a[4]=C>>>a['Cute','KK', 3,'Michelle', 4.3,'Hello','Hi']

6, cancel the order! The element itself is enclosed with remove.

>>> a=['Cute','KK', 3,'Michelle', 4.3,'Hello','Hi']>>> A.remove (3)>>>a['Cute','KK','Michelle', 4.3,'Hello','Hi']

Or with Del, which is surrounded by the ordinal number of the element, this is not, I put the second ' KK ' to delete, haha ~

del a[1]>>> a['cute'Michelle  'Hello'hi']

Pop also has the effect of rejecting an element

>>>a['Cute','Michelle', 4.3,'Hello','Hi']>>>A.pop ()'Hi'>>> A.pop (1)'Michelle'>>>a['Cute', 4.3,'Hello']>>> a[0:1]['Cute']

7, list merge, overlay and identify whether an element is included, this is very simple, not wordy

>>> list1=[2,4,6,7,8,1]>>> list2=[43,35,67,12]>>> list3=list1+  List2>>> list3[2, 4, 6, 7, 8, 1, (+),[2], 4]>>> list3*2 8, 1, approx., 2, 4, 6, 7, 8, 1, List3false, +, in    list3false   in list3true not in list3true

8, Count is to help you count the characters in parentheses appear several times in the list

>>> List3.count (1)1>>> list5=list3*2>>> list5[2, 4, 6, 7, 8, 1, 43, 35, 67, 1 2, 2, 4, 6, 7, 8, 1, approx., +, List5.count (1)>>> 2

Index is to tell you the characters in parentheses in the list, the first good understanding, the second code in the 3,7 is the range, that is, in the list of the fourth to eighth bit to find the number of bits between 1, here again to remind that the number of P is zero-based ha ~

>>> List5.index (4)1>>> list5.index (1,3,7)5

Reverse, as the name implies, is reversed, upside-down meaning

>>> list5.reverse ()>>> list5[12, 67, 35, 43, 1, 8, 7, 6, 4, 2, 12, 67, 35, 43, 1, 8, 7 , 6, 4, 2]

Sort, there are small to large arrangement, from big to small words with reverse is good, true first letter must capitalize Wow

>>> list5.sort ()>>> list5[1, 1, 2, 2, 4, 4, 6, 6, 7, 7, 8, 8, 12, 12, 35, 35, 43, 43, [List5.sort]>>> (reverse=True)>>> list5[67, 67, 43, 43, 35, 35, 12, 12 , 8, 8, 7, 7, 6, 6, 4, 4, 2, 2, 1, 1]

Copy the list, with: If only the copy range is as list7 as the range is good ~

>>> list6=list5[:]>>> list6[67, 67, 43, 43, 35, 35, 12, 12, 8, 8, 7, 7, 6, 6, 4, 4, 2, 2, 1, 1]>>> list7=list5[3:9]>>> list7[43, 35, 35, 12, 12, 8]

Small knowledge points more, I have to digest a bit, refueling! Continue on the path of learning Python Chi Chi go down!

Growth 4-List Series knowledge

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.