引, can also not be specified, do not specify from the bottom to start deletingDel Name_list[i]Effect: spare tire and punctureHReverse () #列表反转Sort () #排序Remove ()Iv. GansoAOnly count () and index () are supportedFive, dictionaryAFavorite_languages = {' Jane ': ' Python ',' Jack ': ' C + + ',' Rose ': ' PHP ',' Jaymes ': ' Java ',}For I in Favorite_languages:Print (i)#默认输出keyBKey: Favorite_languages.keys ()Value: Favorite_languages.values ()Kv:favorite_languages.items ()For K, V in Favorite_langu
]int{1,2,3,4,5}varS1 = y[1:2]varS2 = y[:2]varS3 = y[1:]varS4 =y[:]fmt. Println (S1) fmt. PRINTLN (S2) fmt. Println (S3) Fmt. Println (S4)The Append function adds elements to the slice:For example:var int {123456}S1 at this time for [1, 2, 3, 4, 5, 6]Memory changes are similar to vectors in C + + : Because slices require random access, a contiguous amount of space is required, and if the number of elements exceeds capacity, Then it will reopen a new space and copy the elements in the past . 3. D
>>> people = {"Tom": 175, "Jack": "Kite": "White": "#定义一个字典>>> for name in people: #获取value的值Print People[name]180160175170>>> for name in people: #获取key的值Print NameWhiteKiteJackTom>>> People.keys () # Gets the value of all keys, just like the For loop results above[' White ', ' Kite ', ' Jack ', ' Tom ']>>> people.values () # Gets the value of all values, as in the For loop result above[180, 160, 175, 170]>>> People.has_key ("white") #判断字典中是否存在该键True>>> people["Tom"] #通过key去获取value170>>> print
that list and tuple can be "broken" into normal parameters to the function, and dict can be broken into the keyword parameters to the function (through and *). So you can easily pass a list/tuple/dict to the Format function. Very flexible.Format qualifierIt has a rich "format qualifier" (syntax is {} with: number)For example: Fill and alignPadding is used in conjunction with alignment^, : The filled character after the number, only one character, not specified by default is filled with spaces,
realization principle of dict and check Chinese dictionary is the same. Suppose the dictionary contains 10,000 characters, we need to look up a word, one way is to turn the dictionary back from the first page, until we find the word we want, this method is to find the element in the list method, the larger the list, the slower the lookup.The second method is to search the dictionary's index table (such as the Radicals) for the corresponding page number, and then go directly to the page, find th
have been replaced or added.Let replacestrudent = Strudentdictionary.updatevalue ("10 Yuan", forkey:10);can also write: Strudentdictionary.updatevalue ("10 Yuan", forkey:10);The traversal of the dictionary collection, he is divided into key traversal, value traversal, key and value face changingvarstudentdictionary=[102: "Zhang San" ,105: "Zhang San" ,109 : "Harry"]vari=0;p rintln ("------Traversal value------") ;forstudentidin studentdictionary.keys{i++;if (iThis article is from the "10 Yuan"
1. List and String conversionsList -to-string:Stitching the contents of a list into a stringTo convert a value in a list to a stringString goto list:Convert each character of a string to a value in a listSplit a string into a list2. List and dictionary conversionList to Dictionary:Turn two lists into dictionariesConvert nested lists to dictionariesDictionary goto list:Dictionary key, value to listConversion between Python dictionaries, lists, and stri
Q: What are the contents of L,m after executing the following code? def func (m): for k,v in M.items (): M[k +2] = V+2m = {1:2, 3:4}l = m # copy l[9] = 10func (L) m[ 7] = 8print (" l: , L) print ( m: " , m) # above Python version 3.6 will directly error # You cannot modify the size of a list or dictionary when iterating over a list or dictionary! # in Python version 2.6, the results are the same:print l{1:2, 3:4, 5:6, 7:8, 9:10, 11:12print m{1:2, 3:4, 5:6, 7:8, 9:10, 11:
'}>>> A{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four ', 5: ' Five '}>>> b{1: ' One ', 2: ' Both ', 3: ' Three ', 4: ' Four '}Pop ()Popitem () Random Popup>>> A.pop (2)' Both '>>> A{1: ' One ', 3: ' Three ', 4: ' Four ', 5: ' Five '}>>> A.popitem ()(5, ' five ')>>> A{1: ' One ', 3: ' Three ', 4: ' Four '}>>> A.setdefault(7, ' seven ')' Seven '>>> A{1: ' One ', 3: ' Three ', 4: ' Four ', 7: ' Seven '}>>> B = {7: ' SEVEN '}>>> A{1: ' One ', 3: ' Three ', 4: ' Four ', 7: ' Seven '}>>> A.update
The dictionary is denoted by {}, where a map collection similar to Java is stored as a key-value pair, characterized by unordered1. New operationsOutput results2. Delete operationOutput results3. Find operationsOutput results4.update Method UpdateOutput results5. Dictionary Turn ListOutput results6.fromkeys () Initialization of a dictionaryOutput results7. Multi-level dictionary nesting modificationOutput results8. Get all valueOutput results9. Output All KeysOutput results10.setdefault add a ke
:", b #item () method to form a tuple of each pair of keys and value in the dictionary.
And put these tuples back in the list. item = Dic_test.items () print item #update可以把两个字典合并到其中用update的字典内 Dic2 = {"J": "Nice"} dic_test.update (dic2) Print dic_tes T #fromkeys从keys键队列中统一命名value值 if not set is none seq = [' name ', ' age ', ' job '] print dic_test.fromkeys (seq) Print Dic_
Test.fromkeys (seq, "guess") #统一命名为guess
Let's look at the use of the dictionary below:
First, create a dictionary
(NSString * STRinchkeys) { //the object of the key ObjectforkeyNSLog (@"key=%@,vallu=%@", Str,[dict objectforkey:str]); }The second method class behaves to get a Dictionary objectDictionarywithobjectsandkeys an object corresponding to a built creationNsdictionary * Dict=[nsdictionary Dictionarywithobjectsandkeys:@"Zhang San",@"1",@"John Doe",@"2", nil]; Nsarray* Keys =[dict AllKeys];//the AllKeys property returns all the keywords for(NSString * STRinchkeys) { //the
var dict1 = [1: "A", 2: "B", 3: "C", 4: "D", 5: "E"];var test1 = ["Key1": "Hello", "Key2": "Swift", "Key3": "Learning", "Key4": "Dictionary", "Key5": "Value"]To take a value according to the keyPrint (test1["Key1"]);Number of dictionary elementsPrint (Test1.count);Add a dictionary elementDICT1[6] = "F";test1["key"] = "test";Delete an element in a dictionaryTest1.removevalue (forkey: "Key1");Modifying a dictionary elementtest1["key"] = "Li ping";Traversefor (Key,value) in test1{Print ("key\ (Key)
1 Dictionary Turn charactersDefine a dictionary:Dict = {' name ': ' Python ', ' Age ': 7}Dictionary-to-character conversions can be cast using str such as:STR (DICT)2 Word representable Dictionary:For example, our data is stored in a dictionary format in a file, but we all read the data type in the file as a character type.This time, if you want to use a dictionary to manipulate it, it's not going to work. Use the Eval function to convert data to a dictionary typeThe output is:3 Character Turn l
In the Python manual:Sorted (Iterable[,cmp,[,key[,reverse=true]])Function: Return a new sorted list from the items in iterable.The first parameter is a iterable, and the return value is a list that sorts the elements in iterable. The optional parameters are three, CMP, key, and reverse. 1) CMP Specifies a custom comparison function that receives two parameters (elements of iterable), returns a negative number if the first argument is less than the second argument, returns 0 if the first argument
list) and can be sorted according to the key function>>> A.sort ()>>> A[' H ', ' e ', ' l ', ' l ', ' O ']>>> b = [' Hello ', ' Hi ', ' See You ']>>> b.sort (key = len)>>> b[' Hi ', ' hello ', ' See You ']Shard Assignment:>>> A[' H ', ' e ', ' l ', ' l ', ' O ']>>> A[1:3] = [3,4]>>> A[' H ', 3, 4, ' l ', ' O ']3, meta-groupTuples are immutable sequences, tuples cannot be modified; creating tuples is simple, and the value is a comma.>>> A = 3,>>> A(3,)>>> A =>>> A(1, 2, 3)The main factors to rem
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.