[Python]学习笔记之列表

简介:
# coding=utf-8

#一 初始列表
fruits = ["apple","banama","peach"]
print fruits
print fruits[0]
#列表长度
print len(fruits)
#尾部追加一个数据项
fruits.append("watermelon")
print fruits
#尾部删除一个数据项
fruits.pop()
print fruits
#尾部追加一个数据项集合
fruits.extend(['Pear ','Grape'])
print fruits
#在列表中找到并删除一个特定的数据项
fruits.remove("apple")
print fruits
#在某个特定位置前面添加一个数据项
fruits.insert(0,"apple")
fruits.insert(3,"apple")
print fruits

#二 混合数据
#python列表可以包含混合类型的数据,在同一个列表中同时存在字符串和数字以及其它类型的数据都是可以的
fruits = ["apple",20,"banama",10,"Pear",21]
print fruits

#三 迭代
fruits = ["apple","banama","Pear"]
for fruit in fruits:
   print "水果:"+fruit

#四 列表中存储列表
fruits = ["Watermelon",["apple",10],["banama",21],["Pear",33]]
#每次处理列表的一项时你都要查看一下这一项是不是列表
#它允许检查某个特定标识符是否包含某个特定的数据类型
print isinstance(fruits,list)
for fruit in fruits:
   if isinstance(fruit,list):
      for item in fruit:
	     print item
   else:
      print fruit
#五 处理多层嵌套列表
#"列表中的列表中的列表中的...列表",用for循环,你的代码会变的过于复杂,(for循环中的for循环中的...for循环)。我们应该用函数来代替反复重复的代码。
def listNest(lists):
   for item in lists:
      #如果item是个列表
      if isinstance(item,list):
	     #递归调用
         listNest(item)
      else:
	     print item
print "五 处理多层嵌套列表"
listNest(fruits)






目录
相关文章
|
2天前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
2天前
|
索引 Python
Python中的列表、元组和字典各具特色
Python中的列表、元组和字典各具特色:列表是可变的,元组不可变,字典亦可变;列表和元组有序,字典无序(但在Python 3.7+保持插入顺序);元素类型上,列表和元组元素任意,字典需键不可变;列表用方括号[],元组用圆括号(),字典用大括号{}表示。列表不适合作字典键,元组可以。选择数据结构应依据实际需求。
7 2
|
6天前
|
BI Python
深入浅出:讲解Python中的列表推导式
深入浅出:讲解Python中的列表推导式
|
6天前
|
监控 PHP Python
1688快速获取整店铺列表 采集接口php Python
在电子商务的浪潮中,1688平台作为中国领先的批发交易平台,为广大商家提供了一个展示和销售商品的广阔舞台;然而,要在众多店铺中脱颖而出,快速获取商品列表并进行有效营销是关键。
|
6天前
|
算法 Python
Python中不使用sort对列表排序的技术
Python中不使用sort对列表排序的技术
17 1
|
6天前
|
Python
【Python 基础】列表(list)和元组(tuple)有什么区别?
【5月更文挑战第6天】【Python 基础】列表(list)和元组(tuple)有什么区别?
|
6天前
|
算法 Python
从原始边列表到邻接矩阵:使用Python构建图的表示
从原始边列表到邻接矩阵:使用Python构建图的表示
9 0
|
6天前
|
机器学习/深度学习 存储 数据挖掘
Python中遍历并修改列表的综合指南
Python中遍历并修改列表的综合指南
15 2
|
7天前
|
机器学习/深度学习 自然语言处理 Python
python分词列表转化成词向量
python分词列表转化成词向量
12 1
|
7天前
|
算法 数据处理 Python
Python技术分享:如何将数据列表中的空值补0
Python技术分享:如何将数据列表中的空值补0
14 1