python中range函数

本文详细解析了Python中的range()函数用法,包括其参数start、stop和step的意义,以及在for循环中的应用实例。此外,还介绍了在TensorFlow Python 3.6环境下使用range()函数的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数语法

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)

实例

  1. >>>range(10) # 从 0 开始到 10
  2. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. >>> range(1, 11) # 从 1 开始到 11
  4. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  5. >>> range(0, 30, 5) # 步长为 5
  6. [0, 5, 10, 15, 20, 25]
  7. >>> range(0, 10, 3) # 步长为 3
  8. [0, 3, 6, 9]
  9. >>> range(0, -10, -1) # 负数
  10. [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  11. >>> range(0)
  12. []
  13. >>> range(1, 0)
  14. []

以下是 range 在 for 中的使用,循环出runoob 的每个字母:

  1. >>>x = 'runoob'
  2. >>> for i in range(len(x)) :
  3. ... print(x[i])
  4. ...
  5. r
  6. u
  7. n
  8. o
  9. o
  10. b
  11. >>>

 

在tensorflow python 3.6的环境下,range函数中实参必须为int型,否则报错

 

  1. def load_dataset(data_dir, img_size):
  2. """img_files = os.listdir(data_dir)
  3. test_size = int(len(img_files)*0.2)
  4. test_indices = random.sample(range(len(img_files)),test_size)
  5. for i in range(len(img_files)):
  6. #img = scipy.misc.imread(data_dir+img_files[i])
  7. if i in test_indices:
  8. test_set.append(data_dir+"/"+img_files[i])
  9. else:
  10. train_set.append(data_dir+"/"+img_files[i])
  11. return"""
  12. global train_set
  13. global test_set
  14. imgs = []
  15. img_files = os.listdir(data_dir)
  16. for img in img_files:
  17. try:
  18. tmp= scipy.misc.imread(data_dir+"/"+img)
  19. x,y,z = tmp.shape
  20. coords_x = x // img_size
  21. coords_y = y // img_size
  22. # coords_y = y / img_size
  23. # coords_x = x / img_size
  24. #print (coords_x)
  25. coords = [ (q,r) for q in range(coords_x) for r in range(coords_y) ]
  26. for coord in coords:
  27. imgs.append((data_dir+"/"+img,coord))
  28. except:
  29. print ("oops")
  30. test_size = min(10,int( len(imgs)*0.2))
  31. random.shuffle(imgs)
  32. test_set = imgs[:test_size]
  33. train_set = imgs[test_size:][:200]
  34. return
  1. def get_batch(batch_size,original_size,shrunk_size):
  2. global batch_index
  3. """img_indices = random.sample(range(len(train_set)),batch_size)
  4. for i in range(len(img_indices)):
  5. index = img_indices[i]
  6. img = scipy.misc.imread(train_set[index])
  7. if img.shape:
  8. img = crop_center(img,original_size,original_size)
  9. x_img = scipy.misc.imresize(img,(shrunk_size,shrunk_size))
  10. x.append(x_img)
  11. y.append(img)"""
  12. max_counter = len(train_set)/batch_size
  13. counter = batch_index % max_counter
  14. # counter = tf.to_int32(batch_index % max_counter)
  15. window = [x for x in range(int(counter*batch_size),int((counter+1)*batch_size))]
  16. # window = [x for x in range(tf.to_int32(counter*batch_size),tf.to_int32((counter+1)*batch_size))]
  17. # window = [x for x in np.arange((counter*batch_size),((counter+1)*batch_size))]
  18. # a1=tf.cast(counter*batch_size,tf.int32)
  19. # a2=tf.cast((counter+1)*batch_size,tf.int32)
  20. # window = [x for x in range(a1,a2)]
  21. # window = [x for x in np.arange(a1,a2)]
  22. # win2 = tf.cast(window,tf.int32)
  23. # win2 = tf.to_int32(window)
  24. # win2 = tf.to_int64(window)
  25. imgs = [train_set[q] for q in window]
  26. 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]
  27. 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]
  28. batch_index = (batch_index+1)%max_counter
  29. return x,y

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值