index error in python (work with excel program)

jaql4c8m  于 2022-12-14  发布在  Python
关注(0)|答案(1)|浏览(124)
plans_dtype = ["str", "str", "int", "int", "int", "int","int", "int","int", "int", "int", "int","int", "int"]
/ other code/

plans_dtype_dict = {plans_cols[i]:plans_dtype[i] for i in range(0,len(plans_cols))}
plans_dtype_dict

我想创建一个字典,其中我想把plans_cols作为键,把plans_dtype作为它的值。

IndexError                                Traceback (most recent call last)
Input In [24], in <cell line: 1>()
----> 1 plans_dtype_dict = {plans_cols[i]:plans_dtype[i] for i in range(0,len(plans_cols))}
      2 plans_dtype_dict

Input In [24], in <dictcomp>(.0)
----> 1 plans_dtype_dict = {plans_cols[i]:plans_dtype[i] for i in range(0,len(plans_cols))}
      2 plans_dtype_dict

IndexError: list index out of range

我试图检查我的代码和它的列表然而,我不明白为什么它给了一个索引错误

mbjcgjjk

mbjcgjjk1#

plans_dtype = ["str", "str", "int", "int", "int", "int", "int", "int", "int", "int", "int", "int", "int", "int"]

方法1

dic = {k : plans_dtype[i] for i, k in enumerate(plans_cols)}

方法2

dic = {plans_cols[i] : plans_dtype[i] for i in range(len(plans_col))}

相关问题