Python纽比。在哪里:TypeError:只有整数标量数组才能转换为标量索引;在字典列表中

2024-06-14 11:51:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典列表(这是从数据库中获取的一些数据的一个例子)。在

通过使用纽比。在哪里我想把所有有特定索引的项目放到另一个列表中

from astropy.coordinates import SkyCoord
from astropy import units as u
import numpy
from operator import itemgetter
from decimal import *

lightcurve_data = {}
lightcurve_data['stars'] = [{u'delta_j2000': Decimal('66.113525'), u'calibrated_error': 0.0442272797226906, u'id': 35549943L, u'filter': u'V', u'date': 2458008.64520255, u'calibrated_magnitude': Decimal('12.5425'), u'alpha_j2000': Decimal('325.777614')}, {u'delta_j2000': Decimal('66.113535'), u'calibrated_error': 0.0246241167187691, u'id': 38672301L, u'filter': u'R', u'date': 2458038.52371412, u'calibrated_magnitude': Decimal('11.8814'), u'alpha_j2000': Decimal('325.777077')}]


stars_for_filter = sorted(lightcurve_data['stars'], key=itemgetter('calibrated_magnitude'), reverse=True)
coord_list = SkyCoord(map(itemgetter('alpha_j2000'), stars_for_filter), map(itemgetter('delta_j2000'), stars_for_filter), frame='fk5', unit=u.degree)


index_array = [1., 1.]

median_ra = numpy.zeros(int(numpy.max(index_array)), dtype=float)

lightcurve_data['seperated'] = {}

for i in range(0, len(median_ra)):
    check_in_index_array = numpy.where(index_array == i + 1)
    key = 'some unique key'
    # This bit works
    median_ra[i] = numpy.median(coord_list[check_in_index_array[0]].ra.degree)
    # The rest doesn't...
    lightcurve_data['seperated'][key] = []
    lightcurve_data['seperated'][key] = lightcurve_data['stars'][check_in_index_array[0]]

但是,运行此代码时,我遇到以下异常: TypeError: only integer scalar arrays can be converted to a scalar index 有趣的是,check_in_index_array[0]的相同用法也适用于AstroPy SkyCoord的列表

完整的回溯如下:

^{pr2}$

有什么方法可以修复这个异常吗?或者换一种方式?在

谢谢

威尔。在


Tags: keyinfromimportnumpydataindexfilter
1条回答
网友
1楼 · 发布于 2024-06-14 11:51:48

基于@hpaulj's comment,我已经能够通过将lightcurve_data['stars']从Python列表转换为NumPy数组来回答这个问题。在

array_stars = numpy.asarray(lightcurve_data['stars'])

那么

^{pr2}$

例外不再是偶然的-一切都很好。谢谢您!在

相关问题 更多 >