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/
Python-Level 7 dictionary query and python dictionary Query
1 #-*-coding: UTF-8-*-2 3 4 # job requirements: 5 # five menus of municipalities, provinces, municipalities, counties, districts, and sub-streets; 6 # Step by step to all
In this article, we'll look at the dictionaries in Python, and in this article I'll
Python dictionaryModify the instructions and illustrate how to modify
Python dictionaryValues within the. Don't say much nonsense, let's get into the article.
First we need to know what a modified dictionary is.
Modify
Python basics 2-dictionary, python2-dictionary
Dictionary
Dictionary is another variable container model that can store any type of objects.
Each key-value (key => value) pair of the dictionar
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
Python self-study day-Two day (2)-python-list-tuples-dictionary, python-
ListFormat: name = []Name = [name1, name2, name3, name4, name5]
# List operations
Name. index ("name1") # query the name of the specified value. count ("name1") # query the name of the total number of s
first. >>> import JSON >> > Dordereddict ([( " foo , 1), (" bar , 2), (" spam , 3), ( ' grok , 4)]) >>> Json.dumps (d) " {" foo ": 1," Bar ": 2," Spam ": 3," Grok ": 4} >>> Add: Ordereddict internally maintains a doubly linked list, which arranges the position of the keys according to the order in which they are added. The first newly added element is placed at the end of the list, and subs
A dictionary of Python Learning Notes (v) 2Often used in practical programming.Example 1:copy
>> ad = {"name": "WTF", "hig": "180"}>> BD = AD>> BD{' name ': ' WTF ', ' hig ': ' 180 '}>> ID (AD)4539954352>> ID (BD)4539954352Description: An object affixed with two tags, using the assignment to achieve the so-called "fake copy."If you use Copy ()>> cd = Ad.copy ()>> CD{' name ': ' WTF ', ' hig ': ' 18
elements of the list x to build a dictionary, the key value is the list of each element, value is None4. Get (key) to find value based on key5. Hash_key (x) find if the dictionary contains a key value x6. Update (x) updating another dictionary with one dictionary7, Popitem () delete the last element and return8, Pop (key) delete the key value corresponding to th
字符串右对齐 and fills a new string with a space to a specified lengthA.rsplit () #去掉右边空格或换行符A.rstrip () #去掉字符串右边的空格A.strip () #去掉字符串中的空格A.split () #字符串切片The A.splitlines () #按行分隔 Returns a list that contains rows as elements, and if NUM specifies that only num rows are sliced.A.startswith () #判断是不是以参数中内容开头A.swapcase () #对字符串的大小写字母进行转换A.title () #把字符串转成标题格式A.capitalize () #把字符串首字母变为大写A.endswith () #判断字符串是否以指定字符或子字符串结尾, commonly used to determine file typesA.expandtabs () #把字符串中的 the tab sign (' \ t ')
e.g. a=[[3,789],[1,100],[1,102],[2,102],[1,106],[2,456]]; Convert to a dictionary b={1:[100,102,102],2:[102,456],3:[789]}If cast is used:1 >>> a=[[3,789],[1,100],[1,102],[2,102],[1,106],[2,456]]; 2 >>> b=dict (a)3 >>> b4 {1:106, 2:456, 3:789} 5The result obviously removes the value corresponding to the duplicate key in
One. The characteristics of the dictionary. :(1). The Python dictionary is unordered. (and the list is orderly) we can understand the dictionary as a set of key:value such a health value pair.(2). The key of the Python
1. DictionariesKey-value pairs.1dict={"Key1":"value1",2 "Key2":"value2"3 }4 5 #add a new key/value pair6dict["Key3"] ="Value3"7 8 #Change a Key/value pair9dict["Key2"] ="NewValue"Ten One #Delete a key/value pair A deldict["Key1"] - - forKey,valueinchDict.items (): the Print("%s:%s"%(key,value)) - - if "Key2" inchdict: - Print("Find Key2")2.
}2.4update operationIn []: DOUT[73]: {' C ': 1, ' xx ': 200}In [D.update]: ({' A ': 1, ' B ': 2})In []: DOUT[75]: {' A ': 1, ' B ': 2, ' C ': 1, ' xx ': 200}The 2.5 python dictionary is a reference value , such as:In []: DOUT[76]: {' A ': 1, ' B ': 2, ' C ': 1, ' xx ': 200}I
": 1}, {"b": 1})-1>>> CMP ({"a": 2}, {"a": 1})1>>> CMP ({"a": 2}, {"a": 2,"b": 1})-1Note: When CMP compares the dictionary data, it compares the keys before comparing the values.Len>>> Len ("hello itcast")12>>> len ([1, 2, 3, 4])4> >> Len ((3,4))
dicvisit={}if not Dicvisit.has_key (Visitkey):# does not exist Diaset = set () Diaset.add (diagnosecontent) Dicvisit[visitkey]=diaset #print ("new===" +visitkey + "-----" +diagnosecontent) Else: # take out the corresponding element value in the dictionary, it is a set, and add the diagnostics into the originaldiaset= Dicvisit[visitkey] # For item in Originaldiaset: # Print Item originaldiaset.add (diagnosecontent)Tr
Data types for Python:int (integral type)On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807long (Long i
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.