Python Looping Gurus, question for you,... - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python Looping Gurus, question for you,... (/thread-12868.html) |
Python Looping Gurus, question for you,... - pcsailor - Sep-17-2018 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 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 , 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 RE: Python Looping Gurus, question for you,... - Mekire - Sep-17-2018 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) I assume the values could be read in from some file or however you have them stored.
RE: Python Looping Gurus, question for you,... - pcsailor - Sep-17-2018 Thanks. That is very elegant. RE: Python Looping Gurus, question for you,... - volcano63 - Sep-17-2018 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 RE: Python Looping Gurus, question for you,... - pcsailor - Sep-17-2018 (Sep-17-2018, 09:24 AM)volcano63 Wrote: pointing out a package you use is always a good ideaThanks, good point RE: Python Looping Gurus, question for you,... - pcsailor - Sep-19-2018 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,
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 |