python開發(fā)之for循環(huán)操作實例詳解
更新時間:2015年11月12日 09:54:33 作者:Hongten
這篇文章主要介紹了python開發(fā)之for循環(huán)操作,以實例形式較為詳細的分析了Python中for循環(huán)的具體使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了python開發(fā)之for循環(huán)操作。分享給大家供大家參考,具體如下:
下面是我做的一些學習記錄供大家參考:
#基本的for循環(huán)語句
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中for循環(huán)詳解
- 詳解Python中for循環(huán)的使用
- 詳解Python中for循環(huán)的使用方法
- 講解Python中for循環(huán)下的索引變量的作用域
- Python中for循環(huán)和while循環(huán)的基本使用方法
- Python for循環(huán)中的陷阱詳解
- Python中for循環(huán)控制語句用法實例
- python 用lambda函數(shù)替換for循環(huán)的方法
- python中for循環(huán)輸出列表索引與對應的值方法
- python中關于for循環(huán)的碎碎念
- Python學習筆記之For循環(huán)用法詳解
相關文章
Python使用multiprocessing模塊實現(xiàn)多進程并發(fā)處理大數(shù)據(jù)量的示例代碼
這篇文章主要介紹了Python使用multiprocessing模塊實現(xiàn)多進程并發(fā)處理大數(shù)據(jù)量的示例代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
深入淺析pycharm中 Make available to all projects的含義
這篇文章主要介紹了pycharm中 Make available to all projects的含義,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
python的scrapy框架之Pipeline文件的用法詳解
這篇文章主要介紹了python的scrapy框架之Pipeline文件的用法詳解,Pipeline是一個獨立的模塊,用于處理從Spider中提取的Item對象,實現(xiàn)對數(shù)據(jù)的進一步處理、存儲和清洗等操作,下面將詳細介紹Scrapy框架中Pipeline的用法,需要的朋友可以參考下2023-10-10
Python用HBuilder創(chuàng)建交流社區(qū)APP
這篇文章主要講解Python使用HBuilder創(chuàng)建交流社區(qū)APP,使用HBuilder做一個簡單的社區(qū)瀏覽界面,下面文章附有詳細的代碼,需要的朋友可以參考一下2021-11-11

