tags:

views:

316

answers:

4

HI there,

I am having a problem. I have an array of 31 elements called colors. I also have another array with integer numbers that vary between 0 and 31, this is called c. I want to produce a new array where the values in c are now the corresponding values in colors.

I write:

newarray=colors[c]

but get the error message "list indices must be integers" but c is an array of integers. I am new to python and have not got time to do the tutorials as I just need it for a specific plotting task. Could anyone give me a hand?

Thanks

+3  A: 

array of integers != integer

list indices must be integers - you've given a list of integers.

You probably want a list comprehension:

newarray = [ colors[i] for i in c ]

But you really ought to learn the language properly before using it. It's not like python takes a long time to learn.

EDIT:

If you're still getting the same error then your assertion that c is a list of integers is incorrect.

Please try:

print type(c)
print type(c[0])
print type(colors)
print type(colors[0])

Then we can work out what types you have got. Also a short but complete example would help, and probably teach you a lot about your problem.

EDIT2:

So if c is actually a list of string, you should probably have mentioned this, strings don't get automatically converted to integers, unlike some other scripting languages.

newarray = [ colors[int(i)] for i in c ]

EDIT3:

Here is some minimal code that demonstrates a couple of bug fixes:

x=["1\t2\t3","4\t5\t6","1\t2\t0","1\t2\t31"]
a=[y.split('\t')[0] for y in x]
b=[y.split('\t')[1] for y in x]
c=[y.split('\t')[2] for y in x]  # this line is probably the problem
colors=['#FFFF00']*32
newarray=[colors[int(i)] for i in c]
print newarray

a) colors needs to be 32 entries long. b) the elements from c (i) in the list comprehension need to be converted to integers (int(i)).

Douglas Leeder
thank you for your post, however when I write the command as above the same error message is returned.
I think it is because the elemants of array c are stored as strings. When I type : type(c[1])I get the message "<type"str"> and so I guess I need to change them to type"int". I am having trouble doing this as I can't seem to change them all at once.
I guess we both wrote that at the same time. Colors is also string.
Sorry colors[0] is a string, both the arrays are lists.
Just post your complete code. We can not guess what you are doing from these comments.
recursive
from pylab import*f=open("transformdata.txt")x=f.readlines()a=[y.split('\t')[0] for y in x]b=[y.split('\t')[1] for y in x]c=[y.split('\t')[2] for y in x]subplot(111,projection="hammer")colors=['#FFFF00',etc-there are 31 colours here]newarray=[colors [i] for i in c]p=plot([a],[b],"o",mfc=color)show()
It would probably be best if you edit your question with your code. Also I don't think the pylab stuff is relevant to your problem.
Douglas Leeder
A: 

Python does not support arbitrary list indexing. You can use single integer index like c[4] or slice like c[4:10]. SciPy library has richer indexing capabilities. Or just use list comprehensions as Douglas Leeder advices.

Rorick
A: 

Okay, I think I know what you are after...

So you have your list of 31 colours. Say for argument it is a list of 31 strings like this...

colours = [ "Black", "DarkBlue", "DarkGreen", ... "White" ]

And 'c' is an array of numbers in the range 0 to 31, but in random order...

import random
c = [x for x in xrange(32)]
random.shuffle(c)
# 'c' now has numbers 0 to 31 in random order
# c = [ 2, 31, 0, ... 1]

And what you want to do is map the values in c as an index into the list of colors so that you end up with a list of colors in as indexed by c and in that order...

mapped = [color[idx] for idx in c]
# mapped has the colors in the same order as indexed by 'c'
# mapped = ["DarkGreen", "White", "Black", ... "DarkBlue"]

If that ain't what you want then you need to revise your question!

I think you've got a basic problem in that your list of colours should have 32 elements (colours) in it, not 31, if the list 'c' is a random shuffle of all numbers in the range 0 to 31 (that's 32 numbers, you see).

Pev
the c array has over 6000 elements with a range of 0-31.
+1  A: 

This is your code: (from your comment)

from pylab import* 
f=open("transformdata.txt") 
x=f.readlines() 
a=[y.split('\t')[0] for y in x] 
b=[y.split('\t')[1] for y in x] 
c=[y.split('\t')[2] for y in x]  # this line is probably the problem
subplot(111,projection="hammer") 
colors=['#FFFF00']*31
newarray=[colors [i] for i in c] 
p=plot([a],[b],"o",mfc=color) show()

Without knowing exactly what your data is, or what you're trying to accomplish, I'd suggest trying this:

c=[int(y.split('\t')[2]) for y in x]
recursive
the data consists of coordinates a(range -180 to 180) and b(range -90 to 90) of supernovae. The c value describes a property of the SN that has been returned from some Matlab functions. I want to plot this data onto the hammer projection plot where the c value will be described by a colour gradient. ie when c=0 the colour will be yellow and when c=31 the colour will be blue and all other values are linearly scaled inbetween these colours.I have followed your advise but after the plot line I get the message "sequence length is 6643; must be 3 or 4"
sorry I have just seen a mistake in what I have written, the last line should read:p=plot([a],[b],"o",mfc=newarray): I think that the problem is here, I don't think that it can plot each point(a,b) with the same element of newarray.
When giving error messages it would be nice to tell us which line was causing the problem.
Douglas Leeder
the "sequence length is 6643; must be 3 or 4" comes up after the line that starts p=plot etc.
I think you've got the arguments for plot very wrong - mfc takes a single color - at least that's how I read the documentation http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.plot I suggest asking a new question as you've moved off the original question.
Douglas Leeder
In your code, a, b, and c are all lists of strings. You need to convert them to some kind of numeric types if you want to use them as numbers.
recursive