Python数组遍历的简单实现方法

Python数组遍历的简单实现方法_第1张图片

这篇文章主要介绍了Python数组遍历的简单实现方法,结合实例总结分析了Python针对数组的元素,索引常用遍历技巧,需要的朋友可以参考下

本文实例总结了Python数组遍历的简单实现方法。分享给大家供大家参考,具体如下:

1

2

3

4

>>> os.__file__.split('\\')

['E:', 'Python', 'Python25', 'lib', 'os.pyc']

>>> os.path.split(os.__file__)

('E:\\Python\\Python25\\lib', 'os.pyc')

1

2

3

4

var myArr:Array = new Array("one", "two", "three");

var myStr:String = myArr.join(" and ");

trace(myArr); // one,two,three

trace(myStr); // one and two and three

1) 将每个id的属性值插入数组,Aarry.join(",") 成字符串,保存到一个txt里,或者数据库表里。(txt首选)

2) python直接解析 split(",") 成数组,索引+1是文件名,值是数据库id,读取数据库,
用enumerate函数,遍历数组,生成xml。(首选)

在Python中,我们习惯这样遍历:

1

2

for item in sequence:

process(item)

这样遍历取不到item的序号i,所有就有了下面的遍历方法:

1

2

for index in range(len(sequence)):

process(sequence[index])

其实,如果你了解内置的enumerate函数,还可以这样写:

1

2

for index, item in enumerate(sequence):

process(index, item)

希望本文所述对大家Python程序设计有所帮助。

来源:https://www.weidianyuedu.com

你可能感兴趣的:(Python,编程语言,python,开发语言)