#encoding=utf-8
#列表可以存放各种类型的数据
#跟C#不一样,不能用add
List=[2]
#添加元素,追加
List.append("hello")#[2, 'hello'] #进栈
#insert str.insert(位置,要添加的内容)
List.insert(1,'HongKong')#[2, 'HongKong', 'hello']
#+可以将两个列表加起来,不可以把一个字符+到列表中
mList=['33','44','python','deld']
addList=List+mList#[2, 'HongKong', 'hello', '33', '44'])
#extend 添加列表
List.extend(mList)#List :[2, 'HongKong', 'hello', '33', '44']


#删除列表中的元素
pop=List.pop()#删除列表中的最后一个元素,出栈
removeList=List.remove('HongKong')#删除指定的元素,从左到右,删除匹配的第一个
#列表的切片后产生的仍然是列表
sList=List[1:3] #['hello', '33'])
del List[0] #删除下标指定的元素
del List[1:2]#删除下标指定的范围 'hello', '44', 'python']

#改变列表中的元素
List[0]="Change the list"
#查看列表中的元素
if "python" in List:
print('Including python')
if 'hello' not in List:
print('Apend the hello')
print(List,pop,sList)


列表中的append() extend()的区别应用

#append and extend
List1=[1,2,3,4]
List2=[300,400,500,600]
List1.extend(List2) #List1:[1, 2, 3, 4, 300, 400, 500, 600]
List1.append(List2) #[1, 2, 3, 4, 300, 400, 500, 600, [300, 400, 500, 600]]
#区别;extend()后面的参数只允许是一个列表,相当于迭代器,将后面的列表中的元素一个个添加到前面的列表中
# append() 后面的参数可以是列表或者单个元素,作为一个整体添加到前面的列表中


注意:
list1=[1,2,3]
list2=[2,3,4]
list1=list1.append(list2) #在执行append(),已经将结果添加到list1中去,而这个过程是没有返回值的,无任何值赋值给list1,所有结果为None


字典

列表中改变元素时,通过下标来进行修改,但是下标如果会发生改变,则修改元素就不会太确定。

#encoding=utf-8
#字典的学习
#创建字典
#infor={键:值,键:值,键:值}
info={'name':'li','age':15,'hobby':'run'}
print("%s%s%s"%(info['name'],info['age'],info['hobby']))
#列表中可以有字典,字典可以作为一个元素存在列表中
#字典里面的元素是没有顺序的,因为通过key查找,不需要考虑顺序
infoDic={'1':100,'2':200}
infoList=[info,infoDic]#[{'hobby': 'run', 'age': 15, 'name': 'li'}, {'1': 100, '2': 200}]
for temp in infoList:
print(temp)
#{'hobby': 'run', 'age': 15, 'name': 'li'}
#{'1': 100, '2': 200}

nameList=[{'name':1,'age':100},{'name':1,'age':100}]
for name in nameList:
print(name['name']) #打印出列表中每个字典的name对应的值


#在字典中增加元素
info['add']='this' #在字典中添加了一个新的键值对

info['age']=25 #字典中键已经存在,则改变其对于的值

del info['add'] #删除对应的键,若键不存在,则会产生异常
value=info['age'] #查看键对应的值,若键不存在,则会产生异常,需要先判断一下键是否存在

getValue=info.get('age') #查看键对应的值,若键不存在,无返回值,并且不会出异常

print(info,getValue)

字典的遍历:

#encoding=utf-8
#keys values
dic={1:100,2:200,3:300,4:400,5:500}
keys=dic.keys()#获得字典中的所有键的集合,为一个列表;[1, 2, 3, 4, 5]
values=dic.values()#获得字典中所有的值的集合,为一个列表[100, 200, 300, 400, 500]
#以上两种方式在Python2中得到的是列表,但在Python3中得到的是对象dic_keys(1,2,3,4,5)
for temp in dic.keys():
print(temp) #打印所有的键的一种方式
for temp in dic.values():
print(keys) #打印所有值的一种方式
for temp in dic.items(): #每次取出一个元祖
print()

c=[100,200]
a,b=c #这里等号后面列表中有两个变量,等号前面有两个变量,会一一的赋值对应,相当于拆包a=100 b=200
#由此我们可以考虑这样遍历字典
for A,B in dic.items():
print("key is %s,value is %s"%(A,B))
'''
key is 1,value is 100
key is 2,value is 200
key is 3,value is 300
key is 4,value is 400
key is 5,value is 500
'''
print("%d%d"%(a,b))


for-else的应用

#encoding=utf-8
#for ... else for循环中的遍历后执行else操作
num=['or','no','but','and','hee','look']
for temp in num:
print(temp)
else:
print('print the list over!')#这句代码一定会执行
'''
or
no
but
and
hee
look
print the list over!
'''
#for循环中加入了break
for temp in num:
print(temp)
break
else:
print('break or not') #若执行了for,则不会执行else;若没有执行for循环,则会执行else


更多相关文章

  1. Python登录并获取CSDN博客所有文章列表
  2. 在读取和评估文件列表时加速Python eval。
  3. python排序列表与铸造
  4. Python ElementTree“找不到元素”异常
  5. python_列表_循环遍历
  6. Python---列表生成式
  7. Linux服务列表(CentOS)
  8. 将现有数组中的所有元素传递给xargs
  9. Django查询优化:根据多对一到多对多查找对象列表

随机推荐

  1. Android常用的网路框架
  2. android 数据持久化简述
  3. 【Android 初学】11、关于Android当中的
  4. Android IPC 通讯机制源码分析
  5. android关机重启流程代码
  6. Rexsee API介绍:Android屏幕锁定Keyguard
  7. Android获取手机信号强度/信号格数
  8. Android NDK入门之Hello Jni
  9. 基于MQTT实现Android消息推送(Push…
  10. Android 默认壁纸 简单分析