Python Beginner Day3--(Introduction to List,tuple,dict,set internal functions)

Source: Internet
Author: User
Tags new set

List Internal Methods summary

1,def Append (self, p_object): Adding elements to the original list

Li = List ((2,3,4,6,9,7))

Li.append (' a ')

Print (LI) results: [2, 3, 4, 6, 9, 7, ' a ']

2,def Clear (self): Clears all elements in the list

Li = List ((2,3,4,6,9,7))

Li.clear ()

Print (LI) results: []

3,def copy (self): Copy to List

Li = List ((2,3,4,6,9,7))

result = Li.copy ()

Print (Result) results: [2, 3, 4, 6, 9, 7]

4,def count (self, value): The number of occurrences of the specified element in the statistics list

Li = List ((2,3,4,6,9,7,2))

result = Li.count (2)

Print (Result) results: 2

5,def Extend (self, iterable): Extending the list

Li = List ((2,3,4,6,9,7,2))

Li.extend (' good ')

Print (LI) results: [2, 3, 4, 6, 9, 7, 2, ' g ', ' o ', ' o ', ' d ']

6,def index (self, value, Start=none, Stop=none): Outputs the subscript of the pointing element, you can specify the starting position of the lookup

Li = List ((2,3,4,6,9,7,2))

result = Li.index (2,1)

Print (Result) results: 6

7,def Insert (self, Index, p_object): Inserts an element into the list and places the insertion

Li = List ((2,3,4,6,9,7,2))

Li.insert (2, ' t ')

Print (LI) results: [2, 3, ' t ', 4, 6, 9, 7, 2]

8,def pop (self, index=none): Removes the element from the list, you can specify the subscript, by default remove the last Li = List ((2,3,4,6,9,7,2)) ret = Li.pop () print (LI) Result: [    2, 3, 4, 6, 9, 7] Print (ret) results: 2 9,def Remove (self, value): Removes the specified element li = List ((2,3,4,6,9,7,2)) Li.remove (2) Print (LI) Results: [3, 4, 6, 9, 7, 2]

10,def reverse (self): reverses a sequence li = List ((2,3,4,6,9,7,2)) Li.reverse () print (LI) results: [2, 7, 9, 6, 4, 3, 2] 11,de F Sort (self, key=none, reverse=false): Sort a list li = List ((2,3,4,6,9,7,2)) Li.sort () print (LI) results: [2, 2, 3, 4, 6, 7, 9]

Tuple Internal Method Summary

1,def count (self, value): The number of occurrences of the specified element in the statistics tuple Li = tuple ((1,3,6,12,8,15,12)) ret = Li.count (p) print (ret) Results: 2 2,def index (self, value, Start=none, Stop=none): Outputs the subscript of the pointing element, you can specify the starting position of the lookup Li = tuple ((1,3,6,12,8,15,12)) ret = Li.index ( 8) print (ret) Results: 4

Dict Internal Methods Summary

1,def Clear (self): Clears all elements in the dictionary li = {' K1 ': 1, ' K2 ': 2, ' K3 ': $, ' K4 ': 3, ' K5 ': ' A ',} li.clear () print (LI) results: {}

2,def copy (self): Copy dictionary li = {' K1 ': 1, ' K2 ': 2, ' K3 ': Ten, ' K4 ': 3, ' K5 ': ' A ',} ret = Li.copy () print (LI) Result: {' K4 ': 3, ' K5 ': ' A ', ' K2 ': 2, ' K1 ': 1, ' K3 ': 10}

3,def Fromkeys (*args, **kwargs): Used to create a new dictionary with the SEQ elements in the dictionary key,value for all keys corresponding to the value Li = {' K1 ': 1, ' K2 ': 2, ' K3 ': Ten, ' K4 ': 3, ' K5 ': ' A ',} ret = Li.fromkeys ([' X1 ', ' x2 '], 6) print (LI) Result: {' K2 ': 2, ' K3 ': Ten, ' K1 ': 1, ' K4 ': 3, ' K5 ': ' A '} print (r ET) Result: {' x2 ': 6, ' X1 ': 6}

