Why list index out of range? - python-3.x

count = [0]*1001
count[1000] = 0
Getting this issue on LeetCode, and ran into this. I would expect the above to create 1001 elements, or 0 to 1000 valid positions. But getting a list index error instead.

i've ran your code in the command line with python 3.8.0 and no IndexError was flagged? Is this the exact code your getting the error from or is a similar example to the code you're using?

Related

Inexplicable value error due to encoding of file in Python

I have a file with saved modelling data like this
1
0
1
Trying to iterate over the file using readlines, which correctly reads the line as zero or one.
Error happens when I cast the string into 1 or 0 Value error my code is this
st1=myfile.readline()
try:
line=float(st1.strip(), base=10)
if line==0: zeros+=1
if line==1 or line==0: ones+=1
except ValueError
I am getting value error in each line. I have no clue what is causing this. I do not want to load whole file into a variable as it is quite large. I tried changing 1 to 100 still error.Though a trivial error, I am unable to fix it.I Searched online forums, stack overflow no help. I don't want Numpy, please don't give some solution using Array operation on NumPy. Can you please help?

Missing module docstring (missing-module-docstring)

I am running below the addition of two numbers in jupyter
'''
This program `summationoftwonumbers` and displays their results
'''
A = 1
B = 5
print('Sum of Numbers:',A+B)
It is running fine giving an output as "Sum of Numbers: 6"
But when running a file by using PyLint, summationoftwonumber.ipynb gives the below errors.
summationoftwonumbers.ipynb:1:0: C0114: Missing module docstring (missing-module-docstring)
summationoftwonumbers.ipynb:1:0: W0104: Statement seems to have no effect (pointless-statement)
I do not understand why this is happening.
It's that you used quotes to write a comment, which in some cases creates a docstring. Python is warning you that the statement has no effect in your program. To get rid of the warning you could rewrite the line:
''' This program summationoftwonumbers and displays their results '''
as a normal comment:
# This program summationoftwonumbers and displays their results

Python Machine Learning Regression P.1 pandas error

Pycharm screenshot of error message with code above
I don't know what to do about the error. I don't know what it means
also there should print(data.head()) between line 8 & 9.
The error is happening in your data = read.csv() line. Check your working directory, or point explicitly to the file location, using something like:
data = read.csv("C:/Users/user/filename.csv")

Python 3.7, Feedparser module cannot parse BBC weather feed

When I parse the example rss link provided by BBC weather it gives only an empty feed, the example link is: "https://weather-broker-cdn.api.bbci.co.uk/en/forecast/rss/3day/2643123"
Ive tried using the feedparser module in python, I would like to do this in either python or c++ but python seemed easier. Ive also tried rewriting the URL without https:// and with .xml and it still doesn't work.
import feedparser
d = feedparser.parse('https://weather-broker-cdn.api.bbci.co.uk/en/forecast/rss/3day/2643123')
print(d)
Should give a result similar to the RSS feed which is on the link, but it just gets an empty feed
First, I know you got no result - not an error like me. Perhaps you are running a different version. As I mentioned, it yields a result on an older version in Python 2, using a program that has been running solidly every night for about 5 years, but it throws an exception on a freshly installed feedparser 5.2.1 on Python 3.7.4 64 bit.
I'm not entirely sure what is going on, but the function called _gen_georss_coords which is throwing a StopIteration on the first call. I have noted some references to this error due to the implementation of PEP479. It is written as a generator, but for your rss feed it only has to return 1 tuple. Here is the offending function.
def _gen_georss_coords(value, swap=True, dims=2):
# A generator of (lon, lat) pairs from a string of encoded GeoRSS
# coordinates. Converts to floats and swaps order.
latlons = map(float, value.strip().replace(',', ' ').split())
nxt = latlons.__next__
while True:
t = [nxt(), nxt()][::swap and -1 or 1]
if dims == 3:
t.append(nxt())
yield tuple(t)
There is something curious going on, perhaps to do with PEP479 and the fact that there are two separate generators happening in the same function, that is causing StopIteration to bubble up to the calling function. Anyway, I rewrote it is a somewhat more straightforward way.
def _gen_georss_coords(value, swap=True, dims=2):
# A generator of (lon, lat) pairs from a string of encoded GeoRSS
# coordinates. Converts to floats and swaps order.
latlons = list(map(float, value.strip().replace(',', ' ').split()))
for i in range(0, len(latlons), 3):
t = [latlons[i], latlons[i+1]][::swap and -1 or 1]
if dims == 3:
t.append(latlons[i+2])
yield tuple(t)
You can define the above new function in your code, then execute the following to patch it into feedparser
saveit, feedparser._gen_georss_coords = (feedparser._gen_georss_coords, _gen_georss_coords)
Once you're done with it, you can restore feedparser to its previous state with
feedparser._gen_georss_coords, _gen_georss_coords = (saveit, feedparser._gen_georss_coords)
Or if you're confident that this is solid, you can modify feedparser itself. Anyway I did this trick and your rss feed suddenly started working. Perhaps in your case it will also result in some improvement.

While loop inside PyQt form not working

I'm using Python 3.4 and PyQt 5. I have written a while look Inside PyQt form. If the loop contains a single statement, it's working. But if the loop contains multiple statements, it's throwing an indentation error.
Below is working:
while count > 0: print("count value",count)
Below is throwing an indentation error:
while count>0:
print("count value", count)
count = count -1
Please help me to solve the problem. The same code is working if I run it outside PyQt form code.

Resources