1. When you want to get both the index and the corresponding value, you can use the enumerate () function
For I, V in enumerate ([' Tic ', ' tac ', ' toe ']): print I, V0 tic1 tac2 toe
2. To loop two or more sequences at the same time, you can pair them with the zip () function.
Questions = [' name ', ' Quest ', ' favorite color ']answers = [' Lancelot ', ' The Holy Grail ', ' Blue ']for Q, A in zip (questions, Answers): print ' What's your {0}? It is {1}. '. Format (q, a)------------------------------------------------------what is your name? It is Lancelot.what is your quest? It is the Holy Grail.what is your favorite color? It is blue.
3. To reverse the loop sequence, first the specified sequence of positive direction, and then call the revers () function.
For I in Reversed (xrange (1, 2)): Print I--------------------------------------97531
4. To loop a sequence in sort order, use the sorted () function, which returns a new sort sequence without changing the original sequence
Basket = [' apple ', ' orange ', ' apple ', ' pear ', ' orange ', ' banana ']for f in sorted (set (basket)): print F--------------- ----------------------------------------------------Applebananaorangepear
5. When using a dictionary, you can use the Iteritems () method to retrieve both the keyword and the corresponding value.
Knights = {' Gallahad ': ' The Pure ', ' Robin ': ' The Brave '}for K, V in Knights.iteritems (): print K, v------------------- --------------------Gallahad the Purerobin the Brave
6. Sometimes it can be challenging in cyclic sequences; You can create a new list that is generally simpler and more secure.
Mport Mathraw_data = [56.2, float (' nan '), 51.7, 55.3, 52.5, float (' nan '), 47.8]filtered_data = []for value in Raw_data:
if not Math.isnan (value): filtered_data.append (value) print Filtered_ Data-----------------------------------------------------[56.2, 51.7, 55.3, 52.5, 47.8]
7. Comparison of sequences between different types
Sequence objects can be compared to other objects that have the same sequence type. Compare using dictionary sorting: First compare the top two items, and if they are different, decide to compare the results; If they are equal, the next two items are compared, and so on, until either sequence is exhausted. If the two items that you want to compare are sequences of the same type, the dictionary comparison is recursive. If all items of the two series are compared equally, the sequences are considered equal. If one sequence is the initial subsequence of another sequence, the shorter sequence is a smaller (smaller) sequence. The dictionary ordering of strings uses an ASCII sort of single character. Some examples of comparisons between sequences of the same type:
(1, 2, 3) < (1, 2, 4) [1, 2, 3] < [1, 2, 4] ' ABC ' < ' C ' < ' Pascal ' < ' Python ' (1, 2, 3, 4) < (1, 2, 4) (1, 2) c5/>< (1, 2,-1) (1, 2, 3) = = (1.0, 2.0, 3.0) (1, 2, (' AA ', ' ab ')) < (1, 2, (' abc ', ' A '), 4)
Note that it is legal to compare objects of different types. The results are deterministic but random: the types are sorted by their name. Therefore, the list is always smaller than the string, and the string is always smaller than the tuple. A mixed number type is compared according to its value, so 0 equals 0.0.
Python-Looping techniques (looping techniques)