本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下:
下面是我做的一些学习记录供大家参考:
#基本的for循环语句 test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"] #打印列表的长度 print(len(test_list)) #遍历列表 for i in test_list: print(i) test_str = "hello,i'm hongten" print('打印字符串:' + test_str) #遍历一个字符串 print('遍历一个字符串') for i in test_str: print(i) test_tuple = [("a",1),("b",2),("c",3),("d",4)] print(test_tuple) #遍历一个元组 print('遍历一个元组') for (i,j) in test_tuple: print(i,j) test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'} #字典迭代器 for key in test_dict: print(key + ':' + test_dict[key]) L1 = [1,3,5,7] L2 = [2,4,6,8] #使用zip将两个列表合并 print(zip(L1,L2)) for (i,j) in zip(L1,L2): print(i,j) print('#######################################################') L3 = L2[:] L3.remove(8) print('L1,L3列表为:') print(L1) print(L3) for (i,j) in zip(L1,L3): print(i,j) #可以看出来当长度不一的时候,多余的被忽略 test_keys = ['name','age','gender','weight','hight'] test_values = ['Hongten','20','M','55','170'] #使用zip来构造一个字典 print('字典中的keys:') print(test_keys) print('字典中的key对应的value:') print(test_values) print('构造字典后') test_dic = dict(zip(test_keys,test_values)) for key in test_dic: print( key + ':' + test_dic[key])
运行效果:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 9 2 Jone 3 6 7 hongten hanyuan good Tom 打印字符串:hello,i'm hongten 遍历一个字符串 h e l l o , i ' m h o n g t e n [('a', 1), ('b', 2), ('c', 3), ('d', 4)] 遍历一个元组 ('a', 1) ('b', 2) ('c', 3) ('d', 4) gender:M age:20 name:hongten sports:足球,乒乓球,游泳 [(1, 2), (3, 4), (5, 6), (7, 8)] (1, 2) (3, 4) (5, 6) (7, 8) ####################################################### L1,L3列表为: [1, 3, 5, 7] [2, 4, 6] (1, 2) (3, 4) (5, 6) 字典中的keys: ['name', 'age', 'gender', 'weight', 'hight'] 字典中的key对应的value: ['Hongten', '20', 'M', '55', '170'] 构造字典后 gender:M age:20 name:Hongten weight:55 hight:170 >>>
希望本文所述对大家Python程序设计有所帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
