Tuple out of index - tuples

IndexError: tuple index out of range
I'm getting this error and I'm not sure why, because my code looks like
interpolated_mean = np.array([np.ones(X.shape[1])*self.means[digit] for digit in range(10)])
where self.means returns
[0.2950995723007362, 0.35157590125195076, 0.37957208825304445, 0.3191758946180043, 0.35155769941304243, 0.3984539137236574, 0.3008216529579752, 0.3630624989356188, 0.4623879642875728, 0.33528044078766084]

Related

creating a list in list python

i have to write a function that takes as an input a list and if to Consecutive numbers have the same sighn then they both go to a same list in lists if not a new list in list is opened for example:
[2,5,-3,-1,-1,3,-2,-2]->[[2,5]],[-3,-1,-1],[3],[-2,-2]]
this is my code :
def num_8(lst):
my_lst=[[lst[0]]]
j=0
for i in range(1,len(lst)):
if (lst[i]> 0 and lst[i-1]>0) or (lst[i]<0 and lst[i-1]<0):
my_lst[j].append(lst[i])
print(my_lst)
else:
j=j+1
my_lst[j].append([lst[i]])
return my_lst
print(num_8([2,5,-3,-1,-1,3,-2,-2]))
but i keep on getting
my_lst[j].append([lst[i]])
IndexError: list index out of range
and i dont know were i get out of range :( thanks
The error is with the else part.
my_lst[j] does not exisit at that point(The len of my_lst is j -1)
So you'll have to append the first number to my_lst and not my_lst[j] (Similar to what you have done in the first line of the function)
my_lst.append([lst[i]])

Why is for loop trying to access element outside range of list?

When iterating over a list I am getting a key error, I am confused as to why since I am iterating from 0 to range(len(list))
I tried to iterate from 0 to len(list), but len returns an integer so I can't iterate over it. Next I tried iterating from 0 to range(len(list)) because 'range' returns a list of integers from 0 to len(list). However I get a key error
for index in range(len(objectName.listName)):
print("\n\t listName[%i]: %s" % (index, objectName.listName[index]))
When I execute the above code all the elements in the list get printed but then the iterator trys to access an element which is outside the range, and I get the following error:
Traceback (most recent call last):
File "filename.py", line 276, in
print("\n\t listName[%i]: %s" % (index, objectName.listName[index]))
KeyError: 10
I expected to see each element printed out.
Note: There are only 10 elements (indexed 0 -> 9 ) but the iterator trys to access "key:10".
for index in range(len(objectName.listName)-1):
print("\n\t listName[%i]: %s" % (index, objectName.listName[index]))
Because len(objectName.listName) returns 11
range(11) = 0,1,2...10

How to get value from list using index?

I have to try to get one by one value from list using index and i have try to get index value and update my stage one by one and respectively.
my python code below :
for ress in status_list:
print"res", ress
#self.workflow_stages = ress
if ress:
self.workflow_stages = ress
for index, item in enumerate(status_list):
print "test::", index
index_init = index
print"index_init:::", index_init
next = index_init + 1
print "next", next
lent = len(status_list)
print"lent", lent
return True
Thanks.
This for index, item in enumerate(status_list): means that index variable holds your respective index of the status_list and item variable holds your value. So in given for loop, item will hold the value of the corresponding index.
Generally, if you don't use the enumerate functionality to iterate over a list, you can access a value of the list like this status_list[index]

python : list and dictionary, TypeError: list indices must be integers, not str

i want to calculte the specfic numbers of words from the given sentence...words are alredy save in my dictonary and sentence is will be input from user.....
Here is my code.
from collections import Counter
Find_word= raw_input('Write Sentence:')
wordTosearch=['is', 'am']
sentence= Find_word.split()
cnt = Counter(sentence)
for k in sentence:
if k in wordTosearch:
print k, wordTosearch[k]
if cnt[wordTosearch]>1:
print "aggresive"
else:
print "Not agressive"
from collections import Counter
Find_word= raw_input('Write Sentence:')
sentence= Find_word.split()
cnt = Counter(sentence)
wordTosearch=['is', 'am']
for k in wordTosearch:
print k, cnt[k]
if all( cnt[x] > 1 for x in wordTosearch ):
print "aggresive"
else:
print "Not agressive"
I dont know what the bottom part should do though.
wordTosearch is a list of words.
The following is iterating through that list of words:
if k in wordTosearch:
print k, wordTosearch[k] # <----
where k is a word, and wordTosearch[k] is the attempt to access a list value by a string key which gives you "TypeError: list indices must be integers, not str".
You can not access list values by string indices as Python lists are "numbered" sequences

TypeError: list indices must be integers, not unicode in python code

I used the split() function to convert string to a list time = time.split() and this is how my output looks like :
[u'1472120400.107']
[u'1472120399.999']
[u'1472120399.334']
[u'1472120397.633']
[u'1472120397.261']
[u'1472120394.328']
[u'1472120393.762']
[u'1472120393.737']
Then I tried accessing the contents of the list using print time[1] which gives the index out of range error (cause only a single value is stored in one list). I checked questions posted by other people and used print len(time). This is the output for that:
1
[u'1472120400.107']
1
[u'1472120399.999']
1
[u'1472120399.334']
1
[u'1472120397.633']
1
[u'1472120397.261']
1
[u'1472120394.328']
1
[u'1472120393.762']
1
[u'1472120393.737']
I do this entire thing inside a for loop because I get logs dynamically and have to extract out just the time.
This is part of my code:
line_collect = lines.collect() #spark function
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
time = time.split()
#print time[1] #index out of range error which is why I wrote another for below
for k in time :
time1 = time[k]#trying to put those individual list values into one variable but get type error
print len(time1)
I get the following error :
time1 = time[k]
TypeError: list indices must be integers, not unicode
Can someone tell me how to read each of those single list values into just one list so I can access each of them using a single index[value]. I'm new to python.
My required output:
time =['1472120400.107','1472120399.999','1472120399.334','1472120397.633','1472120397.261','1472120394.328','1472120393.762','1472120393.737']
so that i can use time[1] to give 1472120399.999 as result.
Update: I misunderstood what you wanted. You have the correct output already and it's a string. The reason you have a u before the string is because it's a unicode string that has 16 bits. u is a python flag to distinguish it from a normal string. Printing it to the screen will give you the correct string. Use it normally as you would any other string.
time = [u'1472120400.107'] # One element just to show
for k in time:
print(k)
Looping over a list using a for loop will give you one value at a time, not the index itself. Consider using enumerate:
for k, value in enumerate(time):
time1 = value # Or time1 = time[k]
print(time1)
Or just getting the value itself:
for k in time:
time1 = k
print(time1)
--
Also, Python is zero based language, so to get the first element out of a list you probably want to use time[0].
Thanks for your help. I finally got the code right:
newlst = []
for line in line_collect :
a = re.search(rx1,line)
time = a.group()
newlst.append(float(time))
print newlst
This will put the whole list values into one list.
Output:
[1472120400.107, 1472120399.999, 1472120399.334, 1472120397.633,
1472120397.261, 1472120394.328, 1472120393.762, 1472120393.737]