1

I have an array

a =array([ 0.74552751,  0.70868784,  0.7351144 ,  0.71597612,  0.77608263,
         0.71213591,  0.77297658,  0.75637376,  0.76636106,  0.76098067,
        0.79142821,  0.71932262,  0.68984604,  0.77008623,  0.76334351,
        0.76129872,  0.76717526,  0.78413129,  0.76483804,  0.75160062,
        0.7532506 ], dtype=float32)

I want to store my array in item,value format and can't seems to get it right. I'm trying to get this format:

  a = [(0, 0.001497),
  (1, 0.0061543),
   ..............
  (46, 0.001436781),
  (47, 0.00654533),
  (48, 0.0027139),
  (49, 0.00462962)],
7
  • Is that output a connected to the original a in any way, or is it merely given to show the format? Commented Apr 15, 2014 at 16:28
  • Its merely given to show the format. Basically i'm struggling with format. Commented Apr 15, 2014 at 16:36
  • What works somehow is : for item in enumerate(array): print >> f , item where f = open('random.txt','w') Commented Apr 15, 2014 at 16:56
  • 2
    Are you sure you need to directly store the index? NumPy has functions like argmin, where, argsort, etc that normally mean you don't need to do things like this. Commented Apr 15, 2014 at 16:59
  • 1
    If your index is as simple as you show it in the example (i.e. counting the numbers), then there is no need to state it, totally agreed with @MrE. So unless the indexes are going to change or it isn't as homogeneous in reality as you are showing it, there doesn't seem to be a good reason to do this. Commented Apr 15, 2014 at 17:30

1 Answer 1

2

Numpy arrays have a fixed data type that you must specify. It looks like a data type of int for your item and float for you value would work best. Something like:

import numpy as np
dtype = [("item", int), ("value", float)]
a = np.array([(0, 0.), (1, .1), (2, .2)], dtype=dtype)

The string part of the dtype is the name of each field. The names allow you to access the fields more easily like this:

print a['value']
# [ 0.,  0.1,  0.2]

a['value'] = [7, 8, 9]
print a
# [(0, 7.0) (1, 8.0) (2, 9.0)]

If you need to copy another array into the array I describe above, you can do it just by using the filed name:

new = np.empty(len(a), dtype)
new['index'] = [3, 4, 5]
new['value'] = a['value']
print new
# [(3, 7.0) (4, 8.0) (5, 9.0)]
Sign up to request clarification or add additional context in comments.

8 Comments

In my case, Array is not just 3 items but runs in thousands.
May be i wasn't clear enough. I have an array with values. i want to save the array with index,value where index is just normal serial number.
Then you'll have to type out thousands of values, JK. You have to be more clear about what you mean by "save". It sounds like you're trying to build some kind of table, or maybe just a text file. If you're specific about what you need we'll be specific about how to get it.
Just create a new array using numpy.empty with the right fields then you can copy anything you want into the array.
@sb32134, I'm glad you got what you wanted. In cas you're interested, you've created a dictionary. When you call cb.items() it returns a list of tuples where each tuple is a (key, value) pair from the dictionary. Some things to keep in mind, dictionaries have no order, so the result is not guaranteed to be in any order. For example you might get [(1, .2), (0, .1), (2, .3)] if your array started as [.1, .2, .3]. Also if you're only interested in the final list, you can just do list(enumerate(array)), skip the dictionary altogether.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.