Python personal note, purely convenient query: ############################################################################################ ######### #i =[' car ', ' clothes ', ' ipone '] #语法i #查看所有的元素i [0] #取第一个i [1] # Take a second i[-1] #取最后一个 i[0:10] #取一个到第九个 i[-5:] #取最后五个 i[:5] #取前五个 i.append #i. Append (' Darren ') #往i追加一个元素. i.count #i. Count (' Darren ') #统计darren的数量. i.extend #i. Extent (range ) # Mister into a list, and then add in the I list. I.index (559) #查找第一个559的位置. i[ i.index (559) ] #先找到559 This is worth subscript, then look for 559. i.insert according to subscript #i. Insert (3, ' IBM ') insert ibm i.remove in 3rd element #i. Remove (' IBM ') delete an IBM value. i.sort #排序 (first digit, Re-letter) i.pop #删掉最后一个元素. i.reverse #倒序把所有的IBM改成HP: For n in range (I.count ( ' IBM '): #取几次 i[i.index (' IBM ')]= ' HP '---------------------- ---------------------------import stringstring.ascii_letters #列出a-Z case string.ascii_lowercase #列出a-Z lowercase A-Z into the list: A.split () #把a-Z The entire string becomes a list. List (a) # Turn A-Z every string into a list str (a) #把列表里的多个字符串拼接成一个字符串, this is not a good place to turn a list into a string, there are special uses, described below. Type (a) #查看a是列表还是字符串 '. Join (a) #把列表a的每个元素以空拼接起来, into a string. '-'. Join (a) #以-The string in a is spliced together. type (str (4)) #把数字类型int变成字符串string, The number is converted to a string because it is not allowed to write int to the file when it is written. f.writelines (a) # writes the list to the file. del a[6:11] # Delete list 6-10 rows-------------------------------python tuples--------------------------------- tuple features: Non-modifiable, non-writable, read only. tuple (a) #把一个列表变成元组. List (d) #把一个元组变成列表. --------------------------------------------------------------------------------a.isdigit () #判断是否为数字, is true, otherwise falsea.isalpha () #判断是否为字符串, or True, otherwise falsesys.exit (' goodbye! ') # Normal exit-------------------------python dictionary------------------------a key corresponding to a value:dictname ={ "Key1" : "value1", " Key2 " : " value2 "}-------------------------------------------------------the key corresponds to a list: contacts ={ "Darren" : [' man ', ' it ', ' 15621008081 '], ' Wang ' : [' man ', ' engineer ', '   ';]}------------------------------print Contacts[' WANg '] #打印出字典contact中key为wang的列表. contacts[' Wang '][1]= ' Kings ' #把字典中key为wang的列表种, element 1 changed to kingsprint contacts[' Wang '] contacts[' Wang ']=[' woman ', ' Linux ', '] ' #把key对应的列表下所有的值都改变 contacts.has_key (' Wang ') #查看是否有wang这个key. Contacts.items () #列出字典, becomes a list, and then columns each key below each list has a tuple. -----------------------------------For x,y contacts.items (): #把字典里的内容, print out each line. X is the value corresponding to the Key,y key. print x,y------------------------------------for i in contacts: #把字典里的内容打印出, similar to the way it was. Print i,contact[i]a.get (' sex ') #获取一个key, If there is a value, no error will be entered. A.keys () # Print only Keya.pop (' age ') #删除一个keya [' Sex '] = ' mans ' #添加一个key对应的值 overrides the value if the key already exists. A.setdefault (' haha ', ' it ') #添加一个key和值, such as haha have no change in data, If not, add it. A.update (b) #把b字典里的数据, fused into A's dictionary. A.values () #不重要, mainly to look at the proportion of data in a unified state. A.copy () # b=a.copy () b copies the contents of a. A.clear () #清空Dictionary adel a #删除字典a help (b.values ()) # View the usage of the help. A= ' Stu110a.find (' s ') #查找a中是否有s, some words list element position, no words for -1################################# #################### #query =raw_input (' pelase input your query: ') for k,v in test_ Dict.items (): #查看字典的key和values index = k.find ( Query) # Find out if there are elements in the key that the user wants to query, and then assign the element subscript to indexprint k[:index] + ' \033[32;1m%s\033[0m ' % query + k[index + len (query):],v # match_ counter +=1 #match自增1 key=key[:index]+query+key[index+len (Qury):]k = [a b c d e f]k[:2] #列表中取0元素到 ' two values ' k[2:] #列表中取第2个元素到最后值例子: import stringfor i in string.lowercase: print string.lowercase.index (i) Print i----------------------------------for i,v in enumerate (string.lowercase): print i,v #x效果和上面一样, is the function of enumerations, Let the obtained value have subscript. ------------------------dictionaries are unordered and cannot be sorted, now use a parameter to sort the dictionaries. A={4: ' B ', 2: ' A ', 9: ' 2 ', 8: ' d '}a.items () #把字典变成一个列表, below the list is a tuple. [(8, ' d '), (9, ' 2 '), (2, ' a '), (4, ' B ')]sorted (A.items (), key=lambda x:x[0]) #把元组赋给lambda, lamdba the first element of a list. The most important feature of-------------------------Python collection------------------------collection is the weight, but not the subscript. A=[1,2,3,4,5,6,7,8,9]b=[1,2,3,4,11]set (a) #把列表a变成集合set (b) #把列表b变成集合a & b #交集, A and B have values a | b #求合集, A and B have all been combined, but will be removed from the repetition. A -a.difference (b) #a中有的, b.difference not in B (a) #b中有的, a-b not in a #在a中, not b-a #在b中 in B, not in a a ^ b #把a和b不共有的列出来a. Add (' x ') #添加一个数值, if any, is not added, A.update ([ 10,11,12]) #添加一个列表len (a) #查看集合的长度50 in a #测试50是否在a中, when True is returned, Falsea.issubset (b) #测试是否b中的每一个元素都在a中a >=ba<=b #测试是否a中的每一个元素都在b中. Hash (' s ') #返回s的hash值拉链zip: A=[1,2,3,4,5]b=[6,7,8,9,10]zip (A, b) # Combine two lists [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]map (None, b) #如果不对称则显示为none [(1, 6), ( 2, 7), (3, 8), (4, 9), (5, 10), (0, none)]&NBSP;&NBSP;&NBSP;&NBSP;-------------------------python iterator------------------------f.next () #一次只读一行, read it down until you have an error. Interable
This article from "Small East elder brother" blog, declined reprint!
Section Seventh: Python lists, tuples, dictionaries, collections