4,def Get (self, k, d=none): Gets the value of a key in the dictionary, returns the default value if it does not exist in the key dictionary (the default value is initially empty) Li = {' K1 ': 1, ' K2 ': 2, ' K3 ': Ten, ' K4 ': 3, ' K5 ': ' A ',} ret = li.get (' K3 ', 5) Ret1 = Li.get (' x1 ', ' t ') print (ret) results: print (RET1) results: t

5,def items: Gets the key value of the dictionary to Li = {' K1 ': 1, ' K2 ': 2, ' K3 ': Ten, ' K4 ': 3, ' K5 ': ' A ',} ret = Li.items () print (ret) Results: Dict_items (' K2 ', 2), (' K3 ', Ten), (' K1 ', 1), (' K5 ', ' a '), (' K4 ', 3)])

6,def keys (self): Gets the dictionary key li = {' K1 ': 1, ' K2 ': 2, ' K3 ': 3, ' K4 ': ' A ',} ret = K5 () print (ret) results: DIC T_keys ([' K3 ', ' K2 ', ' K1 ', ' K5 ', ' K4 '])

7,def pop (self, k, d=none): Removes the Pointing key value pair, if key is not in the dictionary, returns the specified value, if the specified value is not set, then the error    li = {' K1 ': 1, ' K2 ': 2, ' K3 ': $, ' K4 ': 3, ' K5 ': ' A ',}    ret = Li.pop (' K2 ')    Ret1 = Li.pop (' x1 ', ')    print (LI)    & nbsp;     result: {' K1 ': 1, ' K5 ': ' A ', ' K4 ': 3, ' K3 ': '    print ' (ret)          results: 2    print (RET1)        results: 8,def Popitem (self): Randomly removes a pair of key values from the dictionary and returns key and value to Cheng Yuanju    li = {' K1 ': 1, ' K2 ': 2, ' K3 ': 3, ' K4 ': ' A ', ' K5 ': ' A ',}    ret = Li.popitem ()    Print (LI)           result: {' K2 ': 2, ' K3 ': Ten, ' K4 ': 3, ' K5 ': ' A '}    print (ret)          results: (' K1 ', 1)

9,def SetDefault (self, k, d=none): Gets the value corresponding to a key in the dictionary, returns the default value if it does not exist in the key dictionary (default is initially empty)    li = {' K1 ': 1, ' K2 ': 2, ' K3 ' : Ten, ' K4 ': 3, ' K5 ': ' A ',}    ret = Li.setdefault (' K3 ', $)    Ret1 = Li.setdefault (' x2 ')    pri NT (LI)            results: {' K1 ': 1, ' K4 ': 3, ' K5 ': ' A ', ' x2 ': None, ' K3 ': 10 , ' K2 ': 2}    print (ret)           results: Ten    print ( RET1)          results: None, def update (self, E=none, **f): Update the dictionary if the key in E is in the original dictionary, Then update value, if not, increase the Key-value key value pair     li = {' K1 ': 1, ' K2 ': 2,}     li.update ({' X1 ': $, ' K2 ': 30 , ' x2 ': +})     print (LI)            result: {' x2 ': 40, ' K2 ': +, ' x1 ':, ' K1 ': 1}

One, def values (self): Gets the values in the dictionary li = {' x2 ': +, ' K2 ': +, ' x1 ': +, ' K1 ': 1} ret = Li.values () print (ret) Results: Dict_values ([20, 30, 1, 40])

Set Internal Method Summary

1, def add (self, *args, **kwargs): Adds the element li = set ([1,2,4,6, ' s ', ' a ']) to the collection Li.add (' Good ') print (LI) Result: {1, 2, 4, 6, ' a ', ' good ', ' s '}

2,def Clear (Self, *args, **kwargs): Clears all elements in the collection Li = Set ([1,2,4,6, ' s ', ' a ']) Li.clear () print (LI) Result: set ()

3,def copy (self, *args, **kwargs): Copy collection li= set ([1,2,4,6, ' s ', ' a ']) ret = Li.copy () print (ret) Result: {1, 2, 4, 6, ' a ', ' s '}

4,def difference (self, *args, **kwargs): Removes the same part of the collection as the parameter, returns a new set of Li = Set ([1,2,4,6, ' s ', ' a ']) ret = li.difference (4, 6, ' P ', ' Q ')) print (ret) Result: {1, 2, ' s ', ' a '}

