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
comes from (Key, value), but there is no special order for the items to be returned.Copy codeThe Code is as follows:>>> D = {'title': 'python website', 'url': 'http: // www.python.org ', 'spam': 0}>>> D. items ()[('Url', 'HTTP: // www.python.org '), ('spam', 0), ('title', 'python website')]
The iteritems method works roughly the same but returns an iterator object instead of a list:
Copy codeThe Code is as
Tuple definitionA tuple is another data type, similar to a list.The tuple is identified with a "()". The inner elements are separated by commas. However, an element cannot be assigned two times, which is equivalent to a read-only list.Example:tuple = (' ABCD ', 786, 2.23, ' John ', 70.2)list = [' ABCD ', 786, 2.23, ' John ', 70.2]Tuple[2] = 1000 # tuples are illegal to applyList[2] = 1000 # is a legitimate
, ' 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 paramete
. An empty dictionary (with no entries) consists of two curly braces, like this: {}.dict function: we can call the Dict function to create a dictionary for other mappings (such as other dictionaries) or for sequences or keyword parameters (keys, values). 1 item=[('name','baiyishaonian'), (' Age ','d= ')]2 dict (item)3 Print DBasic operationsLen (d) : Returns the
[Python-dictionary usage] Three methods for creating a dictionary: python3
# Create an empty dictionary empty_dict = dict () print (empty_dict) # Use ** kwargs variable parameters to input keywords to create a dictionary a = dict (one = 1, two =
DescribeThe Python Dictionary (Dictionary) SetDefault () function is similar to the Get () method, and if the key does not already exist in the dictionary, the key is added and the value is set to the default value.GrammarSetDefault () method syntax:Dict. SetDefault(key,default=None) Parameters
Key--Th
1. Dictionary definition, key unique, value arbitrarydic={key0:value0,key1:value1}2. Outputprint(dic[key0])>>:value03. Traversing a dictionary3.1keys () traverse the key in the dictionarydic={‘name‘:‘zhangsan‘,‘age‘:20}for k in dic.keys(): print(k)>>:nameage 3.2values () traverse the value in the dictionarydic={‘name‘:‘zhangsan‘,‘age‘:20}for v in dic.values(): print(v)>>:zhangsan203.3items
Python Data Type _ tuples, common dictionary operations (Introduction), python Data Types
Tuples
The Python tuples are similar to the list, except that the elements of the tuples cannot be modified.
The tuples use parentheses, And the list uses square brackets.
Creating tuples is simple. You only need to add elements i
'] = ' Eric '>>> d.has_key (' name ')True>>>6.items and IteritemsThe items method returns all dictionary entries in a list, each of which comes from the (key, value). But the items are returned with no special order.>>> d = {' title ': ' Python Web site ', ' url ': ' http://www.python.org ', ' spam ': 0}>>> D.items ()[(' url ', ' http://www.python.org '), (' spam ', 0), (' title ', '
raises an exception because the dictionary after del does not exist: dict ['age']: # Traceback (most recent call last): # File "test. py ", line 8, in
V. Features of dictionary keys
Dictionary values can be any python object without restrictions. They can be either standard objects or user-defined objects, but cannot
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
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
A sequence is a data structure in Python, and mapping is another. Mappings (mapping) refer to values by name. The only mapping structure built in Python is a dictionary. The values in the dictionary are not in a special order, but are stored in a specific key. Keys can be numbers, strings, or even groups of tuples. 1,
problem: construct a dictionary, which is a subset of another dictionaryAnswer: The simplest way is to use a dictionary derivationEG1:1.>>>prices = {' ACME ': 45.23, ' AAPL ': 612.78, ' IBM ': 205.55, ' HPQ ': 37.20, ' FB ': 10.75}>>>P1 = {Key:value for key, value in Prices.items () if value > 200}>>>p1{' AAPL ': 612.78,' IBM ': 205.55}2.
{' B ': 2} #清空字典 >>> dict1.clear () >>> dict1 #字典变为空了 {} #删除字典对象 >>> del dict1 >>> dict1 Traceback (most recent call last): Accessing values in the dictionaryPut the corresponding key into square brackets, as follows:Dict = {' name ': ' Zara ', ' age ': 7, ' Class ': ' First '} print ("dict[' name ']:", dict[' name ']) print ("dict[' age ']:", dict [' Age '])#以上实例输出结果:Dict[' Name ']: Zaradict['
#!/usr/bin/env python
#-*-coding:utf-8-*-
# 1, Scenario 1: Using built-in functions sorted from
random import randint
d = {X:rand Int (m) for x in ' Xadfyz '}
# Converts a dictionary to a list containing tuples, using the ZIP function results such as: [(b, ' a '), (E, ' d '), (+, ' F '), (+, ' Y '), ($, ' X '), (+, ' z ')]
# result = Zip (D.values (), D.keys ()) Result
= Zip (D.itervalues (), D.keys ())
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.