'] = 456Print(obj)= = "{' K1 ': 123, ' K2 ': 456}Python single-instance mode#Python Single-instance mode#1. Using the class method to implement#2. Using static fields to implementclassfoo:instance=Nonedef __init__(self,name): Self.name=name @classmethoddefget_instance (CLS):#CLS class name ifCls.instance:#If the static field instance is True returncls.instanceElse: obj= CLS ('Alex') Cls.in
) Circular Dictionary Dict方法一:info = { ‘stu1101‘:"Zhang san", ‘stu1102‘:"Li si", ‘stu1103‘:"Wang wu",}for i in info: print(i,info[i])执行结果:stu1101 Zhang sanstu1102 Li sistu1103 Wang wu方法二:for k,v in info.items(): #会先把dict转成list,数据里大时莫用 print(k,v)执行结果:stu1102 Li sistu1103 Wang wustu1101 Zhang san 2.4. Procedure Practice
Program: Level Thr
电话号码还是地址?Request=raw_input (' Phone number (p) or address (a)? ')#使用正确的键Key=request #如果请求的既不是 ' P ' is also not ' a 'If request = = ' P ': key= ' phone 'If request = = ' A ': key= ' addr 'Default value provided by the #使用get ()Person=people.get (name,{})Label=labels.get (Key,key)Result=person.get (Key, ' not available ')print '%s '%s is%s. "% ( Name,label,result)----------------------------------------Name:bethPhone number (p) or address (a)? Batting
Python first recognizes complex data types (list, dictionary, and tuple) and pythontuple
In the previous article, we briefly understood the data types: number and string. In this article, we will introduce the heavyweight data types: list, dictionary, and metadata tuple.
I. List:
① What is the list?
For example, if I am a Tom, we can write me = 'Tom 'when definin
dictionary. The following is an example of the use of a dictionary using a data system that emulates user logins .#!/usr/bin/env python db = {} def newuser (): prompt = ' register: ' While true:name = Raw_input (Prompt) if Db.has_key (name ): prompt = ' name taken, try another: ' Continue else:break pwd = Raw_input (' passwd: ') db[name] = pwd def olduser ():
0x 01 Dictionary IntroductionThe child and list are the two most common data types in Python, the dictionary is the data type of the key-value pair (key-value) format, it has the same index as the list, but not the subscript in the list, but the key is used as the index, the value of the key is values, so the dictionary
This article mainly introduces the basic introduction of Python, including file input/output, built-in type, dictionary operation and other ways to useI. Variables and expressionsThe code is as follows:>>> 1 + 12>>> print ' Hello World 'Hello World>>> x = 1>>> y = 2>>> x + y3Python is a strongly typed language and cannot be converted to the appropriate type based
Let's take a look at some of the basic ways that you can iterate through a dictionary in Python:
Script:
#!/usr/bin/python dict={"A": "Apple", "B": "Banana", "O": "Orange"} print "######### #dict ######################" For i in Dict: print "dict[%s]="% i,dict[i]
Dictionary:A python dictionary is another mutable container model, and can store any type of object, such as strings, numbers, tuples, and other container models.First, create a dictionaryA dictionary consists of a pair of keys and corresponding values. Dictionaries are also referred to as associative arrays or hash tables. The basic syntax is as follows:Dict = {
Python dictionary is a very important base type in Python, it is an efficient base type, not only used in writing programs, but even the implementation of Python's low-level use of a large number of dictionaries.
Learn Python dictionaries mainly from a few aspects
1. Basic use of Dictionaries
2. The extension of the
Author: Vamei Source: Http://www.cnblogs.com/vamei Welcome to reprint, please also retain this paragraph of the statement. Thank you.
The Basics tutorial introduces basic concepts, especially objects and classes.
The Advanced tutorial further expands the basic tutorial to illustrate the details of Python. Hopefully, after the advanced tutorials, you have a more comprehensive understanding of python.
As we
This article is referenced from the basic Python Tutorial (second edition)
Operation
Grammar
Example
Results
Build a dictionary
Dict ()1. Creating a dictionary with keyword parameters2. Building a dictionary with other mappings as parameters
1.d = Dict (name= ' Gumby
',' Age': 29,'Sex':'male'}2 3 forKinchD:4 Printk5 6 It's the same as this print. 7 8 forKinchD.keys ():9 PrintK How to do the dictionary:1. Newd[' Tel ']= ' 1234568 'At this point D is {' name ': ' Tom ', ' age ': ' Sex ': ' Male ', ' tel ': ' 12345678 '}2. Modificationd[' Tel ']= ' 88888 '3. DeleteDel (d[' tel ') Delete one of the key values in the dictionaryDel d Delete the en
Python programming to extract the subset from the dictionary for analysis, python subset
This example describes how to extract a subset from a dictionary using Python programming. We will share this with you for your reference. The details are as follows:
First, we will cons
=> 3Note: using D. iteritems (), D. iterkeys () is much faster than without iter.
【
One of the common usage of the dictionary replaces switch]
In C/C ++/Java, there is a convenient function switch, for example:
public class test {public static void main(String[] args) {String s = "C";switch (s){case "A": System.out.println("A");break;case "B":System.out.println("B");break;case "C":System.out.println("C");break;default:System.out.println("D");}}}To
Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!Basic concepts, especially objects and classes, are described in the basic tutorials.The advanced tutorial further expands on the basic tutorials to illustrate Python details. Hopefully, after the advanced tutorial, you have a more comprehensive understanding of python.As we said before, the list is a class in Py
= {' name ': ' Tom ', ' Age ': '}dict1.pop ' print dict1 run result: {' name ': ' Tom '} 12,popitem randomly Delete Keydict1 = {' name ': ' Tom ', ' Age ': '}dict1.popitem ' () print Dict1 run result: {' name ': ' Tom '} 13,setdefault if the key is not in the dictionary, set the key and value, and if so, use the default an
Python list derivation, Dictionary derivation, set derivation usage instance analysis, python instance analysis
This article describes the Python list derivation, Dictionary derivation, and set derivation usage. We will share this with you for your reference. The details are
":System. out. println ("C ");Break;Default:System. out. println ("D ");}}}
To implement the same function in Python,Method 1 is implemented using the if and else statements, for example:
The code is as follows:
From _ future _ import pision Def add (x, y ):Return x + y Def sub (x, y ):Return x-y Def mul (x, y ):Return x * y Def p (x, y ):Return x/y Def operator (x, y, sep = '+ '):If sep = '+': print add
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.