This article introduces how to print a piece of user information using the python dictionary. This article introduces how to print a piece of user information using the python dictionary.
# Log file content
Alex #123 #1 eric #
This article describes a way to print out a user's information in a Python dictionary
#log File Contents
Alex#123#1eric#123#1tony#123#1
# index.py Main program logic
# {' Tony ': [[' 123 ', ' 1 '], ' Alex ': [[' 123 ', ' 1 ']], ' Eric ': [[' 123 ', ' 1 ']} This is the result, please use Python to implement obj =
on the size of the dictionary ...Demo a single-threaded barThe second is through the search engine to collect subdomains, but some subdomains will not be included in the search engine .....Refer to this articleTools | To teach you a collection of sub-domains of information collectionI think this article is also possible to introduce .... I'm too lazy to write it, just stick it over.The third is through a number of third-party websites. The implementa
key in adict. keys (): print key2. traverse the dictionary value (value)Copy codeThe Code is as follows:For value in adict. values (): print value3. Traverse dictionary items (elements)Copy codeThe Code is as follows:For item in adict. items (): print item4. traverse the di
(d.items()) [('z', 5), ('x', 1.5), ('y', 3)]
D. keys () returns all keys in dictionary d in the form of dict_keys (in Python 2, d. iterkeys () returns an iterator object for keys. Python 3 does not have this syntax)
>>> d.keys() dict_keys(['z', 'x', 'y']) >>> for x in d.keys(): print(x) z x y
D. pop (x) returns t
Dictionary and set in Python, Python dictionary set
I. Dictionary (dict)
1. Overview
The dictionary is the only ing type in Python.
You can only use immutable objects (such as strings)
DescribeThe Python Dictionary (Dictionary) Clear () function is used to delete all elements in the dictionary.GrammarClear () method syntax:Dict.clear () Parameters
NA.
return valueThe function does not have any return value.InstanceThe following example shows how the clear () function is used:#!/usr/bin/pythondict = {' Name ': ' Zara ', ' Age ': 7};p rin
allows you to create data indexes in a custom manner.
Ii. dictionary Analysis
A dictionary can contain multiple elements, each of which is well separated;
The dictionary element consists of two parts: Key and Value. The Key is the data index, the Value is the data itself, and the Key and Value are one-to-one correspondence.
1 #! /Usr/bin/
store or obtain relevant data reliably. The reason for selecting hash keys is that their values cannot be changed.Numbers and strings can be used as Dictionary keys. tuples are unchangeable but may not remain unchanged. Therefore, when using tuples for valid keys, you must add restrictions:Only unchangeable parameters such as numbers and strings can be included in the group as valid keys in the dictionary.
access the value.
info={'name':'bing','age':24,'sex':'male'}info['name']>>>'bing'
In Python, the access key is used by default to obtain the value.
info={'name':'bing','age':24,'sex':'male'}for key in info: print 'key=%s,value=%s'%(key,info[key])>>>key='age',value='24'key='name',value='bing'key='sex',value='male'
4.1.3 update the dictionary
Update a
Python data types-tuples, commonly used dictionary operations, python data type dictionaryTuples
The Python tuples are similar to the list. The difference is thatThe 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
Python nested dictionary comparison value and value implementation example, python nested dictionary example
Preface
This article provides examples to show you how to compare the values and values of the python nested dictionary a
dictionary, it will be an error, so if you do not want an error, you can use the Get and SetDefault methods.>>> a{' A ': 1, ' B ': 2, ' C ': Ten, ' d ': 11}>>> a.setdefault (' C ', None) 10>>> a.setdefault (' W ', 1000) 1000>>> a{' A ': 1, ' B ': 2, ' C ': Ten, ' d ': One, ' W ': 1000}>>> a.get (' e ', ') 20>>> a{' A ': 1, ' B ': 2, ' C ': Ten, ' d ': One, ' W ': 1000}3. Convert two lists to dictionariesUse the built-in Zip method to convert two list
#!/usr/bin/python2 3Dict = {'Name':'Zara',' Age':7,'Class':' First'};4 5Del dict['Name']; # The DELETE key is'Name'the entry6 dict.clear (); # Empty dictionary all entries7 del Dict; # Delete Dictionary8 9Print"dict[' age ']:", dict[' Age'];TenPrint"dict[' School ']:", dict['School']; One #但这会引发一个异常 because the dictionary no longer exists after using del: Adict[' Age']: - #Traceback (most recent): -# File"t
Python dictionary key-Value Pair addition and traversal methods, python dictionary
Add a key-Value Pair
First define an empty dictionary
>>> Dic = {}
Directly assign values to keys that do not exist in the dictionary.
>>> dic['nam
Common Methods for operating the dictionary type in python (recommended) and for operating the dictionary type in python
The has_key () method can check whether the dictionary contains the specified key. If yes, True is returned. Otherwise, False is returned.
Syntax format:
Python combines dictionary key values and removes duplicate elements. python dictionary
Suppose there is a dictionary in python as follows:
X = {'A': '1, 2,3 ',' B ': '2, 3,4 '}
To merge:
X = {'C': '1, 2, 3, 4 '}
Three things need
= {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A ';
print "dict[' name ']:", dict[' name ';
Print "dict[' age ']:", dict[' age ';
#以上实例输出结果:
#dict [' Name ']: Zara#dict [' Age ']: 7
If you use a key that is not in the dictionary to access the data, the error is printed as follows:
Copy Code code as follows:
#!/usr/bin/
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.