Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fString Nested forLoop
#1
Hi all,
I'm learning about f-strings and I know in theory nested for loops can be turned into a simpler f-string, but I'm not sure how.
rows = range(1, 10)
columns = range(1, 5)
for row in rows:
  for col in columns:
    wb.active.cell(row, col).value = 'fString_forLoop'
Any clues?
thanks,
phil
Reply
#2
wb.active.cell(row, col).value = f'row:{row}, column:{col}'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi,
Thanks for the code line.
Regarding that f-String:
wb.active.cell(row, col).value = f'row:{row}, column:{col}'
can we break this down a bit?

wb.active # openpyxl method to assign an active worksheet
.cell # openpyxl method to activate a spreadsheet cell
(row, col) # where are you pulling this from?  The only usable code from above is 'rows' & 'columns' (the variable names)
.value # openpyxl method to assign a value within a cell
 =  # assignment operator
f' # python declaration for the start of a f-String
row: # where is this from?
{row},  # what is this pointing to?
column: # where is this from?
{col} # what is this pointing to?
' # end of python f-string
thanks,
phil
Reply
#4
'row:{row}, column:{col}' is string literal. It uses some basic string formatting. I.e. what is enclosed in curly brackets will change. Check https://docs.python.org/3/library/string...ing-syntax
from your enumeration - lines 7 and 9 are just example of text. It could be whatever you want.
lines 8 and 10 uses the f-string formatting to pass variables to string.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
what about line 3?
Reply
#6
(Oct-13-2018, 09:45 AM)pcsailor Wrote: what about line 3?

They come from those lines - in Python, you loop over iterables dicrectly, without indices
for row in rows:
  for col in columns:
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
ok, maybe I'm looking at this wrong.
I'm sorry if this is so basic. I feel pretty dumb right now >:

I thought with the f-string I would only have this code:
rows = range(1, 10)
columns = range(1, 5)
wb.active.cell(row, col).value = f'row:{row}, column:{col}'
and this code is not used:

for row in rows:
  for col in columns:
    wb.active.cell(row, col).value = 'fString_forLoop'
thanks,
Phil
Reply
#8
(Oct-13-2018, 09:55 AM)pcsailor Wrote: ok, maybe I'm looking at this wrong.
I'm sorry if this is so basic. I feel pretty dumb right now >:

I thought with the f-string I would only have this code:
rows = range(1, 10)
columns = range(1, 5)
wb.active.cell(row, col).value = f'row:{row}, column:{col}'

You can process tables as a whole in pandas/numpy - though building initial values may still require either looping or comprehension (comprehension being a looping in not so deceiving disguise)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020