【问题标题】:Python; To get the index of a value in a list, which is the same value as i in separate listPython;获取列表中某个值的索引,该索引与单独列表中的 i 值相同
【发布时间】:2014-11-02 18:26:44
【问题描述】:

distanceorder 是距离的数字排序

for i in range(0, len(distance)):
    if distanceorder[i]==distance[i]:

        RA_order[i]=RA[i]
        DEC_order[i]=DEC[i]
        epoch_order[i]=epoch[i]                 

    else:
        x=distance.index("distanceorder[i]")
        RA_order[i]=RA[x]
        DEC_order[i]=DEC[x]
        epoch_order[i]=epoch[x]

出现错误 ValueError: 'distanceorder[i]' is not in list 我需要以相应距离排序的 RA、DEC 和 epoch。

【问题讨论】:

  • 引号是干什么用的?你不想distance.index(distanceorder[i])吗?

标签: python list indexing


【解决方案1】:

语法错误替换

x=distance.index("distanceorder[i]")

x=distance[index(distanceorder[i])]

如果你的距离是列表类型,你得到错误的原因是因为如果你输入引号,它不会评估distanceorder[i]的值,而是会寻找distanceorder[i]的索引,没有引号它会首先得到distanceorder[i]的值

【讨论】:

    【解决方案2】:

    到了最后 x=distance.index(distanceorder[i])

    但是,我需要定义新列表 RA_order 的大小,而不是让它的大小不受限制,因此它一直在寻找无穷无尽的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 2021-10-06
      • 2013-03-30
      • 2018-04-13
      相关资源
      最近更新 更多