python: how do i know when i am on the last for cycle

2024/10/5 23:24:40
for i in range(len(results_histogram)):if i!=len(results_histogram)-1:url+=str(results_histogram[i])+','

my if statement is checking whether i am on the last loop, but it is not working. what am i doing wrong?

Answer

To avoid the question slightly, you seem to have rewritten str.join:

','.join(results_histogram)

If you get an error like TypeError: sequence item 0: expected string, int found, then you can convert the intermediate results to a string with

','.join(map(str, results_histogram))

str.join is undoubtedly more efficient than concatenating multiple strings in a loop, because in Python, strings are immutable, so every concatenation results in the creation of a new string, which then has to be garbage collected later.


Specifically, your example is "not working" because you skip the last element entirely, when you only want to skip adding the comma. This is clear and obvious with a small example:

>>> x = [1,2,3]
>>> for i in range(len(x)):
...   if i != len(x) - 1:
...     print str(x[i]) + ',',
... 
1, 2,

So you could rewrite your example as

for i in range(len(results_histogram)):url += str(results_histogram[i])if i!=len(results_histogram)-1:url += ','

But you should still stick with str.join.

https://en.xdnf.cn/q/119014.html

Related Q&A

scrape text in python from https://brainly.co.id/tugas/148

scrape "Jawaban terverifikasi ahli" in green box from the url https://brainly.co.id/tugas/148, possibly the color of green tick icon to the left of it also(tag <use xlink:href="#icon-…

Percentage of how similar strings are in Python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic…

A Python dictionary with repeated fields

Im constructing a dictionary with Python to use with a SOAP API.My SOAP API takes an input like this:<dataArray><AccountingYearData><Handle><Year>string</Year></Handle&…

psexec run python script passed from host

I am trying to run a python script on a remote computer via psexec. I am able to connect and run python.exe with the following:C:\test>psexec \\192.168.X.X -u domain\administrator -p password -i C:…

TypeError: main() missing 1 required positional argument: self

My code and error is below and I was trying to understand why I am getting the error and how to fix it. I tried this without self and got another error TypeError: load_data() takes 0 positional argumen…

Python not calling external program

I am having problems with a python program that I wrote. It is actually plpython3u. I am running the program as a Trigger from postgres. I am pretty sure the trigger part works. My test python prog…

Selenium: How do I retry browser/URL when ValueError(No tables found)

I have a code that scrapes oddsportal website. Sometimes while scraping, I get ValueError("No tables found") and when I manually refresh browser, page loads. How do I do it via code? My code…

For loop for web scraping in python

I have a small project working on web-scraping Google search with a list of keywords. I have built a nested For loop for scraping the search results. The problem is that a for loop for searching keywor…

operation on a variable inside a class in python

Im new with oop and python. Ive been trying to do a simple thing: there is class called Foo(),it contains a variable called x which is initially set to zero.>>>a = Foo() >>>a.x >&g…

Print several sentences with different colors

Im trying to print several sentences with different colors, but it wont work, I only got 2 colors, the normal blue and this redimport sys from colorama import init, AnsiToWin32stream = AnsiToWin32(sys.…