range在python中的意思是什么

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)range在python中的意思是什么,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)新巴爾虎右,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792

python range() 函數(shù)可創(chuàng)建一個(gè)整數(shù)列表,一般用在 for 循環(huán)中。

函數(shù)語(yǔ)法

range(start, stop[, step])

參數(shù)說(shuō)明:

start: 計(jì)數(shù)從 start 開(kāi)始。默認(rèn)是從 0 開(kāi)始。例如range(5)等價(jià)于range(0, 5);

stop: 計(jì)數(shù)到 stop 結(jié)束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]沒(méi)有5

step:步長(zhǎng),默認(rèn)為1。例如:range(0, 5) 等價(jià)于 range(0, 5, 1)

實(shí)例

>>>range(10)        # 從 0 開(kāi)始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)     # 從 1 開(kāi)始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)  # 步長(zhǎng)為 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)  # 步長(zhǎng)為 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 負(fù)數(shù)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
以下是 range 在 for 中的使用,循環(huán)出runoob 的每個(gè)字母:
 
>>>x = 'runoob'
>>> for i in range(len(x)) :
...     print(x[i])
... 
r
u
n
o
o
b
>>>
 
在tensorflow python 3.6的環(huán)境下,range函數(shù)中實(shí)參必須為int型,否則報(bào)錯(cuò)
 
def load_dataset(data_dir, img_size):
"""img_files = os.listdir(data_dir)
test_size = int(len(img_files)*0.2)
test_indices = random.sample(range(len(img_files)),test_size)
for i in range(len(img_files)):
#img = scipy.misc.imread(data_dir+img_files[i])
if i in test_indices:
test_set.append(data_dir+"/"+img_files[i])
else:
train_set.append(data_dir+"/"+img_files[i])
return"""
global train_set
global test_set
imgs = [] 
img_files = os.listdir(data_dir)
for img in img_files:
try:
tmp= scipy.misc.imread(data_dir+"/"+img)
x,y,z = tmp.shape
coords_x = x // img_size
coords_y = y // img_size
           
#coords_y = y / img_size
#                       coords_x = x / img_size
            
            #print (coords_x)
coords = [ (q,r) for q in range(coords_x) for r in range(coords_y) ]
for coord in coords:
imgs.append((data_dir+"/"+img,coord))
except:
print ("oops")
test_size = min(10,int( len(imgs)*0.2))
random.shuffle(imgs)
test_set = imgs[:test_size]
train_set = imgs[test_size:][:200]
return
def get_batch(batch_size,original_size,shrunk_size):
global batch_index
"""img_indices = random.sample(range(len(train_set)),batch_size)
for i in range(len(img_indices)):
index = img_indices[i]
img = scipy.misc.imread(train_set[index])
if img.shape:
img = crop_center(img,original_size,original_size)
x_img = scipy.misc.imresize(img,(shrunk_size,shrunk_size))
x.append(x_img)
y.append(img)"""
max_counter = len(train_set)/batch_size   
counter = batch_index % max_counter
#counter = tf.to_int32(batch_index % max_counter)    
window = [x for x in range(int(counter*batch_size),int((counter+1)*batch_size))]
 
#window = [x for x in range(tf.to_int32(counter*batch_size),tf.to_int32((counter+1)*batch_size))]
#window = [x for x in np.arange((counter*batch_size),((counter+1)*batch_size))]
#a1=tf.cast(counter*batch_size,tf.int32)
#a2=tf.cast((counter+1)*batch_size,tf.int32)
#window = [x for x in range(a1,a2)]
#window = [x for x in np.arange(a1,a2)]
#win2 = tf.cast(window,tf.int32)
#win2 = tf.to_int32(window)
#win2 = tf.to_int64(window)
 
imgs = [train_set[q] for q in window]
x = [scipy.misc.imresize(get_image(q,original_size),(shrunk_size,shrunk_size)) for q in imgs]#scipy.misc.imread(q[0])[q[1][0]*original_size:(q[1][0]+1)*original_size,q[1][1]*original_size:(q[1][1]+1)*original_size].resize(shrunk_size,shrunk_size) for q in imgs]
y = [get_image(q,original_size) for q in imgs]#scipy.misc.imread(q[0])[q[1][0]*original_size:(q[1][0]+1)*original_size,q[1][1]*original_size:(q[1][1]+1)*original_size] for q in imgs]
batch_index = (batch_index+1)%max_counter

上述就是小編為大家分享的range在python中的意思是什么了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:range在python中的意思是什么
轉(zhuǎn)載源于:http://muchs.cn/article44/jcjeee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作外貿(mào)建站、虛擬主機(jī)Google、網(wǎng)站內(nèi)鏈、App開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司