Python_Day_03 list, dic, tuple method summary, python_day_03tuple

Source: Internet
Author: User

Python_Day_03 list, dic, tuple method summary, python_day_03tuple

Several data types, dictionaries, lists, and so on are the most common in programming languages. These data types are also available in Python, but some forms are different.In Python, there is another thing called tuple.

 

  List)

  • Initialization list
1 li = list([1,2,3,4])
  • You can also directly create
1 li_dir = [42,534,4654,64]
  • Append (self, p_object) adds an element at the end of the list. The default value is None.
1 li_append = list([1,2,3,4])2 li_append.append(43)3 print(li_append) # [1, 2, 3, 4, 43]
  • Clear (self) clear the Array
1 li_clear = list([1,2,3,4])2 li_clear.clear()3 print(li_clear) # []
  • Copy (self) shallow copy (what is the difference with deep copy? We haven't studied it yet)
1 li_copy = list([11,22,33])2 li_copy_copy = li_copy.copy()3 print(li_copy_copy) #[11, 22, 33]
  • Count (self, value Returns the number of values in the list. A statistical value must be input. Otherwise, an error is returned.
1 li_count = list([111,222,333,444,111])2 li_counts = li_count.count(11) # 03 print(li_counts)4 li_counts_two = li_count.count(111)5 print(li_counts_two) # 2
  • Extend (self, iterable) is equivalent to expansion. The extended parameters must also be lists or tuples. At the same time, they must be extended based on the original list, and the extended data is at the end of the list.
1 li_extend = list ([111,222,333,444,111]) 2 # When the extended parameter is a tuples, if 'is not added to the element,' the extension fails, error 3 li_extend.extend (12,) 4 print (li_extend) # [111,222,333,444,111, 12] 5 li_extend.extend ([12,124, 5, 5]) 6 print (li_extend) # [111,222,333,444,111, 12, 12,124, 3, 4,534, 5, 5]
  • Index (self, value, start = None, stop = None) searches for the subscript of an element in the list based on the value.
1 li_index = list ([111,222,333,444,111]) 2 # by default, the entire list is queried. When the first existing value is found, the system returns and does not continue searching for the list elements, if no element exists, 3 li_index_index = li_index.index (111,0, 4) 4 print (li_index_index) #0 is returned.
  • Insert (self, index, p_object) insert the specified element under the specified subscript
1 li_insert = list([111,222,333,444,111])2 li_insert.insert(3,'Python')3 print(li_insert) # [111, 222, 333, 'Python', 444, 111]
  • Pop (self, index = None) pops up the specified subscript element, and a variable can be defined to receive the pop-up element. the last one is displayed by default. If the list is empty or the subscript exceeds the maximum subscript of the list, an error is returned.
1 li_pop = list([12,4234,3,534,64536])2 li_receive = li_pop.pop(3)3 print(li_pop) # [12, 4234, 3, 64536]4 print(li_receive) # 534
  • Remove (self, value) deletes the elements in the list based on the value. If there are multiple Deleted Values in the list, only the first value will be deleted. If the deleted value does not exist, an error will be returned.
1 li_remove = list([11,23213,11,232323242])2 li_remove.remove(11)3 print(li_remove) # [23213, 11, 232323242]
  • Reverse (self) Flip list
li_reverse = list([11,23213,11,232323242,'fdsa','rt433',13423])li_reverse.reverse()print(li_reverse) # [13423, 'rt433', 'fdsa', 232323242, 11, 23213, 11]
  • Sort (self, key = None, reverse = False) sorting (not much of this study)
Li_sort = list ([4, 6, 2, 1, 7, 9]) li_sort.sort () print (li_sort) # [1, 2, 4, 6, 7, 9] # Keep copy li_sort_y = li_sort [:] li_sort_y.sort () print (li_sort_y) # [1, 2, 4, 6, 7, 9] while retaining li_sort

 

  Tuple (tuples)

The method is relatively simple, but you must add 'At the end of the Creation.' Only search for kinetic energy, no addition, deletion, and modification are required.

tup = tuple((12,3242,3,4435,43,45,6,12))
  • Count (self, value) searches for the number of occurrences of a value in the tuples
tup = tuple((12,3242,3,4435,43,45,6,12))tup_count = tup.count(1) # 0print(tup_count)
  • Index (self, value, start = None, stop = None) searches for subscripts based on a specific value in the tuples. The default value is the entire array and you can customize the search range, if it appears multiple times, only the first
tup = tuple((12,3242,3,4435,43,45,6,12))tup_index = tup.index(12)print(tup_index) # 0

 

  Dict (dictionary)

  The middle keys in the dictionary cannot be repeated. If repeated, the first occurrence will be overwritten.

dic = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'} print(dic) # {'k1': 'v4', 'k2': 'v2', 'k3': 'v3'}
  • Clear (self) clear all elements in the dictionary
dic_clear = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_clear.clear()print(dic_clear) # {}
  • Copy (self) Shortest copy (similar to an array, we haven't studied it in depth)
dic_copy = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_copy_copy = dic_copy.copy()print(dic_copy_copy) # {'k1': 'v4', 'k3': 'v3', 'k2': 'v2'}
  • Fromkeys (* args, ** kwargs) assigns the same value to an array.
dic_keys = dict.fromkeys([1,232,423,43,43],['da','fsda']) # {232: ['da', 'fsda'], 1: ['da', 'fsda'], 43: ['da', 'fsda'], 423: ['da', 'fsda']}dic_keyss = dict.fromkeys([1,232,423,43,43],'da')print(dic_keyss) # {232: 'da', 1: 'da', 43: 'da', 423: 'da'}
  • Get (self, k, d = None) returns the value returned based on a key. If no value exists, you can also customize the default value.
dic_get = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_get_get = dic_get.get('k1','rookieJX')print(dic_get_get) # v4dic_get_get2 = dic_get.get('k','rookieJX')print(dic_get_get2) # rookieJX
  • Items (self returns all elements (all key-value pairs)
Dic_items = {'k1 ': 'v1', 'k2': 'v2', 'k3 ': 'v3', 'k1 ': 'v4'} dic_items.items () print (dic_items) #{'k3 ': 'v3', 'k2': 'v2', 'k1': 'v4 '} note that the returned dynamic, the order is constantly changing.
  • Keys (self returns all keys
Dic_keys = {'k1 ': 'v1', 'k2': 'v2', 'k3 ': 'v3', 'k1 ': 'v4'} dic_keys_receive = dic_keys.keys () print (dic_keys_receive) #(['k1 ', 'k3', 'k2']) note that another list is returned here, and an additional definition list is required to receive the return value.
  • Pop (self, k, d = None) is not very useful because the dictionary is unordered. If the key value does not exist, there is no default value.
Dic_pop = {'k1 ': 'v1', 'k2': 'v2', 'k3 ': 'v3', 'k1 ': 'v4 '} dic_res = dic_pop.pop ('k10', 100) print (dic_pop) # {'k3': 'v3 ', 'k1': 'v4 ', 'k2 ': 'v2'} does not change the original dictionary. You need to redefine it to accept the data print (dic_res) #100 if the pop-up key-value pair is not specified, the specified value will be returned for a long time, the default value is Nonedic = dic_pop.pop ('k1 ', 100) # {'k3': 'v3 ', 'k1': 'v4 ', 'k2 ': 'v2'} does not change the original dictionary. You need to define a new dictionary to receive print (dic_pop) # {'k2': 'v2', 'k3 ': 'v3 '} print (dic) # v4
  • Popitem (self) pops up an object, because it is unordered, so it is really a pain
Dic_popitem = {'k1 ': 'v1', 'k2': 'v2', 'k3 ': 'v3', 'k1 ': 'v4'} dic_popitem.popitem () #{'k1 ': 'v4', 'k3 ': 'v3'} the deleted object is not necessarily print (dic_popitem)
  • Setdefault (self, k, d = None) is similar to get
dic_default = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_default_get = dic_get.setdefault('k1','rookieJX')print(dic_default_get) # v4dic_default_get2 = dic_default.get('k','rookieJX')print(dic_default_get2) # rookieJX
  • Update (self, E = None, ** F) the dictionary dict2's key/value pairs are updated to dict, which seems to be similar to extend, but no new dictionary is generated.
dic_update = {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_other = {'k10':'v4'}dic_update.update(dic_other)print(dic_update) # {'k3': 'v3', 'k1': 'v4', 'k2': 'v2', 'k10': 'v4'}
  • All values (self) values
dic_values =  {'k1':'v1','k2':'v2','k3':'v3','k1':'v4'}dic_values_values = dic_values.values()print(dic_values_values) # ['v2', 'v4', 'v3']

 

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.