5, Def difference_update (self, *args, **kwargs): Remove li = Set ([1,2,4,6, ' s ', ' a ']) from the same part in the original set as the parameter, Li.difference_update (4 , 6, ' P ', ' Q ')) print (LI) Result: {1, 2, ' a ', ' s '}

6,def Discard (self, *args, **kwargs): Removes a specified element from the binding, if the element is in the collection, then remove, if not, do nothing li = set ([1,2,4,6, ' s ', ' a ']) Li.discard (' X ') print (LI) Result: {1, 2, 4, 6, ' a ', ' s '}

7,def intersection (self, *args, **kwargs): A new collection of two identical elements in a collection returns LI = Set ([1,2,4,6, ' s ', ' a ']) ret = li.intersection (9, ' Y ', 1,2,4)) print (ret) results: {1, 2, 4}

8,def intersection_update (self, *args, **kwargs): Remove li = Set ([1,2,4,6, ' s ', ' a ']) in the original set with a different part of the parameter Li.intersection_updat E ((9, ' Y ', 1,2,4)) print (LI) results: {1, 2, 4}

9,def Isdisjoint (self, *args, **kwargs): Determines whether two sets have intersections if there is a return of false if there is no true li = Set ([1,2,4,6, ' s ', ' a ']) ret = LI.ISDI Sjoint ((+, ' O ' P ')) Ret1 = Li.isdisjoint ((9,8, ' o ', ' P ')) print (ret) Result: False print (RET1) Result: True

10,def Issubset (self, *args, **kwargs) and Def issuperset (self, *args, **kwargs): Determines whether a subset or parent set Li = Set ([1,2,4,6, ' s ', ' a ']) ret = Li.issubset (()) Ret1 = Li.issuperset (()) print (ret) Result: False print (RET1) result : True

11,def pop (self, *args, **kwargs): Randomly deletes an element in the collection, returning the deleted element li = set ([1,2,4,6, ' s ', ' a ']) ret = Li.pop () print (LI) Results: {2, 4, 6, ' a ', ' s '} print (ret) Results: 1

12,def Remove (self, *args, **kwargs): Deletes the specified element in the collection, no return value Li = Set ([1,2,4,6, ' s ', ' a ']) Li.remove (2) print (LI) Result: {1, 4, 6, ' s ', ' a '}

13,def symmetric_difference (self, *args, **kwargs): A new collection of two different elements in a collection returns LI = Set ([1,2,4,6, ' s ', ' a ']) ret = Li.symme Tric_difference ((4,6, ' u ', ' I ')) print (ret) Result: {1, 2, ' I ', ' u ', ' a ', ' s '}

14,def symmetric_difference_update (self, *args, **kwargs): Removes the same elements in the original collection as the parameters, and the different elements retain the LI = set ([1,2,4,6, ' s ', ' a ']) Li . Symmetric_difference_update ((4,6, ' u ', ' I ')) print (LI) Result: {1, 2, ' a ', ' I ', ' u ', ' s '}

15,def Union (Self, *args, **kwargs): Joins two collections into a new set li = Set ([1,2,4,6, ' s ', ' a ']) ret = Li.union ((4,6, ' u ', ' i ')) p Rint (ret) Results: {1, 2, 4, ' A ', 6, ' U ', ' s ', ' I '}

16,def Update (self, *args, **kwargs): Update a collection Li = Set ([1,2,4,6, ' s ', ' a ']) li.update ((4,6, ' u ', ' I ') print (LI) Results: {1, 2, 4, 6, ' u ', ' I ', ' a ', ' s '}

Python Beginner Day3--(Introduction to List,tuple,dict,set internal functions)

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.