index-error

Divide elements of a list by integer with list comprehension: index out of range

烂漫一生 提交于 2019-12-20 02:32:32
问题 I am trying to divide all the elements of a list filled with integers by another integer (functionality like in numpy arrays) by list comprehension, like so: results = 300 * [0] for i in range(100): for j in range(300): results[j] += random.randrange(0,300) average_results = [results[x] / 100 for x in results] However, if I run this in Python, it throws an IndexError: list index out of range I have worked around this by using a regular for loop: average_results = [] for x in results: average

IndexError: List index out of range - Python CSV

喜欢而已 提交于 2019-12-17 22:00:19
问题 I am pulling 10 tweets from Twitter using tweepy and storing it in a CSV for later displaying it on the front-end webpage. My code refreshes every 60 minutes, and at certain times I get the "IndexError". Following is the exact error: return ks[5] IndexError: List index out of range Following is the function to retrieve the particular tweet from CSV: def tweet6(self): with codecs.open('HELLOTWITTER.csv', 'r', encoding='utf-8', errors='ignore') as f: reader = csv.reader(f) d = {} for i, row in

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

非 Y 不嫁゛ 提交于 2019-12-05 19:43:59
问题 Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "<ipython-input-264-85078aa26398>", line 1, in <module> t3 = np.concatenate((t1,t2),axis=1) IndexError: axis 1 out of bounds [0, 1) why does it report that axis 1 is out of bounds? 回答1: Your title explains it - a 1d array does not have a 2nd axis! But having said that, on my system as on @Oliver W. s, it does not produce an error In

Concatenation of 2 1D `numpy` Arrays Along 2nd Axis

蓝咒 提交于 2019-12-04 02:42:42
Executing import numpy as np t1 = np.arange(1,10) t2 = np.arange(11,20) t3 = np.concatenate((t1,t2),axis=1) results in a Traceback (most recent call last): File "<ipython-input-264-85078aa26398>", line 1, in <module> t3 = np.concatenate((t1,t2),axis=1) IndexError: axis 1 out of bounds [0, 1) why does it report that axis 1 is out of bounds? Your title explains it - a 1d array does not have a 2nd axis! But having said that, on my system as on @Oliver W. s, it does not produce an error In [655]: np.concatenate((t1,t2),axis=1) Out[655]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17

TypeError/IndexError when iterating with a for loop, and referencing lst[i]

守給你的承諾、 提交于 2019-12-04 02:07:44
问题 I'm using a for loop to iterate over a list like this: lst = ['a', 'b', 'c'] for i in lst: print(lst[i]) But there must be something wrong with that, because it throws the following exception: Traceback (most recent call last): File "untitled.py", line 3, in <module> print(lst[i]) TypeError: list indices must be integers or slices, not str And if I try the same thing with a list of integers, it throws an IndexError instead: lst = [5, 6, 7] for i in lst: print(lst[i]) Traceback (most recent

Python IndexError: list index out of range when using a list as an iterable

僤鯓⒐⒋嵵緔 提交于 2019-12-02 08:07:08
问题 Here is the code: import math as m primeproduct = 5397346292805549782720214077673687806275517530364350655459511599582614290 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181] def pseudoroot(n): print(n) for i in n: if n[i] > m.sqrt(primeproduct): return n[i-1] #greatest divisor below the root psrprime = pseudoroot(primes) Running this code gives this

What does 'index 0 is out of bounds for axis 0 with size 0' mean?

帅比萌擦擦* 提交于 2019-12-01 04:14:58
I am new to both python and numpy. I ran a code that I wrote and I am getting this message: 'index 0 is out of bounds for axis 0 with size 0' Without the context, I just want to figure out what this means.. It might be silly to ask this but what do they mean by axis 0 and size 0? index 0 means the first value in the array.. but I can't figure out what axis 0 and size 0 mean. The 'data' is a text file with lots of numbers in two columns. x = np.linspace(1735.0,1775.0,100) column1 = (data[0,0:-1]+data[0,1:])/2.0 column2 = data[1,1:] x_column1 = np.zeros(x.size+2) x_column1[1:-1] = x x_column1[0]

What does 'index 0 is out of bounds for axis 0 with size 0' mean?

时光总嘲笑我的痴心妄想 提交于 2019-12-01 01:49:08
问题 I am new to both python and numpy. I ran a code that I wrote and I am getting this message: 'index 0 is out of bounds for axis 0 with size 0' Without the context, I just want to figure out what this means.. It might be silly to ask this but what do they mean by axis 0 and size 0? index 0 means the first value in the array.. but I can't figure out what axis 0 and size 0 mean. The 'data' is a text file with lots of numbers in two columns. x = np.linspace(1735.0,1775.0,100) column1 = (data[0,0:

IndexError: List index out of range - Python CSV

断了今生、忘了曾经 提交于 2019-11-28 14:13:39
I am pulling 10 tweets from Twitter using tweepy and storing it in a CSV for later displaying it on the front-end webpage. My code refreshes every 60 minutes, and at certain times I get the "IndexError". Following is the exact error: return ks[5] IndexError: List index out of range Following is the function to retrieve the particular tweet from CSV: def tweet6(self): with codecs.open('HELLOTWITTER.csv', 'r', encoding='utf-8', errors='ignore') as f: reader = csv.reader(f) d = {} for i, row in enumerate(reader): d[row[0]]=row[1:] if (i>=10): break ks=list(d) return (ks[5]) This error occurs only