In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to create and use dictionaries in python. I hope you will get something after reading this article. Let's discuss it together.
The first lesson is to create and use dictionaries # create and use dictionaries # We have talked about two sequences before: 1 list 2 tuples # Common: both refer to element values through indexes # differences: lists are readable and writable The tuple is names = ["Bill", "Mike", "John", "Mary"] numbers = ["1234", "4321", "6789" "6543"] print (numbers [names.index ("Mike")]) # 4321 use the index position of Mike in names to find the list in his phone number numbers (names [numbers.index ("6543")]) # Mary# multiple associated data are scattered into different lists or tuples Difficult to maintain # Dictionary is a pair of key and value {} intermediate: phoneBook = {"Bill": "1234", "Mike": "4321", "John": "6645", "Mary": "7753"} print (phoneBook) # {"Bill": "1234", "Mike": "4321", "John": "6645" "Mary": "7753"} print (phoneBook ["Mike"]) # 4321 Mike-lesson 2 uses the dict function to change the sequence (list, The list and tuples can be converted to dictionary types through the dict function # using the dict function to convert sequences into dictionaries # the first is a list and the second element is a tuple # first define a list There are multiple elements in the list, and the elements in the list can be multiple values It can be a list or a tuple items = [['Bill', "4321"], ("Mike", "7891"), [' John','4567']] d = dict (items) print (d) # {'Bill':' 4321, 'Mike':' 7891, 'John':' 4567}''or it can be written directly as items = ['Bill', "4321"], ("Mike", "7891"), [' John' " ] # d = dict (items) print (dict (items))''dict1 = dict (name= "Bill", number=6543, age=34) # this is to convert the tuple into a dictionary print (dict1) # {' name': 'Bill',' number': 6543, 'age': 34} # you need to define an empty list and append the input input Then convert it into the dictionary items = [] while True: key = input ("Please enter Key:") if key = ": exit": break through the dict function Value = input ("Please enter value:") keyValue = [key,value] items.append (keyValue) # add d = dict (items) print (d)''Please enter Key:majihui Please enter value:12345 Please enter Key:zhaoshun Please enter value:33333 Please enter Key:local Please enter value:china please enter Key::exit {' majihui': '12345,' zhaoshun': '33333' 'local':' china'}'- lesson 3 basic operations of dictionaries # basic operations of dictionaries # get the length of dictionaries through the len function items = [1 mine2 3 3 4 5] # this is a list print (len (items)) # 5 the length of the output list d = {"name": "Bill" "age": 43} # this is a dictionary print (len (d)) # 2 output dictionary length # get and set or modify the value print (d ["name"]) # Bill in the dictionary through key to get the value d ["name"] = "Mary" # modify the value of value in the dictionary to change the name from Bill to Maryprint (d) # {'name':' Mary' 'age': 43} # print (items [10]) # an exception is reported when the output index exceeds the list d ["salary"] = 5432 # append a print (d) # {' name': 'Mary',' age': 43 to the original dictionary 'salary': 5432} # determine whether the field is in the list print (1 in items) # Trueprint (10 in items) # False# for the dictionary He will not look for valueprint ("name" in d) # Trueprint ("location" in d) # False''' list and dictionary summary: 1. The type of key, the list can only be numeric (index), dictionary key can be multiple types, such as string, tuple 2. Automatically add, reference list elements, index must be within the range, otherwise throw an exception, if the dictionary key does not exist, will automatically add a key-value pair 3. Find members, list using in, find values directly, use in in dictionaries, find key search efficiency The dictionary is higher than the list and tuple''IDEs = {' eclipse': {'languages': [' Java','Python','JavaScript','PHP'], 'organization':'Eclipse Foundation'}, 'visualstudio': {' languages':], 'VB.NET'],' organization':' Microsoft'} 'webstorm': {' languages': ['JavaScript'],' organization':'JetBrains'}} labels = {'programming languages supported by languages':'', 'organization to which organization':' belongs'} IDE = input ('Please enter the name of IDE') findIDE = IDE.replace ('' ''). Lower () # converts the input space to blank and converts the input value to lowercase choice = input ('programming language (lang) supported by IDE to query or membership and organization (org)'?) If choice = 'lang': key =' languages'if choice = 'org': key =' organization' if findIDE in IDEs: print ("{} {} is {}." format (IDE, labels [key], IDE [key]))''Please enter the name of the IDE to query the programming language (lang) or affiliation and organization (org) to be queried? the programming language supported by langeclipse is [' Java', 'Python',' JavaScript', 'PHP']. Please enter the name of IDE, which programming language (lang) or org (org) supported by IDE to be queried by WEBstorm? the organization to which orgWEBstorm belongs is JetBrains. Please enter the name of IDE eclipse / / enter this space with spaces to query the IDE supported programming language (lang) or affiliated organization (org)? orgeclipse belongs to the Eclipse Foundation.'' After reading this article, I believe you have a certain understanding of "how to create and use dictionaries in python". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.