how do I create a python list with a negative index

2024/9/22 10:38:42

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

I'm using this code:

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')

and getting the log output as

end

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.

What am I doing wrong?

Thanks in advance for your help.

Answer

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])

Then you will have a[1]==-1, a[5]==-5, etc.

https://en.xdnf.cn/q/71954.html

Related Q&A

Select subset of Data Frame rows based on a list in Pandas

I have a data frame df1 and list x:In [22] : import pandas as pd In [23]: df1 = pd.DataFrame({C: range(5), "B":range(10,20,2), "A":list(abcde)}) In [24]: df1 Out[24]:A B C 0 a …

convert csv to json (nested objects)

I am new to python, and I am having to convert a csv file to json in following format:CSV File :firstname, lastname, email, customerid, dateadded, customerstatus john, doe, [email protected], 124,26/11…

How can I read exactly one response chunk with pythons http.client?

Using http.client in Python 3.3+ (or any other builtin python HTTP client library), how can I read a chunked HTTP response exactly one HTTP chunk at a time?Im extending an existing test fixture (writt…

ValueError: cannot reindex from a duplicate axis in groupby Pandas

My dataframe looks like this:SKU # GRP CATG PRD 0 54995 9404000 4040 99999 1 54999 9404000 4040 99999 2 55037 9404000 4040 1556894 3 55148 9404000 4040 1556894 4 55254 94…

How to calculate class weights of a Pandas DataFrame for Keras?

Im tryingprint(Y) print(Y.shape)class_weights = compute_class_weight(balanced,np.unique(Y),Y) print(class_weights)But this gives me an error:ValueError: classes should include all valid labels that can…

How to change the layout of a Gtk application on fullscreen?

Im developing another image viewer using Python and Gtk and the viewer is currently very simple: it consists of a GtkWindow with a GtkTreeView on the left side displaying the list of my images, and a G…

How to upload multiple file in django admin models

file = models.FileField(upload_to=settings.FILE_PATH)For uploading a file in django models I used the above line. But For uploading multiple file through django admin model what should I do? I found t…

Convert numpy array to list of datetimes

I have a 2D array of dates of the form:[Y Y Y ... ] [M M M ... ] [D D D ... ] [H H H ... ] [M M M ... ] [S S S ... ]So it looks likedata = np.array([[2015, 2015, 2015, 2015, 2015, 2015], # ...[ 1, …

PyQt: how to handle event without inheritance

How can I handle mouse event without a inheritance, the usecase can be described as follows:Suppose that I wanna let the QLabel object to handel MouseMoveEvent, the way in the tutorial often goes in th…

DHT22 Sensor import Adafruit_DHT error

So Ive properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. Im running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22 The webs…