In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Python is a cross-platform computer programming language. Is a high-level combination of interpretive, compiled, interactive and object-oriented scripting language. Originally designed for automated scripting (shell), as versions are constantly updated and new features of the language are added, the more they are used for independent, large-scale project development.
Dictionaries are another variable container model and can store objects of any type.
Each key value key= > value of the dictionary is divided by a colon: split, each key value pair is divided by a comma, and the whole dictionary is included in curly braces {}
The Python dictionary contains the following built-in methods:
Serial number function and description 1dict.clear ()
Delete all elements in the dictionary 2dict.copy ()
Returns a shallow copy of a dictionary 3dict.fromkeys (seq [, val])
Create a new dictionary, using the elements in the sequence seq as the keys of the dictionary, and val is the initial value 4dict.get (key, default=None) corresponding to all the keys in the dictionary.
Returns the value of the specified key, if the value does not return the default value 5dict.has _ key (key) in the dictionary
If the key returns true in the dictionary dict, otherwise it returns false6dict.items ()
Returns a traverable (key, value) tuple 7dict.keys () in a list
Returns a dictionary with all the keys 8dict.setdefault (key, default=None) in a list
Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default9dict.update (dict2)
Update the key / value pair of the dictionary dict2 to dict 10dict.values ()
Returns all values in the dictionary 11pop (key [, default]) as a list
Delete the value corresponding to the given key key of the dictionary, and the return value is the deleted value. The key value must be given. Otherwise, the default value is returned. 12popitem ()
Returns and deletes the last pair of keys and values in the dictionary. # create and use the dictionary 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-# use the dict function to convert a sequence into a dictionary # 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'}''--# the basic operation of the dictionary # get the length of the dictionary through the len function items = [1mem2jing3jin4 items 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.''
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.