Hey guys,
I want to create a new array in python. But cant seem to find a way of doing it without having to give initializing values...

Basically i want to do wat this java line does
float myArr[65353];

and to make matters worse when storing/retrieving data i wont be doing it in sequence. meaning data entry will be something like

myarr[45]=2
myarr[619]=3.465
myarr[36249]=28.405

the main point is for me to use a known integer as the index for storing/reading

Create an empty list:

my_list = []

Create a list of specified length with all "blank" values:

my_list = [''] * my_length

Instead of mixing string with floats, consider

myarr = [0.0] * full_length

I don't know if that's the case here, but often you don't need all the slots in the array in one session, but only a few. Instead of allocating the full size, you may consider storing (index, value) tuples instead.

mydata.append((45, 2))

Then, at the end of the session, you can update the main data store (a database or whatever) from mydata.

Thanks.. the declaration seems to be working. :)
But now i'm having trouble entering data and retrieving data from it.

mydata.append((45, 2)) maybe working (doesn't give bugs) but i don't know to check if its gone to where it should since i don't have an extraction method.
while googling i got answers like

mydata = myarray.index([3])
 mydata =  pop.index(3)

and so on... but none of them seem to work for me....
this is what u'm doing now, but that too gives a bug saying "IndexError: list index out of range".

ST=[0.0]
ST.append((3,42.23))
print("my test val",ST[3])
Member Avatar for masterofpuppets

hi
the error occurs because when you append ( 3, 42.23 ) to [ 0.0 ] you get a list with two elements and you're trying to access a third one which is not there, so you should do :

>>>ST = [ 0.0 ]
>>>ST
[ 0.0 ]
>>>ST.append( ( 3,42.23 ) )
>>>ST
[0.0, ( 3, 42.229999999999997 ) ]
>>>print( "my test val", ST[ 0 ] )
my test val 0.0
>>>print( "my test val", ST[ 1 ] )
my test val (3, 42.229999999999997)

mydata.append((45, 2)) maybe working (doesn't give bugs) but i don't know to check if its gone to where it should since i don't have an extraction method

Why don't you just fire up your interpreter and see what's happening:

>>> mydata = [0.0]
>>> mydata
[0.0]
>>> # mydata only has one element
>>> mydata = [0.0] * 5
>>> mydata
[0.0, 0.0, 0.0, 0.0, 0.0]
>>> # Now mydata has 5 elements
>>> mydata.append((45,2))
>>> mydata
[0.0, 0.0, 0.0, 0.0, 0.0, (45, 2)]
>>> # Using append places the object on the end of the list (this object is a tuple
>>> mydata.insert(2, 45)
>>> mydata
[0.0, 0.0, 45, 0.0, 0.0, 0.0, (45, 2)]
>>> # Using insert (notice index first) "inserts" the object (45) to position 2; however it pushes the rest of the elements up a notch
>>> mydata[4] = 24.2
>>> mydata
[0.0, 0.0, 45, 0.0, 24.199999999999999, 0.0, (45, 2)]
>>> # Looks like indexing is what you should be using.  This changes the element at index 4
>>>

I hope that was clear enough. If not let me know

Actually, the first parameter in the brackets is the index i want to store the value at.

ST=[0.0]
    ST.insert(2,2.2)
    ST.insert(0,0.0)
    ST.insert(3,3.3)
    ST.insert(4,4.4)
    ST.insert(1,1.1)
    ST.insert(5,5.5)
    print ("test point")
    print("my test val 1= ",ST.pop(1))
    print("my test val 2= ",ST.pop(2))
    print("my test val 3= ",ST.pop(3))
    print("my test val 4= ",ST.pop(4))
    print("my test val 5= ",ST.pop(5))

so this should print
my test val 0= 0.0
my test val 1= 1.1
my test val 2= 2.2
my test val 3= 3.3
my test val 4= 4.4
my test val 5= 5.5

Actually, the first parameter in the brackets is the index i want to store the value at.

ST=[0.0]
    ST.insert(2,2.2)
    ST.insert(0,0.0)
    ST.insert(3,3.3)
    ST.insert(4,4.4)
    ST.insert(1,1.1)
    ST.insert(5,5.5)
    print ("test point")
    print("my test val 1= ",ST.pop(1))
    print("my test val 2= ",ST.pop(2))
    print("my test val 3= ",ST.pop(3))
    print("my test val 4= ",ST.pop(4))
    print("my test val 5= ",ST.pop(5))

so this should print
my test val 0= 0.0
my test val 1= 1.1
my test val 2= 2.2
my test val 3= 3.3
my test val 4= 4.4
my test val 5= 5.5

Yes, but when you're inserting to an index, that index must exist. Index 2 does not exist in a single element list (only index 0 and index 1). When you insert, you're also extending your list by one. What you want is to declare a list with 0.0 of the desired length as I demonstrated earlier and instead of insert you should be using indexing.

EDIT: Also note that using pop removes that element from the list. Once again, you should be using indexing.

hey jlm999!
I've just tried your solution.. but the fact that everything gets pushed back is a big NO-NO for me... cos i wont be able to find them at the said reference places...
is there someway i can delete an object at a place and put in a new one there without messing everything up? (i'm playing with that idea right now)

ST=[0.0]*5
    print ("test ARRAY : ", ST)
    ST.insert(2,2.2)
    print ("test ARRAY : ", ST)
    ST.insert(0,0.0)
    print ("test ARRAY : ", ST)
    ST.insert(3,3.3)
    print ("test ARRAY : ", ST)
    ST.insert(4,4.4)
    print ("test ARRAY : ", ST)
    ST.insert(1,1.1)
    print ("test ARRAY : ", ST)

gives me

test ARRAY :  [0.0, 0.0, 0.0, 0.0, 0.0]
test ARRAY :  [0.0, 0.0, 2.2, 0.0, 0.0, 0.0]
test ARRAY :  [0.0, 0.0, 0.0, 2.2, 0.0, 0.0, 0.0]
test ARRAY :  [0.0, 0.0, 0.0, 3.3, 2.2, 0.0, 0.0, 0.0]
test ARRAY :  [0.0, 0.0, 0.0, 3.3, 4.4, 2.2, 0.0, 0.0, 0.0]
test ARRAY :  [0.0, 1.1, 0.0, 0.0, 3.3, 4.4, 2.2, 0.0, 0.0, 0.0]

Yes, but when you're inserting to an index, that index must exist. Index 2 does not exist in a single element list (only index 0 and index 1). When you insert, you're also extending your list by one. What you want is to declare a list with 0.0 of the desired length as I demonstrated earlier and instead of insert you should be using indexing.

EDIT: Also note that using pop removes that element from the list. Once again, you should be using indexing.

I just saw this... let me google indexing :)
Thanks man!

Here's an indexing example:

>>> mydata = [0.0] * 5
>>> mydata
[0.0, 0.0, 0.0, 0.0, 0.0]
>>> mydata[2] = 45
>>> mydata
[0.0, 0.0, 45, 0.0, 0.0]
>>> mydata[0] = 1.0
>>> mydata
[1.0, 0.0, 45, 0.0, 0.0]
>>> mydata[2]
45
>>>

Thanks.. the declaration seems to be working. :)
But now i'm having trouble entering data and retrieving data from it.

mydata.append((45, 2)) maybe working (doesn't give bugs) but i don't know to check if its gone to where it should since i don't have an extraction method.

I'm not sure I know what you mean by "gone to where it should", but you can wither iterate through mydata, or convert it to a dictionary at the end of the session to access it via index. In both cases you start with an empty list.

Here is an assumed scenario:

  1. You obtain the data by parsing some input stream, and the index where the item goes is contained in that stream. (Hence you don't know the index in advance.)
  2. For my 2nd tip, I assumed the data obtained in one parsing session is not the full data set, but will be used to update or augment some base data set (which could be in a database).

Assuming furher that you have some sort of generator-style function that parses the input stream and yields (index, value) pairs, perhaps like this (a pretty common pattern):

def parseStream(stream):
  for line in stream:
    index, value = parseLine(line) # TODO: implement parseLine
    yield (index, value)

Then, in "phase 1" you can first construct mydata as a list of (index, value) pairs:

stream = open('myfile.ext', 'rt')
mydata = list( parseStream(stream) )

Then, in "phase 2" you iterate through the data like this:

for index, value in mydata:
  # do something with value and index

You can even convert mydata to a dictionary and access it by index:

mydata = dict(mydata)
valueAt42 = mydata[42]
indices = mydata.keys()
values = mydata.values()
...

Thanks guys! i'm taking all your tips and coming up with a nice soup of a solution. Now i just realized i have a new complication
All this time i was reading in a string that has integers and casting it into int and then using that value as the index to my array. Suddenly in the list of integers i have a string (a word).. it'd be cool if i can have some way of uniquely converting this into a new int and appending it to the back of the list... but that just feels like a lot of trouble.. so i just want to ignore this case.... Any idea what kinda IF statement i can use?

EDIT:
ahh fixed it with

if (startport<'65400'):

A truly empty value is None.

mylist = [None] * 5

Creates a list with five empty elements.

commented: Read posts carefully and think twice before attacking somebody's reputation! +0

(... in case I confused you with my last post :) )

I think the core message is to use a dictionary instead of a pre-allocated list if you populate ony a small subsets of the list, perhaps like in

mydata = {}
for line in file('datafile.ext'):
  ... # obtain index and value
  mydata[index] = value

To access a value at some index:

valueAt42 = mydata.get(42, 0.0)

That way, you get virtually the same results as with a preallocated array, but with (potentially) a fraction of the used memory.

Of course, if you know you will actually populate all or most of the array anyway, then you should prefer a pre-allocated array for performance reasons.

A truly empty value is None.

mylist = [None] * 5

That's not true. In the contet of a sequence or set (or any data set generally), "empty" has often the meaning of "missing", and a value is only missing if it's not in the list. Avoiding "special" values in the list that are incompatible in behavior with the "normal" values greatly simplifies their usage. For example, you can't do this if you have None's in your list:

myOtherList = [ x * 2 for x in mylist ] # mul will fail for None's

Instead, you'd have to test for the None special case all over. Sometimes you can't avoid that, but often you can.

commented: Clearly misunderstands that empty is not the same as non-existant. +0

@mitsuevo: Convert each value to an integer upon obtaining it:

value = int(value)

But I think this question should have gone in a new thread instead of diverging on this one.

A Python dictionary is an efficient way to store data and has very fast lookup speeds ...

# in Python you could use a dictionary key:val pair
# where the key is the integer index and val is the float
# if you just want to store values at an index

myarr = dict()

# load and store
myarr[45] = 2
myarr[619] = 3.465
myarr[36249] = 28.405

# retrieve val at index
ix = 619
val = myarr[ix]
print( "Value at index %d --> %s" % (ix, val) ) 

# this is a safer way to retrieve val at index
ix = 600
# if ix has not been loaded, use a default of 0.0
val = myarr.get(ix, 0.0)
print( "Value at index %d --> %s" % (ix, val) )

"""my output -->
Value at index 619 --> 3.465
Value at index 600 --> 0.0
"""

Sorry, didn't see pythopian's post of the same thing.

It worked it worked!!!
Thanks guys!!!
Will contact you all if i need any further help, and looking forward to being a part of DaniWeb more often! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.