python- tuples, lists, dictionaries common method

Strings, lists, dictionaries, tuples, the sequence structure are set Sequence, by an index, the function includes a slice, you can store any type of data.

List list

   Is defined: list = []

         list=[1,2,'a',"hello",(3,4),[3,5,'f'],{'a':'tom'}]

    1> by:

      a> list.append () # default increased tail

        list.append (100) list.append ([1,2]) # default increased tail

      b> list.insert (subscript insert, insert value) is inserted into a certain position #

        list.insert(0,120)

      c> Insert sublist

          list[-2].insert(1,"edf")

    2> Delete:

      a> del subscript looking at the value of deleted

        del  list[1]        

        del  list[0:2]

      b> list.pop (): returns a value, delete the return value of that element

        list.pop(2)

      c> list.remove () #: value delete, delete the slowest, because to traverse to find this value

        list.remove(2)

    3> change

      list [0] = 20 # subscript Review

    4> check

      list [3] # subscript Find

  Merge list: +, extend ()

   a = [1,2]      b = [3,4]

   a> + temporary combined, save as equivalent

    print (a + b)  - -   [1,2,3,4]

   b> permanently merge - expanded list

    print (a.extend (b)) - - - - a result: [1,2,3,4]

  Built-in functions:

  len (list): calculated number of elements

       max (list): Returns the maximum element of the list

  min (list): Returns the list of the minimum element

  list.reverse (): Reverse elements in the list

  list.remove (obj): remove the list a value of the first match

  The first element and the last element:

  str [0], p [referred to as (p) -1]

  p [-1], p [-I (p)]

Tuple tuple: can not be modified, the value of the element can not be modified, the number can not be modified, corresponding to fixed

  1> sequence is also a sequence

  2> can store any type of data

  3> also has a function sections

  4> different tuples and lists: the list of tuples its constituent elements can

  5> only query can not be CRUD

Tuples and lists differences:

Dictionary dict

    1> defined

  Key-value pair dic = { 'key': 'value', 'key': value}

    Key: can be: a string (common), int, float, tuple, bool

       Not: list, dict

    Value: Any type

  dict1 = {}

  dict2 = {'name':mike,'age':28}

 2> Features

  Sequence sequence, called characteristic map, dictionaries are unordered, look for clear, scalability, and looking through the key values

 3> common method

  a> by:

    The dictionary is the only key;

    A dictionary is unordered;   

    python3: Tail increase python2: random increase   

    If the key name exists - modify operations;

    If the key name does not exist --- New;

    dict['weight'] = 170

 

  b> deleted:

     del :   del  dict['name']

    pop :   dict.pop('age')

  c> 改:

    通过键进行修改;

    dict ['name'] = 'mike'; (如果键存在,则修改值;如果键不存在,则新增键值对)

  d> 查:

    通过键去找值;

    如果键不存在-报错-keyerror;

    print (dict['name']);

   4> 常用操作

   a> 判断键是否在字典中,in

    print ('name' in dict)      - -- -   True

   b> 清空字典内容clear()

    清空 :  dict.clear() 

    重新复制 : dict = {} 

    c> 长度len(dict)

    len(dict)

    len(dict['name'])   #len(dict['键名'])

    d> 返回所有的键keys()

    dict.keys() - - 类列表,不能使用下标,但可以遍历

    如果一定要使用下标,则需要将其转换为列表list

      list(dict.keys())[0]         tuple(dict.keys())[0]

   e>返回所有的值values()

     dict.values()

   f> 返回所有的键值对items()

    dict.items()  - -  [(’键’,‘值‘),(’键’,‘值‘)]

    print(dict.items())   --    [('name','Jack'),('age',21)]

     g> 字典合并update()

    d={1:'1',2:'2'}   

    print(d.update({4:'4',5:'5'})) 

    结果: {1:’1‘,2:’2‘,3:’3‘,4:’4‘,5:‘5’}

    h> 字典的遍历操作  

     1) for one in dict: # one取得是键

         print (one)

          -- name

           age

     2)  for one in dict:#获取键值

        print(one,dict[one])

        --  name tom

            age 21

        3)  for a,b in dict.items():

        print(a,b)

             -- name tom

            age 21

    5> 使用场景

     有序场景不能用;

          鉴定函的存储可以使用列表;

     可以作为扩展内容;

Guess you like

Origin www.cnblogs.com/yangguanghuayu/p/11199955.html