Python Forum
Python Looping Gurus, question for you,...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Looping Gurus, question for you,...
#1
Hello,
I have a looping question for all you Python gurus, because, while I understand the theoretical of iteration, I don't have the creation of loops yet in practice. This forum has helped but I must have an old, thick skull Wall

Here's my question.
If I have this code,

# Cell values
sheet['A1'].value = ''
sheet['A2'].value = ''
sheet['A3'].value = ''
sheet['A4'].value = ''
sheet['A5'].value = ''
sheet['A6'].value = ''
sheet['A7'].value = ''
sheet['A8'].value = ''
sheet['A9'].value = ''
sheet['A10'].value = ''
sheet['A11'].value = ''
sheet['A12'].value = ''
sheet['A13'].value = ''
sheet['A14'].value = ''
sheet['A15'].value = ''
sheet['A16'].value = ''
sheet['A17'].value = ''
sheet['A18'].value = ''
sheet['A19'].value = ''
sheet['A20'].value = ''
sheet['A21'].value = ''
sheet['A22'].value = ''
sheet['A23'].value = ''
sheet['A24'].value = ''
sheet['A25'].value = ''
sheet['A26'].value = ''
sheet['A27'].value = ''
sheet['A28'].value = ''
sheet['A29'].value = ''
sheet['A30'].value = ''
sheet['A31'].value = ''
sheet['A32'].value = ''
sheet['A33'].value = ''
sheet['A34'].value = ''
sheet['A35'].value = ''
sheet['A36'].value = ''
sheet['A37'].value = ''
sheet['A38'].value = ''
sheet['A39'].value = ''
sheet['A40'].value = ''
and each .value is different, is it worthwhile to create a loop, or what would be the best way to do a loop?

If I was to guess, & I will Think , I would say to create a tuple with each .value stored within the tuple. Then use a forLoop to create each argument starting with this code,
sheet['A1'].value = ''
while iterating +1 and adding the next sequential value from the tuple.

Am I in the ballpark?

Thanks,
Phil
Reply
#2
Quote:is it worthwhile to create a loop, or what would be the best way to do a loop?
Absolutely.

I'm not sure exactly what you want the final thing to be, but something like:
from pprint import pprint

values = (5, 2, 9, 4, 7, 32, 6, 11, 56, 92)
results = {"A{}".format(i) : v for i,v in enumerate(values, start=1)}

pprint(results)
Output:
{'A1': 5, 'A10': 92, 'A2': 2, 'A3': 9, 'A4': 4, 'A5': 7, 'A6': 32, 'A7': 6, 'A8': 11, 'A9': 56}
I assume the values could be read in from some file or however you have them stored.
Reply
#3
Thanks. That is very elegant.
Reply
#4
If you are using openpyxl (BTW, pointing out a package you use is always a good idea), here's an example of filling table with the sheet.cell method, though -IMO - in this line

...         _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))


the method can be called without assignment
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
#5
(Sep-17-2018, 09:24 AM)volcano63 Wrote: pointing out a package you use is always a good idea
Thanks, good point Confused
Reply
#6
Further question if I could,...

If I have this tuple:
('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten')
and I want to accomplish three things,
  1. list the tuple elements down a column starting with A1 and ending with A10,
  2. have the number-iteration in A1 to A10 replace the '#' placeholder in this code:
    sheet['A#'].value = 'tuple-element'
  3. have the tuple element iterate through and replace the 'tuple-element' placeholder in this same code:
    sheet['A#'].value = 'tuple-element'

how would I accomplish this?
If possible, could you point me in the direction using simple loops instead of f-strings? I'm still learning loops and I'm challenged with the older, simpler style right now.

I've been working on it and I'm stuck with this consistent result
{"sheet['A1'].value = ''": 'one', "sheet['A10'].value = ''": 'ten', etc.
So I have the A1-A10 numbering working.
I'm having trouble inserting the tuple element in between the quotations in this part of the code
value = ''
Thanks,
Phil
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Looping a question until correct feedback is given TylerTrunzo 9 1,722 Oct-31-2022, 12:19 PM
Last Post: rob101
  Virtual Assistant code is looping back in python ShishirModi 1 2,480 Oct-13-2020, 08:04 AM
Last Post: Nickd12

Forum Jump:

User Panel Messages

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