Python(day5)

汉诺塔

def hanoi(n,x,y,z):	#n=层数,第二个数为初始层,第三个数为辅助层,第四个数为目标层
	if n == 1:
		print(x,'-->',z)	#直接将初始层上的n盘移动到目标层
	else:
		hanoi(n-1,x,z,y)	#此时x为初始层,z为辅助层,y为目标层,将x上的前n-1个盘移动到y上
		print(x,'-->',z)	#将x上的n盘移动到z上
		hanoi(n-1,y,x,z)	#此时y为初始层,x为辅助层,z为目标层,将x上的前n-1个盘移动到y上
n = (int(input('请输入汉诺塔的层数:'))
hanoi(n,'X','Y','Z')

-

字典{ }、元组( )、列表[ ]

-

字典

不是序列类型,是映射类型
-
创建和访问字典:标志性:{ },由一个键和一个值组成
Eg:

>>>dict1 = {1:'one',2:'two',3:'three'}	#1,2,3为键,one,two,three,为值
>>>print('1的英文字母是:',dict1[1])	#dict1[1]不是索引值,是键的值,可以是整型,变量,字符串类型
>>>1的英文字母是:one
>>>dict1[2]
>>>'two'
>>>dict1
>>>{1:'one',2:'two',3:'three'}	#会自动排序

创建空字典:

>>>dict2{}
>>>dict2
>>>{ }

修改:

>>>dict1[2]= 'owt'
>>>dict1
>>>{1:'one',2:'owt',3:'three'}

添加:

>>>dict1['4'] = 'four'
>>>dict1
>>>{1:'one',2:'owt',3:'three',4:'four'}

-

fromkeys(s[,v])

s是键值,v是对应的值(默认是None)
返回一个新的字典

>>>dict1 = {}
>>>dict1.fromkeys({1,2,3)}
>>>{1:None,2:None,3:None}
>>>dict1.fromkeys{(1,2,3),'Number''}	#对应的值只能一个参数
>>>{1:'Number',2:'Number,3:'Number'}
#错误:
>>>dict1.fromkeys{(1,2,3),('one','two','three')}
>>>{1:('one','two','three'),2:('one','two','three'),3:('one','two','three')}

若要批量修改:

#直接创造新的字典
>>>dit1.fromkeys{(1,3),'数字'}
>>>{1:'数字',3:'数字'}

-

访问字典的方法:keys(),values(),item()

keys():返回字典的键
values():返回字典键的值
item():返回项

keys():

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>for eachkey in dict1.keys():
	print(eacheky)
	
打印结果	 #打印键
>>>	0
	1	
	2

-
values():

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>for eachvalues in dict1.values:
	print(eachvalue)

打印结果	#打印键的值
>>>	three
	three
	three

-
items():

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>for eachitem in dict1.items():
	print(eachitem)

打印结果	#打印项
>>>	(0:'three')
	(1:'three')
	(2:'three')

如果打印字典里没有的键,会报错

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>print(dict1[4])
>>>#报错

-

get()

试图访问字典中不存在的键时,会打印None

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>dict1.get(4)
>>>print(dict1.get(4))
>>>None

设置值:

>>>dict1.get(4,'four')
>>>'four'
>>>dict1.get(1,'four')	#如果是已存在的键,则会打印原来的值,不会改变
>>>three

-
判断键是否在字典中,可通过in,not in方法

>>>1 in dict1
>>>True
>>>5 in dict1
>>>False

-

清空字典:

clear()
用于删除字典内所有元素。

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>dict1.clear()
>>>dict1
>>>{}

或:

>>>dict1 = dict1.fromkeys(range(3),'three')
>>>dict1
>>>{0:'three',1:'three',2:'three'}
>>>dict1 = {}	#不严谨,若将dict1赋予其他字典,其他字典会保留值

区别:

#clear()
>>>a = {1:'one'}
>>>b = a
>>>b
>>>{1:'one'}
>>>a.clear()
>>>a
>>>{}
>>>b
>>>{}
#直接等于空字典
>>>a = {1:'one'}
>>>b = a
>>>b
>>>{1:'one'}
>>>a = {}
>>>a
>>>{}
>>>b
>>>{1:'one'}

-

copy()

返回一个字典的浅拷贝(父不变,子变)

>>>a = {1:'one',2:'two'}
>>>b = a.copy()
>>>c = a
>>>b
>>>{1:'one',2:'two'}
>>>c
>>>{1:'one',2:'two'}	#字典一样
>>>id(a)
>>>1927837913520
>>>id(b)
>>>1927837917328
>>>id(c)
>>>1927837913520	#a、c地址相同,b不相同

-

pop()

用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

>>>a = {1:'one',2:'two,3:'three'}
>>>a.pop(2)
>>>two
>>>a
>>>{1:'one',3:'three'}

-

popitem()

随机返回并删除字典中的一对键和值(一般删除末尾对)。
如果字典已经为空,却调用了此方法,就报出KeyError异常。

>>>a = {1:'one',2:'two,3:'three'}
>>>a.popitem()
>>>(1,'one')

-

setdefault()

字典中找不到对应的键时会自动添加

>>>a = {1:'one'}
>>>a.setdefault(2)
>>>a
>>>{1:'one',2:None}
>>>a.setdefault(3:'three')
>>>a
>>>{1:'one',2:None,3:'three'}

-

update()

通过字典映射关系来更新另一个字典

>>>a = {1:'one',2:'None,3:'three'}
>>>b = {2:'two'}
>>>a.update(b)
>>>a
>>>{1:'one',2:two,3:'three'}

-

集合

在python3里,若用{}括起来的元素没有体现映射关系,则是一个集合;若体现出映射关系,则是字典。
-
元素具有唯一性

>>>num = {0,1,2,3,4,4,5,6,7,4,3,1}
>>>num
>>>{0,1,2,3,4,5,6,7}	#重复的元素会被剔除

-
集合是无序的,不能试图索引集合中的某一元素

>>>num = {0,1,2,3,4,5}
>>>num[2]
>>>#报错,不支持索引

-

创建集合

  1. 直接把一堆元素用花括号括起来
  2. 使用set()工厂函数

使用set():

>>>set1 = set([1,2,3,4])
>>>set1
>>>{1,2,3,4}

-

append()

在列表末尾添加新的对象

-

若不通过集合,来剔除重复的数的方法:

>>> num = {0,1,2,3,4,5,4,3,2,6}
>>>temp = []
>>>for each in num:			#在num中
	if each not in temp:		#如果num中的元素不在temp里
		temp.append(each)	#将其元素添加到temp里
>>>temp					#输出temp
>>>{0,1,2,3,4,5,6}		

若通过集合方式:

>>> num = {0,1,2,3,4,5,4,3,2,6}
>>>temp = list(set(num))
>>>temp
>>>{0,1,2,3,4,5,6}

-

访问集合中的值

  1. 使用for把集合的元素一个个读取出来
  2. 通过in,not in 判断元素是否在集合中
>>> num = {0,1,2,3,4,5}
>>>1 in num
>>>True
>>>'1' in num
>>>False

-

add()

添加

>>> num = {0,1,2,3}
>>>num.add(4)
>>>num
>>>{0,1,2,3,4}

-

remove()

移出

>>> num = {0,1,2,3,4,5}
>>>num.remove(4)
>>>num
>>>{0,1,2,3,5}

-

不可变集合:forzenset()

返回一个冻结的集合,冻结后集合不能再添加或删除任何元素。

>>> num = forzen([1,2,3,4])
>>>num.add(6)
>>>报错		#'forzenset()'没有add()等内置方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值