Python實(shí)現(xiàn)斐波那契數(shù)列得多種寫法總結(jié)

目錄

斐波那契數(shù)列——經(jīng)典例子,永不過時!!!

1.for循環(huán)

def fibonacci1(n):    a, b = 0, 1    for i in range(n):        a, b = b, a+b        print(a)fibonacci1(3)

def fib1(w):    a, b = 1, 1    for i in range(w-1):        a, b = b, a+b    return a???????print(fib1(3))

[^1]剛好得出這個位置得數(shù)

2.while循環(huán)

def fibnaqi2(m):    a, b = 0, 1    i = 0    while i < m:        print(b)        a, b = b, a+b        i += 1fibnaqi2(4)

[^1]剛好得出這個位置得數(shù)

3.使用遞歸

def fib2(q):    if q == 1 or q == 2:        return 1    return fib2(q-1)+fib2(q-2)???????print(fib2(9))

4.遞歸+for循環(huán)

def fibnacci3(p):    lst = []    for i in range(p):        if i == 1 or i == 0:            lst.append(1)        else:            lst.append(lst[i-1]+lst[i-2])    print(lst)fibnacci3(5)

5.遞歸+while循環(huán)

def fibnacci4(k):    lis = []    i = 0    while i<k:        if i == 0 or i == 1:            lis.append(1)        else:            lis.append(lis[i-2]+lis[i-1])        i += 1    print(lis)fibnacci4(6)

6.遞歸+定義函數(shù)+for循環(huán)

def fibnacci5(o):    def fn(i):        if i < 2:            return 1        else:            return (fn(i-2)+fn(i-1))    for i in range(o):        print(fn(i))???????fibnacci5(8)

7.指定列表

def fib3(e):    if e == 1:        return [1]    if e == 2:        return [1, 1]    fibs = [1, 1]    for i in range(2, e):        fibs.append(fibs[-1]+fibs[-2])    return fibsprint(fib3(12))

趣方程求解

題目描述

二次方程式 ax**2 + bx + c = 0 (a、b、c 用戶提供,為實(shí)數(shù),a ≠ 0)

# 導(dǎo)入 cmath(復(fù)雜數(shù)學(xué)運(yùn)算) 模塊import cmatha = float(input('輸入 a: '))b = float(input('輸入 b: '))c = float(input('輸入 c: '))# 計(jì)算d = (b ** 2) - (4 * a * c)# 兩種求解方式sol1 = (-b - cmath.sqrt(d)) / (2 * a)sol2 = (-b + cmath.sqrt(d)) / (2 * a)print('結(jié)果為 {0} 和 {1}'.format(sol1, sol2))

pandas 每日一練

# -*- coding = utf-8 -*-# @Time : 2022/7/26 21:48# @Author : lxw_pro# @File : pandas -8 練習(xí).py# @Software : PyCharmimport pandas as pdimport numpy as npdf = pd.read_excel('text5.xlsx')print(df)print()

程序運(yùn)行結(jié)果如下:

   Unnamed: 0 Unnamed: 0.1  project  ...           test_time       date      time
0           0     00:00:00   Python  ... 2022-06-20 18:30:20 2022-06-20  18:30:20
1           1            1     Java  ... 2022-06-18 19:40:20 2022-06-18  19:40:20
2           2            2        C  ... 2022-06-08 13:33:20 2022-06-08  13:33:20
3           3            3    MySQL  ... 2021-12-23 11:26:20 2021-12-23  11:26:20
4           4            4    Linux  ... 2021-12-20 18:20:20 2021-12-20  18:20:20
5           5            5     Math  ... 2022-07-20 16:30:20 2022-07-20  16:30:20
6           6            6  English  ... 2022-06-23 15:30:20 2022-06-23  15:30:20
7           7            7   Python  ... 2022-07-19 09:30:20 2022-07-19  09:30:20
[8 rows x 7 columns]

41、將test_time列設(shè)置為索引

print(df.set_index('test_time'))???????print()

程序運(yùn)行結(jié)果如下:
                    Unnamed: 0 Unnamed: 0.1  ...       date      time
test_time                                     ...                     
2022-06-20 18:30:20           0     00:00:00  ... 2022-06-20  18:30:20
2022-06-18 19:40:20           1            1  ... 2022-06-18  19:40:20
2022-06-08 13:33:20           2            2  ... 2022-06-08  13:33:20
2021-12-23 11:26:20           3            3  ... 2021-12-23  11:26:20
2021-12-20 18:20:20           4            4  ... 2021-12-20  18:20:20
2022-07-20 16:30:20           5            5  ... 2022-07-20  16:30:20
2022-06-23 15:30:20           6            6  ... 2022-06-23  15:30:20
2022-07-19 09:30:20           7            7  ... 2022-07-19  09:30:20
[8 rows x 6 columns]

42、生成一個和df長度相同得隨機(jī)數(shù)dataframe

df1 = pd.DataFrame(pd.Series(np.random.randint(1, 10, 8)))print(df1)???????print()

程序運(yùn)行結(jié)果如下:

   0
0  1
1  3
2  2
3  7
4  7
5  3
6  5
7  1

43、將上一題生成得dataframe與df合并

df = pd.concat([df, df1], axis=1)print(df)???????print()

程序運(yùn)行結(jié)果如下:

   Unnamed: 0 Unnamed: 0.1  project  ...       date      time  0
0           0     00:00:00   Python  ... 2022-06-20  18:30:20  1
1           1            1     Java  ... 2022-06-18  19:40:20  3
2           2            2        C  ... 2022-06-08  13:33:20  2
3           3            3    MySQL  ... 2021-12-23  11:26:20  7
4           4            4    Linux  ... 2021-12-20  18:20:20  7
5           5            5     Math  ... 2022-07-20  16:30:20  3
6           6            6  English  ... 2022-06-23  15:30:20  5
7           7            7   Python  ... 2022-07-19  09:30:20  1
[8 rows x 8 columns]

44、生成新得一列new為popularity列減去之前生成隨機(jī)數(shù)列

df['new'] = df['popularity'] - df[0]print(df)???????print()

程序運(yùn)行結(jié)果如下:

  Unnamed: 0 Unnamed: 0.1  project  popularity  ...       date      time  0  new
0           0     00:00:00   Python          95  ... 2022-06-20  18:30:20  1   94
1           1            1     Java          92  ... 2022-06-18  19:40:20  3   89
2           2            2        C         145  ... 2022-06-08  13:33:20  2  143
3           3            3    MySQL         141  ... 2021-12-23  11:26:20  7  134
4           4            4    Linux          84  ... 2021-12-20  18:20:20  7   77
5           5            5     Math         148  ... 2022-07-20  16:30:20  3  145
6           6            6  English         146  ... 2022-06-23  15:30:20  5  141
7           7            7   Python         149  ... 2022-07-19  09:30:20  1  148
[8 rows x 9 columns]

45、檢查數(shù)據(jù)中是否含有任何缺失值

jch = df.isnull().values.any()print(jch)    # 運(yùn)行結(jié)果為:False???????print()

46、將popularity列類型轉(zhuǎn)換為浮點(diǎn)數(shù)

fds = df['popularity'].astype(np.float64)print(fds)???????print()

程序運(yùn)行結(jié)果如下:

0     95.0
1     92.0
2    145.0
3    141.0
4     84.0
5    148.0
6    146.0
7    149.0
Name: popularity, dtype: float64

47、計(jì)算popularity大于100得次數(shù)

cs = len(df[df['popularity'] > 100])print(cs)    # 運(yùn)行結(jié)果為:5???????print()

48、查看project列共有幾種學(xué)歷

ckj = df['project'].nunique()print(ckj)    # 運(yùn)行結(jié)果為:7???????print()

49、查看每科出現(xiàn)得次數(shù)

ckc = df.project.value_counts()print(ckc)print()

程序運(yùn)行結(jié)果如下:

Python     2
Java       1
C          1
MySQL      1
Linux      1
Math       1
English    1
Name: project, dtype: int64

50、提取popularity與new列得和大于136得最后3行

df1 = df[['popularity', 'new']]hh = df1.apply(np.sum, axis=1)res = df.iloc[np.where(hh > 136)[0][-3:], :]print(res)

程序運(yùn)行結(jié)果如下:

   Unnamed: 0 Unnamed: 0.1  project  popularity  ...       date      time  0  new
5           5            5     Math         148  ... 2022-07-20  16:30:20  3  145
6           6            6  English         146  ... 2022-06-23  15:30:20  5  141
7           7            7   Python         149  ... 2022-07-19  09:30:20  1  148
[3 rows x 9 columns]

到此這篇關(guān)于Python實(shí)現(xiàn)斐波那契數(shù)列得多種寫法總結(jié)得內(nèi)容就介紹到這了,更多相關(guān)Python斐波那契數(shù)列內(nèi)容請搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評論
更多 網(wǎng)友評論1 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 伊人久久久大香线蕉综合直播| 机机对机机的30分钟免费软件| 在线看中文字幕| 亚洲日韩精品无码一区二区三区 | 风间由美性色一区二区三区| 日本特黄特色特爽大片老鸭| 四虎影视永久费观看在线| √天堂资源地址在线官网| 毛片高清视频在线看免费观看| 国产精品入口麻豆完整版| 久久成人国产精品免费软件| 老子影院午夜伦手机电影| 天天欲色成人综合网站| 亚洲妇熟xxxx妇色黄| 67194成人手机在线| 亚洲AV无码国产精品永久一区| 国产香蕉一区二区三区在线视频| 欧美精品v日韩精品v国产精品| 把女人弄爽大黄a大片片| 午夜一级毛片免费视频| 99re热这里只有精品视频| 偷窥欧美wc经典tv| yy6080一级毛片高清| 亚洲人成网站在线观看青青| 国产一区二区三区在线电影| 天堂√在线中文资源网| 欧美三级黄视频| 精品人妻系列无码天堂| 1000部无遮挡拍拍拍免费视频观看| 久久久久久国产精品无码下载| 亚洲综合色视频在线观看| 国产中文欧美日韩在线| 在线电影中文字幕| 日韩中文字幕视频| 琪琪女色窝窝777777| 日本xxxxbbbb| gogogo高清在线播放| 久久伊人精品一区二区三区 | 蜜臀av免费一区二区三区| 一个人看的www在线观看免费 | 娇妻校花欲乱往事叶子txt下载|