Notes of Python (4)------list (how to)

Source: Internet
Author: User

1. Add--append, Extend, insert

List.append (item) ———————— Add an entry to the end of the list

List.extend (Iterative object) —————— expansion list to increase list, string, tuple, etc.

a=[1,3]in[]: a.append (5) in[[]: aout[]: [1, 3, 5]b=[5,6,7]in[ Wuyi]: a.extend (b) in[[aout[]:[1, 3, 5, 5, 6, 7]

List.insert (i,item) ———— inserting items before a specified position

a=[1,2,'haha', [34,485,2734],23.45,'Okthen']in[[A.insert]: (3,'newly added items') in[69]: aout[69]: [1, 2,'haha','newly added items', [34, 485, 2734], 23.45,'Okthen']in[]: A.insert (100,'newly added items') in[71]: aout[71]: [1, 2,'haha','newly added items', [34, 485, 2734], 23.45,'Okthen','newly added items']

When I index exceeds the maximum value, automatically joins the tail (same append)

2.hasattr ()

Hasattr (obj, name,/)
Return whether the object has a attribute with the given name.

Determine if an object has an iterative nature

IN[54]: a=[1,2,3]in["B="'OK Then'in[[c=]: 1,2,3,4) in[[]: d=345.372in[]: Ar=hasattr (A,'__iter__') in[: Br=hasattr (b),'__iter__') in[: Cr=hasattr (C,'__iter__') in[(a): Dr=hasattr (D,'__iter__') in[62]:Print(AR,BR,CR,DR) true True True False

3. Count--count (): Number of occurrences of an element in a list

Help (List.count)-on Method_descriptor:count (...)     return number of occurrences of value

4.index ()-the position of the element when it first appears

5. Delete--remove and pop

Help (List.remove)-on Method_descriptor:remove (...)     -None- Remove first occurrence of value.     if  is  not present. in[]: Help (List.pop)-on Method_descriptor:pop (...)      and return item at index (default last).     if  is or  is out of range.

Remove (item) —————— Delete the first occurrence of item, no return value

Pop (i (optional)) —————— Delete the item at the index, the index is optional, and the last one is deleted without an index. There is a return value: the deleted item

aout[75]: [1, 2,'haha','newly added items', [34, 485, 2734], 23.45,'Okthen','newly added items']in[: A.remove (2) in[77]: aout[77]: [1,'haha','newly added items', [34, 485, 2734], 23.45,'Okthen','newly added items']in[78]: A.pop () out[78]: 'newly added items'

6. Sort--list.sort (): No return value, will modify the original list, sorted by keyword, by default from small to large.

Help (List.sort)-on Method_descriptor:sort (...)    L.sort (Key=none, Reverse=false), None--stable sort *in place*

System sort function sorted (): valid for all iteration sequences (list, string, tuple), return sorted list

Help (sorted) to built-  in module builtins:sorted (iterable, key=none, reverse=False)    A new listfrom in  ascending order.          and the      in descending order.

8. String and List conversions: two ambiguous functions str.split () &str.join ()

Str.split (): Convert string to list based on delimiter

A='www.sina.com'in[]: a.split ('. ' out[]: ['www''sina'  com']   # return value for list string ———————— + = List

Str.join (iterative): Use the string str to link an iterative object and return a string. That is, list ———————— + = string

' . '. Join (['sina','sina','com ' ]) out['sina.sina.com'

Notes of Python (4)------list (how to)

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.