python實現將range()函式生成的數字儲存在一個列表中
阿新 • • 發佈:2020-04-03
說明
同學的程式碼中遇到一個數學公式牽扯到將生成指定的數字儲存的一個列表中,那個熊孩子忽然懵逼的不會啦,,,給了博主一個表現的機會,,,哈哈哈好嘛,雖然很簡單但還是記錄一下吧,,,嘿嘿
一 程式碼
# 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)
二 結果
好嘛,,,有沒有很神奇的節奏!
補充知識:Python 通過range初始化list set 等
啥也不說了,還是直接看程式碼吧!
""" 01:range()函式調查 02:通過help()函式調查range()函式功能 03:Python中的轉義字元 04:使用start、step、stop的方式嘗試初始化list、tuple、set等 05:使用len()獲取list、set、tuple的長度 """ 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,-2): tempStr += (" " + str(i) + " ") # 注意看輸出不包括0 print("for i in range(20,-2) " + tempStr) """ for i in range(20,-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,3] 1 2 3 print("for i in [1,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))) # 預設步長1,輸出:[5,6,7,8,9]不包括10 print(list(range(0,10,2))) #輸出:[0,4,8] print(list(range(10,2))) #輸出:[] print(list(range(10,-2))) #輸出:[10,2] # 嘗試使用start、step、stop的方式嘗試初始化list、tuple、set等 # print(list(1,9,1)) # TypeError: list() takes at most 1 argument (3 given) # print(set(1,1)) # TypeError: set expected at most 1 arguments,got 3 # print(tuple(1,1)) # TypeError: tuple() takes at most 1 argument (3 given) tempList = list(range(0,1)); print("list(range(0,1)): " + str(tempList)) tempSet = set(range(0,1)) print("list(set(0,1)): " + str(tempSet)) tempTuple = tuple(range(0,1)) print("list(tuple(0,1)): " + str(tempTuple)) tempDic = {"num":1} print("len(list) :" + str(len(tempList))) # len(list) :10 print("len(set) :" + str(len(tempSet))) # len(set) :10 print("len(tuple) :" + str(len(tempTuple))) # len(tuple) :10 print("len(dic) :" + str(len(tempDic))) # len(dic) :1 # list.append [0,1,3,5,'b'] tempList.append('b') print("list.append " + str(tempList)) # set.add {0,'a'} tempSet.add('a') print("set.add " + str(tempSet))
以上這篇python實現將range()函式生成的數字儲存在一個列表中就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。