enumerate 和 dict.items()】的更多相关文章

对 list 遍历 a_list = [1,2,3] for index,iterm in enumerate(a_list): print(index,iterm) 对 dict 遍历 dict = {"yellow":1, "red":2} for k, v in dict.items(): print(k,v)…
python2里面,dict.items返回的是数组,six.iteritems(dict)则返回生成器. 意味着,dict很大的时候,后者不占用内存. >>> import six >>> six.iteritems({'a':1,'b':2}) <dictionary-itemiterator object at 0x7fa3101cb940> >>> {'a':1,'b':2}.items() [('a', 1), ('b', 2)]…
dict.items() 1 >>> d = dict(one=1,two=2) 2 >>> it1 = d.items() 3 >>> it1 4 dict_items([('one', 1), ('two', 2)]) 5 >>> type(it1) 6 <class 'dict_items'> 7 >>> from collections import Iterable 8 >>>…
1.dict.items() 例子1: 以列表返回可遍历的(键, 值) 元组数组. dict = {'Name': 'Runoob', 'Age': 7} print ("Value : %s" % dict.items()) 返回结果: Value : dict_items([('Age', 7), ('Name', 'Runoob')]) 例子2:遍历 dict = {'Name': 'Runoob', 'Age': 7} for i,j in dict.items(): prin…
         Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda : 4.5.11    typesetting : Markdown   code """ @Author : 行初心 @Date : 18-9-23 @Blog : www.cnblogs.com/xingchuxin @Gitee : gitee.com/zhichengji…
python新手必躺的5大坑 对于Python新手来说,写代码很少考虑代码的效率和简洁性,因此容易造成代码冗长.执行慢,这些都是需要改进的地方.本文是想通过几个案列给新手一点启发,怎样写python代码更优雅. 新人躺坑之一:不喜欢使用高级数据结构 sets(集合) 很多新手忽视sets(集合)和tuple(元组)的强大之处 例如,取两个列表交集: def common_elements(list1, list2): common = [] for item1 in list1: if item…
Python的字典的items(), keys(), values()都返回一个list >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' } >>> dict.values() ['b', 2, 'world'] >>> dict.keys() ['a', 1, 'hello'] >>> dict.items() [('a', 'b'), (1, 2), ('hello', 'worl…
一.get方法 dict = {'k1':1,'k2':2} dict.get('k1') 1 dict.get('k2') 2 dict.get('k3') None dict.get('k3','wohaoshuai') wohaoshuai (如果k3不存在那么就设置为wohaoshuai) 二.items dict.items() dict_items([('a', 1), ('b', 2)]) 三.pop dict.pop('k1') dict {'k2':2} 四.update d2…
字典Dict的跟进学习: 一. items()方法的遍历:items()方法把字典中每对key和value组成一个元组,并把这些元组放在列表中返回. dict = {"name" = "柒禾", "age" = 18, "height" = 170.0} for k, v in dict.items(): print("Key=", k "Value=",v) 如果只有一个参数呢? fo…
一.元组: tuple Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组 tup2 = (111, 22, 33, 444, 55, 6, 77 ) for x in (tup2): #遍历 print(x) list2 = [111, 22, 33, 444, 55, 6, 77 ] tup2 = tuple(list2) #将列表转变为元组 二.列表: list 遍历列表: #遍历列表 list1 = [1…