python 两个list合并成一个dict的方法

摘要: def meshList(): list2 = [1, 2, 3, 4, 5 ];list3 = ["a", "b", "c", "d","e"]; dict={}; length=len(list2);
def meshList():
    list2 = [1, 2, 3, 4, 5 ];
    list3 = ["a", "b", "c", "d","e"];
    dict={};
    i=0;
    length=len(list2);    
    while i<length:
        'dict[list2[i]]=list3[i];这种方法也可以'
        dit={list2[i]:list3[i]};
        dict.update(dit);
        i+=1;
    return dict;

if __name__ == '__main__':
    print Run();


上面是不使用python的内置函数的情况下,还有一种方式,使用py的内置函数:

dict(zip(file, titles))

如图所示,完成:

1569124552533818.png


本文由 帝一博客 原创发布。用户在本站发布的原创内容(包括但不仅限于回答、文章和评论),著作权均归用户本人所有。独家文章转载,请联系邮箱:17762131@qq.com。获得授权后,须注明本文地址: https://bubukou.com/pythonpachong/1457.html

网友留言评论

0条评论