zoukankan      html  css  js  c++  java
  • zip函数-Python 3

    zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。

    zip函数在获取数据后,生成字典(dict)时比较好用。

    for examples:

     1 # Code based on Python 3.x
     2 # _*_ coding: utf-8 _*_
     3 # __Author: "LEMON"
     4 
     5 pList = [('li', 'LY', 80), ('zeng', 'ZW', 90), ('dudu', 'LR', 98)]
     6 names = []
     7 scores = []
     8 for i in range(len(pList)):
     9     aStr = pList[i][0]
    10     bStr = pList[i][2]
    11     names.append(aStr)
    12     scores.append(bStr)
    13 
    14 
    15 aDict = dict(zip(names, scores))
    16 print(aDict)

    运行结果:

    1 {'dudu': 98, 'zeng': 90, 'li': 80}

    当然,上述案例有更简单的实现方法:

     1 # Code based on Python 3.x
     2 # _*_ coding: utf-8 _*_
     3 # __Author: "LEMON"
     4 
     5 pList = [('li', 'LY', 80), ('zeng', 'ZW', 90), ('dudu', 'LR', 98)]
     6 aDict = {}
     7 for data in pList:
     8     aDict[data[0]] = data[2]
     9     # dict[Key]=value
    10 print(aDict)
    作者:Lemon
    出处:个人微信公众号:“Python数据之道”(ID:PyDataRoad)和博客园:http://www.cnblogs.com/lemonbit/
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文出处,否则保留追究法律责任的权利。
  • 相关阅读:
    HDU 5671 矩阵
    HDU 5670
    UVA 11995 STL 使用
    VK Cup 2016
    字段定义
    apache用户
    apache
    使用第三方登录
    setex()
    如果客户端禁用了cookie,如何实现session
  • 原文地址:https://www.cnblogs.com/lemonbit/p/6238477.html
Copyright © 2011-2022 走看看