I have a python dictionary of the type
Mutual={'A':[[1],[2],[],[]],'B':[[1],[],[],[]]}
I want to access the elements for key 'A'.
I tried this:
count=0
for z in range(2):
print Mutual["A"][z][count]
count+=1
I am getting the following error
IndexError: list index out of range
Can anyone suggest why is it so. Must be some silly mistake which I am unable to catch.
When you run the first iteration, Mutual["A"][z][count] returns 1. On the next iteration Mutual["A"][z][count] is supposed to be the second element in the list [2] (z is 1 and count is 1 as well) which does not exist. That is why you get an IndexError: list index out of range error.
A suggested modification to your code could be:
listA = Mutual["A"]
for z in range(len(listA)):
for w in range(len(listA[z])):
print listA[z][w]
This way, if the inner lists are of size zero the inner loop will not be executed and therefore, you will not try to access an out of bounds index.
It is usual in python to iterate the actual elements vs. using range() and indexes:
for z in Mutual['A']:
for count in z:
print count
Related
I have a list of unknown length containing strings. I want to compare the last item with the second to last item, but I can't find an easy way to retrieve these elements so I can compare them.
I tried getting the length of the list and then using (length-1) as the index I pass into the list GET function, but this doesn't seem to work.
set(my_list a b c) # I don't actually know the length of the original list
list(LENGTH my_list list_len)
list(GET my_list (list_len-1) last_item) # doesn't work
I expect to have element c saved in the last_item variable, but it remains empty. What is the right way to do this in CMake?
You have to use math in order to perform mathematical operations.
list(LENGTH my_list list_len)
math(EXPR list_last "${list_len} - 1")
list(GET my_list ${list_last} last_item)
Edit: It is possible to get elements from the end of a list using negative numbers, you can use -1 to get the last item of a list:
list(GET my_list -1 last_item)
I'm rather new to Python so bear with me. I'm trying to write a bit of code that will calculate a sliding slope for each frame of a movie, where each frame is a sublist in a larger list. I want to calculate the slopes for every n number of values ("width" in the code) to produce a new list of lists with the slopes for each frame in their own sublist.
This code worked with six values in each sublist but when I run it with more numbers in each sublist it gives me the "list index out of range" error in reference to YSD.append(SlidingSlope(smoothedarrayX,array_y[:][k],3)).
Here's the code:
def SlidingSlope(array_x,array_y,width):
arrayofslopes=[]
for i in range(0, len(array_x)):
x=array_x[i:i+width]
y=array_y[i:i+width]
arrayofslopes.append(sp.stats.linregress(x,y))
return arrayofslopes
def YFramesSlopes(array_y):
YSlopedata=[]
for j in range(0,len(array_y)):
YSD=[]
YSlopedata.append(YSD)
for k in range(0,len(array_y[j])):
YSD.append(SlidingSlope(smoothedarrayX,array_y[:][k],3))
return YSlopedata
So I guess the overarching issue I'm struggling with is how to make this sliding slope function iterate over the values in the Y sublists using the same list of X values, if that makes sense. Any help is greatly appreciated.
I have the following list:
a = [1,(landa/mu)]
I want to add elements to this list using previous elements:
a = a + [[f(i)*a[n-i] for i in range(1,n)] for n in range(2,K)]
in which f is a function and landa, mu and K are parameters. However, it is giving me the following error:
list index out of range
Could you please tell me how can I solve this problem? I can simply write it in a for loop but I want it to be in a list comprehension format to be done fast since K sometimes become a large number.
I am not able to update the empty dictionary with keys from list l2 and values from list l1 of same index position, wherever the wanted keys are not found in the dictionary. I'm getting an IndexError again and again.
l1=[56,87,12,32]
l2=['a','gf','lk','po',56]
dict={}
print "length of l2 is",len(l2)
print "items",dict.items()
for i in range( len(l2)):
if l2[i] in (dict.keys()):
print 'key is present '
else:
print 'key ' ,l2[i],' not present'
dict[l2[i]]=l1[i]
print dict
Your last for loop iteration value is 4 (i = 4), and l1's highest index is 3 (4 items in the list, we start the index at 0). So when your for loop runs l1[4] your gonna get a index out of range error, because l1's index only goes up to 3. You either have to add an extra element to l1 or change your for loop.
p.s. Try not to use l's next to numbers in your variables because they tend to look like the number 1.
I have a named tuple with values [x,y].
Both fields will hold strings.
My problem is ,I want to match the contents of the 'x' field and access the 'y' field of that index.
name_array_tuple_list
is the name of the list which holds the named tuples.
So far I have got this
print([x[0] for x in name_array_tuple_list].index('SNVT'))
Which prints the index of the matched value.
My question is how to access the y value of lets say the index 3.
You were very close. Try this:
print([point.y for point in name_array_tuple_list if point.x == 'SNVT'])
Interpret the code as follows:
print
a list of
y field contents
for every named tuple in a list
but only if the named tuple's x field matches SVNT