self? A characteristic of eating, everyone is the same, everyone can eat, so this is common to everyone, there is no need to initialize the difference, butFor example, the name, everyone is different, so this name needs to initialize the function to define name, to distinguish each person. Methods, properties, functions, functions. If there is a cup, he has a variety of properties, long, wide, high, shape it has a function is used to fill the water, we call this water-loading action is called m
The prototype of the slice operator in Python is[Start:stop:step]That is: [Start index: End index: Step value]Start index: Same as other languages, starting from 0. Sequence from left to right, index of first value is 0, last one is-1 End index: The slice operator is taken to the index and does not contain the value of the index. Step value: The default is one after the other, if it is 2, it means to take one operation. When the step value is positive
After the study of the list, the meta-group was studied. Tuples and lists function similarly, but tuples cannot be modified, so tuples are called read-only lists.The following is a list of string operations:Name.capitalize () #首字母大写Name.count ("a") #数列表中有几个aName.center (5o, "-") #意思是一共打印50个字符, not enough to "-" fillName.endwith ("ex") #看字符串是否以ex结尾Name.expandtabs (tabsize=30) #将tab键转换成30个空格The following is a dictionary:In dictionaries that are not indexed by location, the dictionary is unordered
Reading speed issues for Python dictionaries and listsRecently in the genome data processing, need to read large data (2.7G) into the dictionary, and then the processed data to the dictionary key value matching, in the processed file after each read a row to find out whether in the dictionary keys, the following two code of the efficiency difference is very large:First paragraph:if (Pos in Fre_dist.keys ()):Newvalue= Fre_dist[pos]Second paragraph:if (
Python simple definition and use of the dictionary dict method example, pythondict
This example describes how to define and use a dictionary in Python. We will share this with you for your reference. The details are as follows:
# Coding = utf8print ''' the dictionary ing data type in Python is composed of key-value p
One, list (lists)Defines a list using a pair of middle (square) brackets "[]".
One of the data types built into Python is a list: Lists are an ordered collection of data that can be added and deleted randomly. For example, list all the names of all the students in the class, and list all the work numbers of the factory employees.
Do not know if there is no one like me, just contact list to say this order is not very understand, do not know what is ord
I have met once before, this time in the group also encountered a few times a problemA program written in python2.7. The dictionary derivation is used, but the server version number is python2.6 and cannot be executed.Today checked the next about Dict comprehensions, in pep274 have clear explanation.http://legacy.python.org/dev/peps/pep-0274/Implementation All implementation details were resolved in the Python
Python prints Unicode in Dict to ChineseImport JSONA = {u ' content ': {u ' address_detail ': {u ' province ': U ' \u5409\u6797\u7701 ', U ' city ': U ' \u957f\u6625\u5e02 ', U ' street_ Number ': U ', u ' district ': U ', u ' street ': U ', U ' city_code ': +, U ' point ': {u ' y ': U ' 43.89833761 ', U ' x ': U ' 125.31364243 '} , U ' address ': U ' \u5409\u6797\u7701\u957f\u6625\u5e02 '}, U ' status ': 0
One, the string s = ' python ' s1 = ' python ' + ' learn ' #相加其实就是简单拼接s2 = ' python ' * aa multiplication is actually how many times you copy yourself, and then stitch together a string slice: a= ' abcdefg ' Print (a[0]) # Index The subscript is the string consisting of elements starting with the first, the initial index is 0 and so on. Print (a[0:]) #默认到最后print
The example of this article summarizes the common operation methods of Dict dictionary in Python. Share to everyone for your reference. Specifically as follows:
The following Python code shows the common operation of the dictionary in Python, the dictionary plays a pivotal role in
If we need to get the dictionary value, we have two methods, one is through dict[' key ', and the other is the Dict.get () method.What we are sharing today is the dictionary's Get () method.Here we can use a dictionary to make a small game, assuming the user in the terminal input string: "1" or "2" or "3", return the corresponding content, if it is input other, then return "error"Some friends here may use if elif else judgment statement to operate, in
list conversionManual constructionUsing zipDictionary goto list:List (dic) = = Dict.keys ()Dict.keys () dict.values ()Dictionary transfer tuple:Tuple (DIC)--Contains only key valuesDictionary to string:STR (DIC)practice One: Put the string "K1:1|k2:2|k3:3" processed into python the form of a dictionary : {' K3 ': 3, ' K2 ': 2, ' K1 ': 1
string = "K1:1|k2:2|k3:3"dic={}Print String.Split ("|")For KV in String.Split ("|"): K,v =kv.spl
= {'Max': 1001,'Rachel': 20001,"Jim111": 14000}Print(salary['Max']) salary['Max'] = 2000Print(salary['Max'])Print(Salary.pop ('Jim','No Jim'))Print(Salary.get ('Rachel',-1))Print(Salary) value= []salary['Max'] =valuePrint(Salary)#4set-only key, no Value,set,dict key does not allow mutable objects, such as list, can only be string or integer these invariant objectsSet1 = Set ([All]) Set2= Set ([2, 3,'a']) Set1.add (4) Set1.remove (2) s=set (['SDF', 3]
The basic method of a list of Python, which goes on to say dict,tuple.One, tuple, tuple1, the definition of the tupleOr:2. Use slices to access elements in tuplesDetailed reference to the slice usage of list3, the tuple is not modifiable, is immutable variable4. Accessing elements in tuplesSecond, the use of the dictionary1. Dictionary definition2, the query operation of the dictionary3. Add a dictionary el
Common Dict Operations:d={'name':'Suki', ' Age':' -', 'Sex':'Mans', 'Addr':'Nanjing' }#dictionary values are convenient, but dictionaries are not sequential, List has subscriptPrint(d['name'])#non-existent key will errorPrint(D.get ('name'))#non-existent key returns none#add keyd['Height']=175D.setdefault ('Weight','Wuyi')#Modifyd['Height']=175D.setdefault ('Weight', 51)#DeleteD.pop ('Weight') D.popitem ()#randomly delete a keydeld['Height']d.c
Python extracts dict and converts it to xml/json/table, and outputs the implementation code, dictjson
Core code:
#! /Usr/bin/python #-*-coding: gbk-*-# Set the source file output format import sysimport getoptimport jsonimport createDictimport myConToXMLimport myConToTable def getRsDataToDict (): # obtain the input parameters in the console and find the source da
Python uses the list or dict field mode to read files,
Preface
Python is a powerful tool for processing text data. It is extremely simple to read, split, filter, and convert, so developers do not need to consider complicated stream file processing processes (compared with JAVA, hey ). Some complex text data processing and computing tasks, including writing Stream
# # Python has four kinds of sequences for storing data--list, tuple, dict, set# # List-an ordered sequence of data of any type can be stored-a list can consist of 0 or more elements, separated by commas, and the entire list wrapped in square brackets:-Create-Create a list using [] or list ()-New_list = ["Stanley", "Loves", "Lily"]-Access and modification-use list name [index] to access and modify list elem
Part of the Source: http://blog.sina.com.cn/s/blog_5c6760940100bmg5.html① Direct Assignment----result is a reference to a different nameThe modification of the new dictionary has a full effect on the original dictionary, just a reference to the relationship>>> C = {'a': 1,'b': 2}>>> d =C>>> d['e']=3>>>d{'a': 1,'b': 2,'e': 3}>>>c{'a': 1,'b': 2,'e': 3}>>> f =dict ()>>> f =D>>> f["k"]=4>>>f{'a': 1,'k': 4,'b': 2,'e': 3}>>>c{'a': 1,'k': 4,'b': 2,'e': 3}② u
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.