I have two lists with just one element as follows(each of these two lists always contains only one element):
Vnew = [600]
Newoints2 = [(2447,3480)]
I'm trying to bring both of them together using the following code sample:
for i, key in enumerate(Vnew2):
pos3[key] = newpoints2[i]
But this returns an error as IndexError: list assignment index out of range
I actually did this one for other lists which have more than one element. It worked fine and got the output as {0:(1245,674),1:(2457,895),...}
Can someone find the mistake here?
It looks like you are trying to concatenate the lists into a new list. You don't always need to enumerate through the list.
You will be able to do this by Vnew + Newoints2
Related
I been trying to code the following line but I get the message that operands could not be broadcast together with shapes (0,) (30,)
x has a length of 32
x[:-N+1] I want to access all elements except the last two
x[N:-N] I want to access all elements except the first one and the last one
x[N+1:] I want to access all elements except the first
y = x[:-N+1] - 2 * x[N:-N] + x[N+1:]
How should I index x to access those values? I'm new to python so any tips would be appreciated.
Slice except the last two: x[:N-2]
Except first and last: x[1:N-1]
Except first two: x[2:]
Python slice can be obtained by:
x[starting_index:end_index] {including starting_index element and excluding end_index}
If you don't specify the starting_index, it becomes 0 by default.
If you don't specify the end_index, it becomes N by default.
Considering the list length might be vary, the python has good apporach to access the items of a list.
Given a alist=['a','b','something',1,2]
if you use alist[1:], you will get ['b', 'something', 1, 2]
if you use alist[:1], you will get ['a','b','something',1]
if you only use alist[1], you will only get a ,which is the first item of the list.
so, if you use alist[-1:], you will get 2 , which is the first item starts from right.
if you use alist[:-1], you will get ['a','b','something',1] , which are items start from right except the fist.
In conclusion, the empty thing in the [:] is what you will get after use slicing.
I am using the following code to find the indices
result1=np.where(np.logical_and(R[i,:]>= 30, R[i,:]<= 30.3))
The output is
result1 = (array([284, 285]),)
When I run len(result1) I get a value of one.
Why is the length one when there are 2 elements.
The two numbers are indexes. I need to use the indexes but cannot because len(result1) is one.
I tried changing it to list but that did not help.
Please help me with this.
result1 is a tuple containing a single array, and thus has a length of 1; it is the array that has 2 elements.
You can use like len(result1[0]) or if you can remember later use like this; result1 = ([284,285],'') -> len(result1).
I was learning about Deep Learning in Kaggle through an exercise and this confused me. In order to write a code for checking whether something was a hot dog or not, there was a list of predictions, with each element being the most likely prediction for what a different image was. So the overall list was :
[
[('n07697537', 'hotdog', 0.8770528)],
[('n07697537', 'hotdog', 0.9659182)],
[('n07579787', 'plate', 0.7972369)],
[('n07583066', 'guacamole', 0.9996675)]
]
And one element is:
[('n07697537', 'hotdog', 0.9659182)]
So in order to check whether an image is most likely a hotdog, I'd have to get that second field, the label. But I ran into some syntax issues trying to access the field inside the nested list.
So I tried accessing the first element's label as an example (decoded is the name of the outer list) with print(decoded[0][1]). This didn't work. So I checked the sample solution after failing to figure out how to access the element cleanly without having to do something convoluted.
The sample code used
labels = [d[0][1] for d in decoded]
And that successfully makes a list of the labels. I tried to do something similar before checking the solution but I was slightly off, I tried the singular version of this by setting d = decoded[0] , and I got a list of length 1 with the three elements, like the element example earlier. What I found confusing is that d[0][1] works to give the me label, but decoded[0][1] does not. Why?
You need to work on tuples :
decoded = [[('n07697537', 'hotdog', 0.8770528)], [('n07697537', 'hotdog', 0.9659182)], [('n07579787', 'plate', 0.7972369)], [('n07583066', 'guacamole', 0.9996675)]]
d = [x for y in decoded for x in y]
labels = [d[0][1] for d in decoded]
This script gives:
['hotdog', 'hotdog', 'plate', 'guacamole']
If you would like to access the first element's label aka 'hotdog' in the first tuple, you need to print(decoded[0][0][1]) where the [1] in decoded[0][0][1] is the 2nd element in the tuple (0-indexed), the right hand [0] is the tuple itself, and the left hand [0] is the inner list.
Some background: there is actually a list enclosing other lists of tuples in your example shown as [[()],[()],[()]] where () is a tuple and [] is a list. You could in theory have multiple tuples in each inner list like [[(),(),()],[(),()],[()]] etc. However, you access values within tuples the same way as you do with lists, using indices, hence the confusion.
The code [d[0][1] for d in decoded] works because d is actually just a list of tuples (though only one tuple is in the list in this case).
I'm trying to create and fill in a multidimensional list in python 3.4 like this:
ret_array = []
then in a while list:
ret_array[k] = [track_name,t_num,disc_num,artist]
but this way I'm getting the error:
IndexError: list assignment index out of range
I know how many elements there will be but I'd prefer to do that more dynamically.
Any idea how I should do this the right way?
you can not assign a value to an index which doesn't exist but you can append it to the bottom of the list
ret_array.append([track_name,t_num,disc_num,artist])
or if you want to insert your element in a specific index you can use the insert function
ret_array.insert(k,[track_name,t_num,disc_num,artist])
you can also use dictionary like this
ret_array = {}
ret_array[k] = [track_name,t_num,disc_num,artist]
a=[]
a.append(3)
a.append(7)
for j in range(2,23480):
a[j]=a[j-2]+(j+2)*(j+3)/2
When I write this code, it gives an error like this:
Traceback (most recent call last):
File "C:/Python26/tcount2.py", line 6, in <module>
a[j]=a[j-2]+(j+2)*(j+3)/2
IndexError: list assignment index out of range
May I know why and how to debug it?
Change this line of code:
a[j]=a[j-2]+(j+2)*(j+3)/2
to this:
a.append(a[j-2] + (j+2)*(j+3)/2)
You're adding new elements, elements that do not exist yet. Hence you need to use append: since the items do not exist yet, you cannot reference them by index. Overview of operations on mutable sequence types.
for j in range(2, 23480):
a.append(a[j - 2] + (j + 2) * (j + 3) / 2)
The reason for the error is that you're trying, as the error message says, to access a portion of the list that is currently out of range.
For instance, assume you're creating a list of 10 people, and you try to specify who the 11th person on that list is going to be. On your paper-pad, it might be easy to just make room for another person, but runtime objects, like the list in python, isn't that forgiving.
Your list starts out empty because of this:
a = []
then you add 2 elements to it, with this code:
a.append(3)
a.append(7)
this makes the size of the list just big enough to hold 2 elements, the two you added, which has an index of 0 and 1 (python lists are 0-based).
In your code, further down, you then specify the contents of element j which starts at 2, and your code blows up immediately because you're trying to say "for a list of 2 elements, please store the following value as the 3rd element".
Again, lists like the one in Python usually aren't that forgiving.
Instead, you're going to have to do one of two things:
In some cases, you want to store into an existing element, or add a new element, depending on whether the index you specify is available or not
In other cases, you always want to add a new element
In your case, you want to do nbr. 2, which means you want to rewrite this line of code:
a[j]=a[j-2]+(j+2)*(j+3)/2
to this:
a.append(a[j-2]+(j+2)*(j+3)/2)
This will append a new element to the end of the list, which is OK, instead of trying to assign a new value to element N+1, where N is the current length of the list, which isn't OK.
At j=2 you're trying to assign to a[2], which doesn't exist yet. You probably want to use append instead.
If you want to debug it, just change your code to print out the current index as you go:
a=[]
a.append(3)
a.append(7)
for j in range(2,23480):
print j # <-- this line
a[j]=a[j-2]+(j+2)*(j+3)/2
But you'll probably find that it errors out the second you access a[2] or higher; you've only added two values, but you're trying to access the 3rd and onward.
Try replacing your list ([]) with a dictionary ({}); that way, you can assign to whatever numbers you like -- or, if you really want a list, initialize it with 23479 items ([0] * 23479).
Python lists must be pre-initialzed. You need to do a = [0]*23480
Or you can append if you are adding one at a time. I think it would probably be faster to preallocate the array.
Python does not dynamically increase the size of an array when you assign to an element. You have to use a.append(element) to add an element onto the end, or a.insert(i, element) to insert the element at the position before i.