Introduction to Python Programming (015) - Usage of the enumerate() function

Introduction to Python Programming (015) - Usage of the enumerate() function

1. Syntax of enumerate() function

The function of the enumerate() function is to form a traversable or iterable object into an index sequence according to the set sequence number. The syntax format is as follows:

enumerate(iterable[,start])
说明:1)iterable:表示一个序列、迭代器或其他支持迭代的对象。2)start:用于设置下标的起始位置,默认为03)函数返回一个列表(类型为:enumerate),列表由新生成的序号和原有数据组成的元组构成。

For example:

lang = ["java","go","c","vb","c++"]
list1 = enumerate(lang,1)
print("list1的类型:",type(list1))
print(list1)
print(list(list1))
for i in enumerate(lang,1):
    print(i)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
list1的类型: <class 'enumerate'>
<enumerate object at 0x000001E137D1F780>
[(1, 'java'), (2, 'go'), (3, 'c'), (4, 'vb'), (5, 'c++')]
(1, 'java')
(2, 'go')
(3, 'c')
(4, 'vb')
(5, 'c++')

2. Create an index for an iterable object

The enumerate() function can quickly index and number the elements in an iterable object. For example:

stu = ["刘刚","张静静","王芸","张云涛","刘贝贝"]
list1 = enumerate(stu,1)
for item in list1:
    print(item)
    
运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
(1, '刘刚')
(2, '张静静')
(3, '王芸')
(4, '张云涛')
(5, '刘贝贝')

You can also use the enumerate() function directly in the sequence:

stu = ["刘刚","张静静","王芸","张云涛","刘贝贝"]
for item in enumerate(stu,1):
    print(item)
    
运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
(1, '刘刚')
(2, '张静静')
(3, '王芸')
(4, '张云涛')
(5, '刘贝贝')

Strings can be indexed using the enumerate() function:

str = "张王李赵刘曹关"
for item in enumerate(str,1):
    print(item)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
(1, '张')
(2, '王')
(3, '李')
(4, '赵')
(5, '刘')
(6, '曹')
(7, '关')

3. Convert strings, lists, etc. into dictionaries

1. Convert string to dictionary

str = "张王李赵刘曹关"
name = dict(enumerate(str,1))
print(name)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    1: '张', 2: '王', 3: '李', 4: '赵', 5: '刘', 6: '曹', 7: '关'}

2. Convert the list into a dictionary and use the serial number as the key of the dictionary

str = ["马芸","马花疼","黄征","丁蕾","许志军","张三疯"]
name = dict(enumerate(str,1))
print(name)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    1: '马芸', 2: '马花疼', 3: '黄征', 4: '丁蕾', 5: '许志军', 6: '张三疯'}

3. Convert the list into a dictionary and use the serial number as the value of the dictionary

str = ["马芸","马花疼","黄征","丁蕾","许志军","张三疯"]
name = dict((item,i) for i,item in enumerate(str,1))
print(name)

运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    '马芸': 1, '马花疼': 2, '黄征': 3, '丁蕾': 4, '许志军': 5, '张三疯': 6}

4. Create indexes for dictionary keys

Use the enumerate() function to index the keys of the dictionary and generate a tuple consisting of the serial number and the key of the dictionary. For example:

dict = {
    
    "马芸":20,"马花疼":85,"黄征":76,"丁蕾":88,"许志军":102,"张三疯":56}
for item in enumerate(dict,1):
    print(item)
for i,item in enumerate(dict,1):
    print(i,":",item,end="  ")
    
运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
(1, '马芸')
(2, '马花疼')
(3, '黄征')
(4, '丁蕾')
(5, '许志军')
(6, '张三疯')
1 : 马芸  2 : 马花疼  3 : 黄征  4 : 丁蕾  5 : 许志军  6 : 张三疯  

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132175030