, ' address " : " Earth Asia China Beijing ' }result =len (info) print ( " " %result) Execution Result:Returns a list containing all keys for the dictionary info = { " name ' : " Squad leader , ' sex ' : ' f , ' address " : " Earth Asia China Beijing ' }result =info.keys () print ( key,%s " %result) Returns a list containing all the value of the dictionary inf
delete
Cycle
Length Len (D)
Display key, value, key value>>>list (D.values ()) >>>list (D.keys ()) >>>list (D.items ())
Get method: D.get (Key.default)When the key does not exist, the dictionary traversal looks for a missing-key error, but when the key does not exist, a system default of None or a user-defined default value can be obtained by getting it.Eg:>>>d.get (' spam ') 2Print (D.get (' toast ')) NoneD.get (' Toast ', 88) 88
Write a function remove_duplicates that takes the list as an argument and returns a new list that contains the unique elements in the source list. Elements that do not recur in the new list can take any order.Target=[]def remove_duplicates (list):For each in list:If each isn't in target:Target.append (each)return targetA collection is a collection of unique elements, without any particular sort. We can create a collection from a list like this:Country_ set = set (countries)Len (Country_set)196Ad
A diagram in Python--A dictionaryA dictionary is another mutable container model and can store any type of object.Each key-value pair in the dictionary is key-value with a colon: a split, a comma, a split between each pair, and the entire dictionary is enclosed in curly braces {}, and the key must be unique, but the va
)Define Dictionary using a pair of large (curly) brackets "{}".A dictionary (Dictionary) is one of the built-in data types of Python that defines a one-to-one relationship between keys and values, but they are stored in a disorderly manner. The "value" in the dictionary is r
Full-stack python development-Day5 tuples, dictionaries, python-day5Full-stack python development-Day5 tuples and dictionariesI. PrefaceFirst of all, no matter what data type we learn, we will start learning with the following questions:#1: basic usage 1. Purpose 2. Definition method 3. Common Operations + Built-in met
>>>list (IT) #可以将迭代器转换成列表>>>[(' Key1 ', ' value1 '), (' Key2 ', ' value2 ')] keys and Iterkeys: returns the key as a list, the iterator for the key
Popd.pop (' key ') has a return value and removes
Popitem:D.popitem () pop-up random items, because the dictionary is unordered
SetDefault:>>>d.setdefault (' key ', ' default_value ')When the key does not exist, return the default value and update the dictionary
Python Learning Note 4: DictionariesSummary: dictionaries are mutable variables;Dictionaries are not sorted, so they cannot be like lists, tuples, strings, slices1, define the dictionary dict--curly braces {}, the dictionary is just key-value formFor example: stu={' name ':
;dict.keys () Dict_keys ([' Key1 ', ' key2 ', ' Key3 ', ' key4 ', ' key8 ']) >>>dict.values () dict_values ([ 1,2,3,4,none])(8). Update (New_dict) methodRole: To bring a new dict_new to the Dict>>>dict1={' 1 ': 2, ' 3 ': 4}>>>dict.update (dict1) >>>dict{' key1 ':1, ' key2 ': 2, ' Key3 ':3, ' key4 ': 4, ' 1 ': 2, ' 3 ': 4}
The difference between a dictionary and a listFeatures of Dict:Find and insert very fast and do not increase as key incre
Getting started with Python: 9 dictionaries 1. definition of dictionaries
The feature of the dictionary is that the icon of the dictionary is curly braces, and the elements in the dictionary use key-value pairs. A key-value pair i
' Animals brought from the old Zoo was ', New_zoo[2]print ' last animal brought from the old Zoo is ', new_zoo[2][2 ]
Output
$ python using_tuple.pynumber of animals in the zoo was 3Number of animals in the new zoo was 3All animals in New zoo was (' Monkey ', ' Dolphin ', (' Wolf ', ' Elephant ', ' penguin ')) Animals brought from the old Zoo is (' Wolf ', ' Elephant ', ' penguin ') last A Nimal brought from the old Zoo is Penguin
3. DictionariesThe
print ' animals brought to old zoo are ', new_zoo[2]
print ' Las T animal brought from Old Zoo is ', new_zoo[2][2]
Output
$ python using_tuple.py number of animals in the zoo was 3 number of animals in the
New Zoo are 3 all
animals in New zoo are (' monkey ', ' Dolphin ', (' Wolf ', ' Elephant ', ' penguin ')) Animals brought from Old
Zoo are (' wolf ', ' Elephan T ', ' Penguin ') last
animal brought from Old Zoo is Penguin
3. DictionaryThe
dictionary. (Format:dict.popitems () )>>> D = {' A ': 1, ' B ': 2, ' C ':3}>>> d{' a ': 1, ' B ': 2, ' C ': 3}>>> d.popitem (' A ', 1)SetDefault ()The SetDefault () function finds the key in the dictionary dict, returns the value it corresponds to, returns the value of default if the key does not exist in the dictionary, and adds the key value to the
Dictionaries
Objects that can store multiple elements become containers, and the list and tuple mentioned above belong to the container class, which introduces a new container: a dictionary.
Basic conceptsCommon basic methods for creating dictionaries:
dic={' key ': value, ' key ': value, ' key ': Value ...}
The dictionary
that list and tuple can be "broken" into normal parameters to the function, and dict can be broken into the keyword parameters to the function (through and *). So you can easily pass a list/tuple/dict to the Format function. Very flexible.Format qualifierIt has a rich "format qualifier" (syntax is {} with: number)For example: Fill and alignPadding is used in conjunction with alignment^, : The filled character after the number, only one character, not specified by default is filled with spaces,
The dictionary consists of key and value, and key must be unique, and the value may not be unique. The dictionary is unordered.common methods for dictionaries :Clear ():Clear all contents of the dictionary>>> dic1={"name": "Zeng", "age":--} >>> dic1.clear () >>> Dic1 {}Items ()Gets the
Python practices (five lists and dictionaries) and python practices
After learning the list and the tuples, the next step is the string operation, which is, this process is really cool, otherwise it is easy to be broken down by fragmented concepts.
String
For the methods in the string, -- X -- is similar to this method with two underscores (_), which is internal
Dictionaries and collections in Python
Mapping Type: represents a collection of arbitrary objects, and can be indexed and serialized differently by another collection of almost any key value, which is unordered and indexed by key any immutable object can be used as a dictionary key, such as a string, a number, a tuple, and so on . Lists,
(a) brief descriptionA dictionary is a python built-in data structure that associates data with keys (for example: Name: Zhang San, name is key, Zhang San is data). For example: The following is a dictionary{' name ': ' Zhang San ', ' Date of birth ': ' 2899-08-12 ', ' score ': [' 3.21 ', ' 3.10 ', ' 3.01 ']}Create dictionari
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.