程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Enumerate usage and zip usage in Python: parallel traversal, collection usage and characteristics

編輯:Python

This article has participated in 「 New people's creation ceremony 」 Activities , Start the road of nuggets creation together .

python in enumerate usage

1、 Use scenarios

When you use for Loop through , You need to use two parameters :

  • Traversed elements
  • Traverse the offset value of the element , That is, the subscript of the element

2、 usage

enumerate Function returns a generator object , This object supports iterative protocols , After that, I will discuss in my blog . This means that after the function is called , Will return a tuple (index,value).

S = [‘a’,‘b’,‘c’]
for (i,value) in enumerate(S):
print(i, 'and', value)
>>>
0 and a
1 and b
2 and c

Use enumerate Function can get the element and its subscript at the same time , Some code operations are omitted , It's more convenient .

python in zip usage : Parallel traversal

python The built-in zip The function allows us to use for To traverse multiple sequences . for example :

L1 = [1,2,3,4]
L2 = [5,6,7,8]
lis = list(zip(L1,L2))
print(lis)
>>>[(1,5),(2,6),(3,7),(4,8)]
for (x,y) in zip(L1,L2):
print(x,y,"=",x+y)
>>>
1 5 = 6
2 6 = 8
3 7 = 10
...
...
...

The above example 1 in ,zip Create tuple pairs and store them in the list . example 2 Use in for Go through the epochs and make pairs . That is to say, it scans in the loop L1 and L2. Here we have a preliminary understanding of zip function . Let's take a closer look at zip:

  • zip Any type of sequence is acceptable ( Iteratable object ), Including file types . But it should be noted that ,zip() Both parameters in must be the same sequence object , Or it's all lists , Or they are all strings .
  • zip There can be more than two parameters . such as :
L1 = [1,2,3,4]
L2 = [5,6,7,8]
L3 = [4,3,2,1]
list(zip(L1,L2,L3))
>>>[(1,5,4),(2,6,3),(3,7,2),(4,8,1)]
  • When the parameter lengths of two sequences are different ,zip Truncate with the shortest sequence . such as :
L1 = [a,b,c]
L2 = [1,2,3,4]
list(zip(L1,L2))
>>>[(a,1),(b.2),(c,3)]
  • have access to zip Construct a dictionary
keys = ['name','age','job']
vals = ['ww',23,'student']
d = dict(zip(keys,vals))
>>>{'name' : 'ww', 'age' : 23, 'job' : 'student'}

python The use and characteristics of collections in

1、 Application 1 : duplicate removal

python One of the functions of a set in is to remove duplicate elements from a string or list . This function is very useful . such as :

Output is :

It's not enough just to know , There are several features to note when using set de duplication . Let's look at the above code , It was found that the order of the elements in the list changed after the list was changed to a set , in the majority of cases , We all want the order of sets to be the same as that of lists . So readers can read another blog post link : python How to remove duplicate elements in .

In addition, the author found that , When performing set conversion on numbers , The order of the numbers is automatically sorted . Please have a look at :

Output is :

So we come to :

  • When processing strings , After collection conversion , The order of the elements will change ;
  • When working with numbers , After collection conversion , Elements will be sorted automatically ;

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved