python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中
說(shuō)明
同學(xué)的代碼中遇到一個(gè)數(shù)學(xué)公式牽扯到將生成指定的數(shù)字存儲(chǔ)的一個(gè)列表中,那個(gè)熊孩子忽然懵逼的不會(huì)啦,,,給了博主一個(gè)表現(xiàn)的機(jī)會(huì),,,哈哈哈好嘛,雖然很簡(jiǎn)單但還是記錄一下吧,,,嘿嘿
一 代碼
# coding=utf-8'''@author: jiajiknag程序功能:'''# 方法一lifts = []for n in range(1,13): # lift = 1 +6 * np.sin(np.pi * n/12) lift = 1 + n/12 lifts.append(lift)print(lifts)# 方法二print('------------------------------------')squares = [1 +i/12 for i in range(1,5)]print(squares)
二 結(jié)果
好嘛,,,有沒(méi)有很神奇的節(jié)奏!
補(bǔ)充知識(shí):Python 通過(guò)range初始化list set 等
啥也不說(shuō)了,還是直接看代碼吧!
'''01:range()函數(shù)調(diào)查02:通過(guò)help()函數(shù)調(diào)查range()函數(shù)功能03:Python中的轉(zhuǎn)義字符04:使用start、step、stop的方式嘗試初始化list、tuple、set等05:使用len()獲取list、set、tuple的長(zhǎng)度'''help(range)tempRange = range(1,100,2)print('type(tempRange): ' + str(type(tempRange)))print('tempRange: ' + str(tempRange))tempStr = ''for i in range(5): # 注意 輸出0到4,包括0和4但不包括5. tempStr += (' ' + str(i) + ' ')print('for i in range(5) ' + tempStr) #for i in range(5) 0 1 2 3 4 tempStr = ''for i in range(20,0,-2): tempStr += (' ' + str(i) + ' ')# 注意看輸出不包括0print('for i in range(20,0,-2) ' + tempStr)'''for i in range(20,0,-2) 20 18 16 14 12 10 8 6 4 2 '''tempStr = ''for i in [1,2,3]: tempStr += (' ' + str(i) + ' ')# for i in [1,2,3] 1 2 3 print('for i in [1,2,3] ' + tempStr)tempStr = ''for i in 'Hello world!': tempStr += (' ' + str(i) + ' ')# for i in 'Hello world!' H e l l o w o r l d ! print('for i in 'Hello world!' ' + tempStr)print(list(range(5,10))) # 默認(rèn)步長(zhǎng)1,輸出:[5, 6, 7, 8, 9]不包括10print(list(range(0,10,2))) #輸出:[0, 2, 4, 6, 8]print(list(range(10,0,2))) #輸出:[]print(list(range(10,0,-2))) #輸出:[10, 8, 6, 4, 2]# 嘗試使用start、step、stop的方式嘗試初始化list、tuple、set等# print(list(1,9,1)) # TypeError: list() takes at most 1 argument (3 given)# print(set(1,9,1)) # TypeError: set expected at most 1 arguments, got 3# print(tuple(1,9,1)) # TypeError: tuple() takes at most 1 argument (3 given)tempList = list(range(0,10,1));print('list(range(0,10,1)): ' + str(tempList))tempSet = set(range(0,10,1))print('list(set(0,10,1)): ' + str(tempSet))tempTuple = tuple(range(0,10,1))print('list(tuple(0,10,1)): ' + str(tempTuple))tempDic = {'num':1}print('len(list) :' + str(len(tempList))) # len(list) :10print('len(set) :' + str(len(tempSet))) # len(set) :10print('len(tuple) :' + str(len(tempTuple))) # len(tuple) :10print('len(dic) :' + str(len(tempDic))) # len(dic) :1# list.append [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ’b’]tempList.append(’b’)print('list.append ' + str(tempList))# set.add {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ’a’}tempSet.add(’a’)print('set.add ' + str(tempSet))
以上這篇python實(shí)現(xiàn)將range()函數(shù)生成的數(shù)字存儲(chǔ)在一個(gè)列表中就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)2. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 如何在jsp界面中插入圖片4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. phpstudy apache開(kāi)啟ssi使用詳解6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令10. xml中的空格之完全解說(shuō)
