鱼C论坛

 找回密码
 立即注册
查看: 979|回复: 6

[已解决]循环赋值时候out of range

[复制链接]
发表于 2018-5-9 08:42:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我的思路是,建立一个函数,输入总长和极限长度,然后: temp =( 总长 / 极限长度)   向上取整,然后再  Panel = (总长 / temp)
最后调用方法的时候,出错了,就是说循环赋值的时候,out of range.
请问怎么写才对?



import math
def Panel_Avg(Len, Panel_Len ):
    temp = int(math.ceil(Len / Panel_Len))
    print(temp)
    for i in range(1,temp+1):
        test1 = []
        if i == temp:
            test1[i] = int(Len % Panel_Len)
        else:
            test1[i] = int(Len / Panel_Len)

    return test1


Panel_Avg(3100,1240)


运行结果:

C:\Python36\python36.exe C:/Users/irhphd/PycharmProjects/Panel/excel_test2.py
3
Traceback (most recent call last):
  File "C:/Users/irhphd/PycharmProjects/Panel/excel_test2.py", line 15, in <module>
    Panel_Avg(3100,1240)
  File "C:/Users/irhphd/PycharmProjects/Panel/excel_test2.py", line 10, in Panel_Avg
    test1[i] = int(Len / Panel_Len)
IndexError: list assignment index out of range

Process finished with exit code 1
最佳答案
2018-5-9 09:07:31
hi 原因如下

  1. import math
  2. def Panel_Avg(Len, Panel_Len ):
  3.     temp = int(math.ceil(Len / Panel_Len))
  4.     print(temp)

  5.     test1 = [ ]  #如果要保留所有数值,列表 test1 应该处于for循环外

  6.     for i in range(1,temp+1):
  7.         
  8.         if i == temp:
  9.             test1.append( int(Len % Panel_Len) )  # 之前的报错原因是:空列表无法通过索引赋值,因为列表中无元素;建议通过append()方法
  10.         else:
  11.             test1.append( int(Len / Panel_Len) )

  12.     return test1
复制代码


>>> Panel_Avg(3100,1240)
3
[2, 2, 620]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-9 09:04:27 | 显示全部楼层
你的列表test1初始化为一个长度为0的列表,是不能直接赋值的
比如
lst = []
lst[0]=1
这种写法是非法的,因为lst中本来就没有下标为0的元素

你可以这样修改
  1. test1 = [0 for x in range(temp)]
  2. for i in range(1,temp+1):
  3.     if i == temp:
  4.         test1[i-1] = int(Len % Panel_Len)
  5.     else:
  6.         test1[i-1] = int(Len / Panel_Len)
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-9 09:07:31 | 显示全部楼层    本楼为最佳答案   
hi 原因如下

  1. import math
  2. def Panel_Avg(Len, Panel_Len ):
  3.     temp = int(math.ceil(Len / Panel_Len))
  4.     print(temp)

  5.     test1 = [ ]  #如果要保留所有数值,列表 test1 应该处于for循环外

  6.     for i in range(1,temp+1):
  7.         
  8.         if i == temp:
  9.             test1.append( int(Len % Panel_Len) )  # 之前的报错原因是:空列表无法通过索引赋值,因为列表中无元素;建议通过append()方法
  10.         else:
  11.             test1.append( int(Len / Panel_Len) )

  12.     return test1
复制代码


>>> Panel_Avg(3100,1240)
3
[2, 2, 620]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-9 09:15:11 | 显示全部楼层
我修改了一下代码,不报错了,但是赋值不成功.只返回了最后一个值,中间2次的1240都没有存进数组里面.
怎么破??

# -*- coding: UTF-8 -*-
import math
def Panel_Avg(Len, Panel_Len ):
    temp = int(math.ceil(Len / Panel_Len))
    print(temp)
    for i in range(1,temp+1):
        test1 = []
        if i == temp:
            test1.append(Len % Panel_Len)     #均分到最后剩余的值
            print('Len % Panel_Len  id :',id(Len % Panel_Len))
            print(test1[0])
        else:
            test1.append(Panel_Len)     #均分N份的值
            print('Panel_Len:',Panel_Len,'id:' ,id(Panel_Len))

    return test1
kk=[]
kk = Panel_Avg(3100,1240)
print(kk)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-9 09:28:49 | 显示全部楼层

感谢,已经解决问题了,谢谢.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-5-9 09:32:06 | 显示全部楼层
BngThea 发表于 2018-5-9 09:04
你的列表test1初始化为一个长度为0的列表,是不能直接赋值的
比如
lst = []

谢谢胸弟的回复.原因不是因为超出了range范围,是因为我没用append,
我建立这个数组的初衷没打算定义他有多少个,后续需要用到count来计算数组数量,所以不能固定数组元素数量.
总之感谢胸弟的热心回复.谢谢.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-9 09:36:23 | 显示全部楼层
x8774763 发表于 2018-5-9 09:32
谢谢胸弟的回复.原因不是因为超出了range范围,是因为我没用append,
我建立这个数组的初衷没打算定义他有 ...

用append也可以,关键看你的实际需求,既然不需要记录数据长度那就用append更佳
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-17 10:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表