python遍历 truple list dictionary的几种方法总结

yipeiwu_com7年前Python基础

实例如下:

def TestDic1():
  dict2 ={'aa':222,11:222}
  for val in dict2:
    print val

def TestDic2():
  dict2 ={'aa':222,11:222}
  for (key,val) in dict2.items():
    print key,":",val   

def TestList1():
  list=[1,2,3,4,5,3,2,'ada','fs3']
  for i in range(len(list)):
    print list[i]

def TestDic3():
  dict2 ={'aa':222,11:222}
  print "###########iteritems#################"
  for k,v in dict2.iteritems():
    print "dict[%s]=" % k,v

def TestDic4():
  dict2 ={'aa':222,11:222}
  print "###########iterkeys,itervalues#######"
  for k,v in zip(dict2.iterkeys(),dict2.itervalues()):
    print "dict[%s]=" % k,v    

def TestList2():
  list=[1,2,3,4,5,3,2,'ada','fs3']
  for i in list:
    print i

以上这篇python遍历 truple list dictionary的几种方法总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python数据可视化之画图

Python数据可视化之画图

安装数据可视化模块matplotlib:pip install matplotlib 导入matplotlib模块下的pyplot 1 折线图 from matplotlib imp...

python破解zip加密文件的方法

python破解zip加密文件的方法

首先我们先来桌面创建一个文件 我们创建了一个名为q的txt文件然后我们将它压缩,压缩的时候记得设置上密码 我这边将密码设置为123456, 接下来我们打开我们的编写工具,开始...

详解python中的index函数用法

1.函数的创建 def fun():        #定义 print('hellow') #函数的执行代码 retrun 1 #返回值 fun()...

Python中字符串对齐方法介绍

目的   实现字符串的左对齐,右对齐,居中对齐。 方法   字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符 复制代码...

python利用dlib获取人脸的68个landmark

(1) 单人脸情况 import cv2 import dlib path = "1.jpg" img = cv2.imread(path) gray = cv2.cvtColo...