Python dictionary, pythonThis article mainly introduces the dictionary in python, which is one of the most powerful data types in Python, this article explains what is a dictionary, how to create a
First, the contentIi. Exercise 1, Element classificationRequirements: A collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.That is: {' K1 ': All values greater than 66, ' K2 ': All values less than 66}L = [11,22,33,44,55,66,77,88,99,90]d = {' K1 ': [], ' K2 ': []}for i in Range (Len (l)): if l[i
1.Python dictionary (Dictionary)A dictionary is another mutable container model and can store any type of object.each key value of the dictionary (key=>value) pair with a colon (:) split, Each pair is separated by a comma (,) , and the entire
characters (including ' \ n ', ' \ R ', ' \ t ', '). Split split:names = ' Cao, Cjk,alex ' name2 = Names.split (', ') then name2 = [' Cao ', ' CJK ', ' Alex '] splits the string into lists as ', '. join The elements in the list into a string: Name3 = ' | '. Join (name2) Name3 = ' Cao|cjk|alex ' Determine if there are spaces: name = ' Cao Fa ', print (' in name ') return true str.format (): msg = ' Hello {name} ' it ' s been {time] days since I saw you ' MSG2 = Msg.format (name = ' C
Summary of several methods to traverse the python Dictionary (recommended) and python dictionary
As follows:
ADict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} print '----------- dict -----------' for d in aDict: print "% s: % s" % (d, aDict [d]) print '----------- item -----------' for (k, v) in aDict. ite
There are three built-in data structures in Python-lists, tuple tuples, and dictionary DictItems in the list are included in square brackets , and items are separated by commasTuples and lists are very similar, except that tuples and strings are immutable , meaning that you cannot modify tuples. Tuples are defined by a comma-separated list of items in parentheses .The most common use of tuples is in the pri
; shopping_list.count (' food ') #Statistics' food 'the number of elements, just added one, so now is2a>>> list2= [' Banana ', ' Apple '] #Create a new list>>> Shopping_list.extend (list2) #Merge the new list above into theshopping_listin>>> Shopping_list.sort () #sort the list>>> Shopping_list.reverse () #Invert the list>>> del Shopping_list[3:8] #Delete Index3to8the element, not including8>>> for I in Shopping_list: #Traverse List... print IThis article from the "Wind continue to blow" blog, d
;> a[1, 2, [3, 4]]>>> a.extend(b)>>> a[1, 2, [3, 4], 3, 4]InsertInsert (Index, object) inserts an element in the specified position before Index object>>> a = [0, 1, 2]>>> a.insert(1, 3)>>> a[0, 3, 1, 2]When modifying an element, use the subscript to determine which element is being modified before you can modify it.Demo #定义变量A,默认有3个元素 A = [‘xiaoWang‘,‘xiaoZhang‘,‘xiaoHua‘] print("-----修改之前,列表A的数据-----") for tempName in A: print(tempName) #修改元素 A[1] = ‘xiaoLu‘ print("-----修改之后,列表A的数据---
Dictionaryinfo = { ' a ': 123, ' B ': ' xxx '}# value two methods the first one does not exist error # print (info["x"]) print (Info.get (' x ')) # delete del info[' a ']# Determine if key exists print (' a ' in info)Set/tupleImport Copy # introduces the Copy Library # Collection (array) names = [' AAA ', ' BBB ', ' CCC ', ' DDD ', [1, 2, 3, 4]]# insert Names.insert (0, ' first ') # Append NAMES.A Ppend (' last ') # Delete print (names) del Names[1]names.pop (1) names.remove ("ddd") # do
of times that STR appears in the mystr between start and endMystr=' Howmany fish is therein the pool'result_one=mystr.count ('A ', 1,10) result_two=mystr.count ('a')Print (str (result_one))print(str (result_two))Replace the str1 in mystr with the STR2 if count specifies that the replacement is not more than count times.Mystr=' Howmany fish is there in the pool ha ha'mystr.replace (' Ha','ha') mystr.replace ('ha') ,'Ha', 1)Slice mystr with Str as delimiter, if Maxsplit has a specified value,
Python-Level 7 dictionary query and python dictionary Query
1 #-*-coding: UTF-8-*-2 3 4 # job requirements: 5 # five menus of municipalities, provinces, municipalities, counties, districts, and sub-streets; 6 # Step by step to all layers 7 # exit to the previous Layer 8 # exit program at any time 9 10 11 mapChina = {12
Python dictionary is a very important base type in Python, it is an efficient base type, not only used in writing programs, but even the implementation of Python's low-level use of a large number of dictionaries.
Learn Python dictionarie
;>> A[5, 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]7. Modifying list values>>> a[0] = ' 5 ' >>> A[' 5 ', 1, 2, 3, 1, 1, 2, 3, 4, 5, 6, 7]8. Delete list valuesPop () Delete the element and return the deleted value by default delete the last element, you can specify a location to delete >>> A.pop ()7>>> A.pop (0)' 5 'Remove () deletes the specified element when there are multiple identical elements when the first one in the list is deleted >>> A [1, 2, 3, 1, 1, 2, 3, 4, 5, 6]>>> A.remove
Python self-study day-Two day (2)-python-list-tuples-dictionary, python-
ListFormat: name = []Name = [name1, name2, name3, name4, name5]
# List operations
Name. index ("name1") # query the name of the specified value. count ("name1") # query the name of the total number of specified values. clear ("name") # clear the l
, ' C ': None, ' e ': None, ' d ': none, ' F ': none}{' y ': 2, ' x ': 1}{' Key2 ': 2, ' Key1 ': 1}Zip ([iterable, ...])Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortes
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.