网站首页 > 基础教程 正文
python作业:
1、斐波那契数列:
方法1:
list=[1,1]
for i in range(10):
list.append(list[-1]+list[-2])
print(list)
方法2
list=[]
for i in range(10):
if i==0 or i==1:
list.append(1)
else:
list.append(list[-1]+list[-2])
print(list)
方法3
def feibo(n):
i=1
j=1
list1=[i,j]
for _ in range(2,n):
k=i+j
list1.append(k)
i=j
j=k
return list1
print(feibo(9))
方法4:
x,y=0,1
max=int(input('斐波那契数列项数:'))
for i in range(max):
print(y,end=',')
x,y=y,x+y #更新x,y值,交换两个变量的值,执行中变量x 将变为原来变量b的值,而变量b将变为原来变量a+b的值
2、将两个列表合成一个字典?
方法1;
a=['a','b','c','d','e']
b=[1,2,3,4,5]
c=zip(a,b)
d = dict(c)
print(d)
print(type(d)
方法2:
a =['a','b','c','d','e']
b =[1,2,3,4,5]
dict ={}
i = 0
while i < len(a) :
dict[a[i]] = b[i]
i +=1
print(dict)
方法3:
a =['a','b','c','d','e']
b =[1,2,3,4,5]
dic={key : value for key,value in zip(a,b)}
print(dic)
(三)求出1-10 的奇数
a=[1,2,3,4,5,6,7,8,9,10]
b=[]
for i in a:
if i%2==1:
b.append(i)
print(b)
(四)将一个url取出键值,并生成字典
方法1:
url = 'http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0'
str1 = url.split('?')[1]
print(str1)
str2 = str1.split('&')
print(str2)
dic = {}
for i in str2:
key,value=i.split('=')
dic[key]=value
print(dic)
print(dic.get("page_size"))
s = 'http://ip:port/extername/get_account_trade_record.json?page_size=20&page_index=1&user_id=203317&trade_type=0'
a = s.split('?')[1]
dic = {}
for i in a.split('&'):
key, value = i.split('=')
dic[key] = value
print(dic)
猜你喜欢
- 2024-10-31 Python办公神器:教你如何快速分拆、删页、合并PDF文件
- 2024-10-31 Python3中的字符串操作 python3 字符串操作
- 2024-10-31 学好了Python,我们就可以玩转字符串算法了
- 2024-10-31 玩转Python—字符串使用教程 python字符串常用方法
- 2024-10-31 你会在 Python 中使用字符串吗? python字符串怎么用
- 2024-10-31 Python3中可能不会用到的10个功能!但是能让你的代码更简洁直观
- 2024-10-31 手把手教你写爬虫 |Python 采集大众点评数据采集实战
- 2024-10-31 「python杂谈」使用多个分隔符分隔字符串
- 2024-10-31 如何用Python+OpenCV处理图像色彩?终于有人讲明白了
- 2024-10-31 字符串基本操作2-2-Python3零基础入门
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)