Python列表和字典的学习

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一枚努力的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值