登录  /  注册

range在python中是什么意思

藏色散人
发布: 2019-06-29 11:15:07
原创
31439人浏览过

range在python中是什么意思

python range() 函数可创建一个整数列表,一般用在 for 循环中。

函数语法

range(start, stop[, step])
登录后复制

参数说明:

start: 计数从 start 开始。默认是从 0 开始。例如range(5)等价于range(0, 5);

stop: 计数到 stop 结束,但不包括 stop。例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5

step:步长,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)

实例

>>>range(10)        # 从 0 开始到 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)     # 从 1 开始到 11
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)  # 步长为 5
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)  # 步长为 3
[0, 3, 6, 9]
>>> range(0, -10, -1) # 负数
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
以下是 range 在 for 中的使用,循环出runoob 的每个字母:
 
>>>x = 'runoob'
>>> for i in range(len(x)) :
...     print(x[i])
... 
r
u
n
o
o
b
>>>
 
在tensorflow python 3.6的环境下,range函数中实参必须为int型,否则报错
 
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
登录后复制

相关推荐:《Python教程

以上就是range在python中是什么意思的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号