python - 索引错误: list assignment index out of range in Python

标签 python list

我使用以下代码解决了 python 中的问题:

T = int(raw_input()) 
C=[] 
for x in range(T):     
    C[x]=int(raw_input()) 
res=[] 
for x in range(T):     
    res[x]=2**C[x]+2*C-1      
for x in range(T):     
    print "%d\n",(res[x])   

出现了这个错误:

Traceback (most recent call last):
  File "C:\Users\ACER\Documents\works\source code\python practice\Test1.py", line 4, in <module>
    C[x]=int(raw_input())
IndexError: list assignment index out of range

请问可以解决这个错误吗

最佳答案

您的列表是空的。 perl 会自动扩展数组,但 python 不会。

C.append(int(raw_input()))

相反。

关于python - 索引错误: list assignment index out of range in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25407328/

相关文章:

python从字符串中获取日期

java - 此脚本中的 int 和 Integer 有什么区别?

python - 迭代具有不同元素的列表

arrays - 如何找到重新排序列表以获取另一个列表所需的步骤列表?

python - 升级 python

python - python/numpy中的线性组合

python 3.2-使用字典为字母表中的每个字母分配一个数值&根据字母值查找.txt文件中的单词总和

c# - c# winForms 列表数组的奇怪结果

python - 尝试使用 def 时出现 Nameerror

python - python抽象基类是否应该从对象继承