, ' 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
Python dictionary and python beginners
A dictionary consists of multiple keys and their corresponding values (the key-value pair is an item). Each key and its value are separated by a colon, items are separated by commas (,), and the entire dictionary is enclosed by a pair o
': 'master', 'retrycount': 3}>>> d.clear() 2>>> d{}
Del allows you to delete a single element from the dictionary based on the key.Clear deletes all elements in a dictionary. Note that the set represented by empty braces indicates that a dictionary has no elements.
String is case sensitive
>>> d = {}>>> d["key"] = "value">>> d["key"] = "other value" 1>>> d{'key':
DescribeThe Python dictionary (Dictionary) items () function returns an array of traversed (key, value) tuples in a list.GrammarThe items () method syntax:Dict.items () Parameters
NA.
return valueReturns an array of (key, value) tuples that can be traversed.InstanceThe following example shows how to use the items () function:#!/usr/bin/pythondict = {' Nam
Welcome to follow Me blog: Cloud DreamerDescriptionThe Python Dictionary items () method returns a traversal (key, value) array of tuples in a list form (not a direct list, to return a list value and also call the list function).GrammarDict.items()Parameters
NA
return valueReturns an array of traversed (key, value) tuples as a list.ExampleExample code:= {‘Google‘‘www.google.com‘‘Runoob‘‘www.ru
SetDefault () methodParameters
Key--The value of the lookup.
Default-key is not present, set the defaults.
GrammarSetDefault () method syntax:Dict. SetDefault(key,default=None) DescriptionIf the parameter key does not exist in the dictionary, the key is added and the value of the key is set to default.If the parameter key already exists, do not take any actionreturn valueThe method does not have any return value.InstanceWhen
First, generate a random dictionary:From random import RandintD={x:randint (60,99) for x in "ABCDEFG"} # {' A ': +, ' B ': $, ' C ': "All, ' d ':", ' e ': ", ' F ': $, ' G ': 88}In the first way, the subclasses of collections are sorted from large to small counter:From collections Import CounterD={x:randint (60,99) for x in "ABCDEFG"}C=counter (d). Most_common () #返回一个列表, in accordance with the value of dict from large to small sort [(' B ', '), (' G ', "), (' d ',"), (' E ', "), (' A '," five
This 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 dictionary and assign values to the dictionary, basic ope
Meta-group
Tuple: The feature is the content is not variable, the list of read-only, can be queried, cannot be modifiedA = 2,print(a)print= ('a','b ','C')print(b[1])Operation Result:(2,)class'tuple'>bDictionary
Dictionary: Internal data is out of order when printed, the key value is hash, and there is uniq
Python dictionary introduction and Usage Details
#!/usr/bin/env python# -*- coding:utf-8 -*-"""
Old Rules: Environment 2.7.x in the following method. Please remember the print format (the output content should be placed in parentheses) for users of version 3.x or later)
Basic composition and usage of dictionaries
dict
, if you write a function, you can use it more conveniently,We can also use set to replace list.
>>> def addFunc(d,word,pag): d.setdefault(word,set()).add(pag) >>> d={'hello':set([1,4,9]),"good":set([1,3,6])} >>> addFunc(d,'hello',8) >>> d {'good': set([1, 3, 6]), 'hello': set([8, 1, 4, 9])} >>> addFunc(d,'bad',8) >>> d {'bad': set([8]), 'good': set([1, 3, 6]), 'hello': set([8, 1, 4, 9])}
2. use the dictionary to complete the simple factory modeDi
', ' chemistry ', 1997,2000) afterdeletingtup:traceback (mostrecentcalllast):file "test.py",line9,in dictionary ==================================================== ======= dictionary is another mutable container model and can store any type of object. Each key-value pair is used (:) split each pair with (,) to split the entire dictionary included in c
'>>> D{' name ': ' Gumby '}>>>As you can see, when the key does not exist, SetDefault returns the default value and updates the dictionary accordingly. If the key exists, it returns its corresponding value, but does not change the dictionary. The default value is optional, and this is the same as get. If not set, none is used by default.>>> d = {}>>> print D.set
in range (0, rows): if I! = 0 and I! = Rows-1: if k = 0 or k = rows-1: # because the visual effect looks more like a square, spaces are added on both sides, increase the distance from print "*", # note that "," must not be omitted. else: print "can be used to avoid line breaks.", # There are three spaces in else: print "*", # Here * Space k + = 1 I + = 1
Python note (7): dictionary, class, attribute, object instance, inheritance, python instance
(1) Brief Description
A dictionary is a Python built-in data structure that associates data with keys (for example, name: Zhang San, name is a key, and name is data ). For example, t
Python data type dictionary and python Data TypeDictionary Definition
Dictionary is a key-value data type, which is very important and serves as a basis for distinguishing the use of lists and dictionaries.
Syntax format:
info = { 'stu1101': "Aaron", 'stu1102': "Jim", 'stu1103': "Jack",}
Python dictionary operations
# Dictionary type# Create a dictionary # empty dictionary var = dict () print (var, type (var) var ={} print (var, type (var )) # create a
, using in is the best choice.
Bad
d = {'name': 'python'}if d.has_key('name'): pass
Good
if 'name' in d: pass
2. Use get to get the value in the dictionary
To obtain the value in the dictionary, you can use d [x] to access this element. However, if the key does not exist, a KeyError is returned, of course, you can first use the in operation to check whether the k
#encoding =utf-8Import Random#dict. Update (DICT2) a adds the key-value pairs of the dictionary dict2 to the dictionary dict# 7–1. Dictionary method. Which dictionary method can be used to merge two dictionaries together?Dict1 = {1: ' W ', 2: ' Y '}Dict2 = {3: ' t '}Dict1.update (DICT2)
the value.4, using a dictionary to store user-supplied data or when writing code that can automatically generate a large number of key-value pairs, it is usually necessary to define an empty dictionary first:>>> alien_0 = {}>>> alien_0[' color ' = ' green '>>> alien_0[' points '] = 5>>> Print (ALIEN_0){' Points ': 5, ' color ': ' Green '}5. Modify the values in
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.