如何创建带有负索引的python列表 [英] how do I create a python list with a negative index

查看:33
本文介绍了如何创建带有负索引的python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,需要创建一个带有负索引的列表,但到目前为止还没有成功.

I'm new to python and need to create a list with a negative index but have not been successful so far.

我正在使用以下代码:

a = []
for i in xrange( -20, 0, -1 ):
    a[i] = -(i)
    log.info('a[{i}]={v}'.format(i=i, v=a[i]))
else:
    log.info('end')

并将日志输出显示为

end

顺便说一句,我使用的是站点呼叫Quantopian,因此log.info来自其基础结构,只是将输出打印到Web控制台中.

Incidentally I'm using a site call quantopian so the log.info is from their infrastructure and just print out the output into a web console.

我在做什么错了?

预先感谢您的帮助.

推荐答案

如果您使用的是Quantopian,建议您熟悉numpy和pandas.例如:

If you are using Quantopian, it is advisable that you become familiar with numpy and pandas. For example:

>>> import numpy as np 
>>> -1*np.arange(20)

array([  0,  -1,  -2,  -3,  -4,  -5,  -6,  -7,  -8,  -9, -10, -11, -12,
       -13, -14, -15, -16, -17, -18, -19])

然后您将得到 a [1] ==-1 a [5] ==-5 等.

这篇关于如何创建带有负索引的python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