`

Python 字典的遍历

阅读更多
# encoding: utf-8

test_dict = {  
    'attack': 379,  
    'attack_growth': 16.8,  
    'bp_size': 80,  
    'critical_rate': 0.15,  
    'ctype': '2',  
    'defense': 155,  
    'defense_growth': 8.25,  
    'exp_type': 'c',  
    'fate_id': ['ch_10004', 'ch_10005', 'ch_10006', 'ch_20003', 'ch_20004'],  
    'hp': 1861,  
    'hp_growth': 69.6,  
    'max_talent_lv': 7,  
    'name': 'caocao',  
    'pvp_skill_rate': 1,  
    'recover': 281,  
    'recover_growth': 11.8,  
    'souls_needed': 30,  
    'star': '5',  
    'talent_lv': 0,  
    'who_is': 'caocao',  
}  

# 不同的遍历方法
def test1():
    for key in test_dict:   # 这种最快, 其实也很显而易见
        pass

def test2():
    for key in test_dict.keys():  
        pass

def test3():
    for key, value in test_dict.items():  
        pass 

if __name__ == '__main__':  
    from timeit import Timer  
    t1 = Timer("test1()", "from __main__ import test1")  
    t2 = Timer("test2()", "from __main__ import test2")  
    t3 = Timer("test3()", "from __main__ import test3")
    # 执行100,000次的时间  
    print t1.timeit(100000)  
    print t2.timeit(100000)  
    print t3.timeit(100000)
    # 3次执行100,000次的时间  
    print t1.repeat(3, 100000)  
    print t2.repeat(3, 100000)  
    print t3.repeat(3, 100000) 

 结果:

0.0656280517578
0.0932841300964
0.217456817627
[0.06363296508789062, 0.062133073806762695, 0.0649728775024414]
[0.08316302299499512, 0.08275103569030762, 0.08605194091796875]
[0.21467804908752441, 0.19786405563354492, 0.20139288902282715]

 

 但空的循环没有意义

# encoding: utf-8

test_dict = {    
    'attack': 379,    
    'attack_growth': 16.8,    
    'bp_size': 80,    
    'critical_rate': 0.15,    
    'ctype': '2',    
    'defense': 155,    
    'defense_growth': 8.25,    
    'exp_type': 'c',    
    'fate_id': ['ch_10004', 'ch_10005', 'ch_10006', 'ch_20003', 'ch_20004'],    
    'hp': 1861,    
    'hp_growth': 69.6,    
    'max_talent_lv': 7,    
    'name': 'caocao',    
    'pvp_skill_rate': 1,    
    'recover': 281,    
    'recover_growth': 11.8,    
    'souls_needed': 30,    
    'star': '5',    
    'talent_lv': 0,    
    'who_is': 'caocao',    
}    
  
# 不同的遍历方法  
def test1():
    data = {}
    for key in test_dict:
        data[key] =  test_dict[key]
    return data

def test2():  
    data = {}
    for key in test_dict.keys(): 
        data[key] =  test_dict[key]   
    return data  
  
def test3():
    data = {} 
    for key, value in test_dict.items():    
        data[key] = value
    return data

print test1()
print test2()
print test3()
  
if __name__ == '__main__':    
    from timeit import Timer    
    t1 = Timer("test1()", "from __main__ import test1")    
    t2 = Timer("test2()", "from __main__ import test2")    
    t3 = Timer("test3()", "from __main__ import test3")  
    # 执行100,000次的时间    
    print t1.timeit(100000)    
    print t2.timeit(100000)    
    print t3.timeit(100000)  
    # 3次执行100,000次的时间    
    print t1.repeat(3, 100000)    
    print t2.repeat(3, 100000)    
    print t3.repeat(3, 100000) 

 

结果:

0.39611697197
0.423596143723
0.477458953857
[0.41298508644104004, 0.3765869140625, 0.3731379508972168]
[0.4185318946838379, 0.42104601860046387, 0.4249718189239502]
[0.42894506454467773, 0.4049968719482422, 0.3907771110534668]

差别没那么明显, 不同的遍历在不同地方处理数据

 

 

 

分享到:
评论

相关推荐

    Python字典遍历操作实例小结

    主要介绍了Python字典遍历操作,结合实例形式总结分析了Python遍历字典键值对、遍历键、遍历值等相关操作技巧,需要的朋友可以参考下

    Python简单遍历字典及删除元素的方法

    本文实例讲述了Python简单遍历字典及删除元素的方法。分享给大家供大家参考,具体如下: 这种方式是一定有问题的: d = {'a':1, 'b':2, 'c':3} for key in d: d.pop(key) 会报这个错误:RuntimeError: dictionary...

    python 循环遍历字典元素.docx

    python 循环遍历字典元素.docx

    Python字典创建 遍历 添加等实用基础操作技巧

    字段是Python是字典中唯一的键-值类型,是Python中非常重要的数据结构,因其用哈希的方式存储数据,其复杂度为O(1),速度非常快。下面列出字典的常用的用途. 一、字典中常见方法列表 代码如下: D.clear() #移除D中的...

    第七天 03字典遍历【千锋Python人工智能学院】1

    1. 有一个列表persons,保存的数据都是字典字典遍历<1> 遍历字典的key(键)<2> 遍历字典的value(值)<3> 遍历字典的项(元素)<4> 遍

    Python3实现的字典遍历操作详解

    本文实例讲述了Python3字典遍历操作。分享给大家供大家参考,具体如下: 字典是针对非序列集合而提供的一种数据类型。 通过任意键查找集合中值信息的过程叫映射,python通过字典实现映射。 为字典赋值: >>> d={'...

    python遍历字典函数.docx

    python遍历字典函数 Python是一种高级编程语言,它提供了许多内置函数和模块,使得编程变得更加简单和高效。其中,遍历字典是Python中常用的操作之一。在本文中,我们将介绍如何使用Python遍历字典函数。 Python中的...

    Python中遍历字典过程中更改元素导致异常的解决方法

    主要介绍了Python中遍历字典过程中更改元素导致错误的解决方法,针对增删元素后出现dictionary changed size during iteration的异常解决做出讨论和解决,需要的朋友可以参考下

    python中将字典形式的数据循环插入Excel

    1.我们看到字典形式的数据如下所示 list=[[2891-1, D],[2892-1, D],[2896-1, B],[2913-1, 0],[2913-2, 1],[2913-3, 1]] 此list是在数据库中存在的 2.我们把这些样式的字点数据做一次数据转换 把list转换成字典的...

    Python中字典对象的遍历示例

    使用环境:需要先安装PyCharm(请自己百度下载安装),以及然后官网上下载Python 2.7版本,以及Python 3.7版本后,安装在自己的电脑上。 使用步骤: 1、下载解压缩之后,打开...目的:帮助理解字典对象的遍历操作。

    Python多维/嵌套字典数据无限遍历的实现

    下面小编就为大家带来一篇Python多维/嵌套字典数据无限遍历的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    python字典的遍历3种方法详解

    主要介绍了python字典的遍历相关知识详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    python 循环遍历字典元素的简单方法

    下面小编就为大家带来一篇python循环遍历字典元素的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    头歌Python入门之元组与字典

    3.字典的遍历:遍历字典中的键-值对,遍历字典中的键(Python为字典类型内置了keys()方法,该方法会将字典里的键遍历出来,keys()方法每次都是将menu菜单中的键输出,显示菜名),遍历字典中的值(Python为字典类型...

    Python实现字典的遍历与排序功能示例

    主要介绍了Python实现字典的遍历与排序功能,结合实例形式分析了Python字典的遍历与排序相关函数与使用技巧,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics