Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Code Generator
#8
I would use a dictionary, and make use of the modulo operator. Something like this:
N_cols = 30
name2index = {"{}{}".format(chr(65 + int(a/12)), a%12 + 1):(a+1) for a in range(N_cols)}
print(name2index)

print(name2index['A1'])
print(name2index['B12'])
int(a/12) turns a number into an integer, dropping any decimals. So int(1/12) = 0, int(2/12) = 0, ..., int(12/12) = 1, int(13/12) = 1, etc. We can use this to get the letter.
The modulo operator % gives the remainder of a certain division. So 0 % 12 = 0, 1 % 12 = 1, 2 % 12 = 2, ... , 12 % 12 = 0, 13 % 12 = 1, 14 % 12 = 2, etc.

The outermost braces {} turn it into a dictionary. The keys are 'A1', 'A2', etc. and the values are 1, 2, 3, 4...

P.S. here's my version for replicating tests
Reply


Messages In This Thread
Python Code Generator - by KatherineHov - Jul-24-2017, 05:58 PM
RE: Python Code Generator - by MTVDNA - Jul-24-2017, 10:00 PM
RE: Python Code Generator - by KatherineHov - Jul-25-2017, 04:13 PM
RE: Python Code Generator - by MTVDNA - Jul-25-2017, 04:52 PM
RE: Python Code Generator - by KatherineHov - Jul-25-2017, 05:40 PM
RE: Python Code Generator - by MTVDNA - Jul-25-2017, 06:03 PM
RE: Python Code Generator - by KatherineHov - Jul-25-2017, 09:16 PM
RE: Python Code Generator - by MTVDNA - Jul-25-2017, 09:54 PM
RE: Python Code Generator - by KatherineHov - Jul-26-2017, 05:15 PM
RE: Python Code Generator - by MTVDNA - Jul-26-2017, 09:56 PM
RE: Python Code Generator - by KatherineHov - Jul-27-2017, 01:22 AM
RE: Python Code Generator - by MTVDNA - Jul-27-2017, 10:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is the following code returning a generator expression? quazirfan 8 1,861 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  Python returns generator instead of None Tawnwen 4 4,898 Mar-09-2018, 07:06 AM
Last Post: DeaD_EyE
  receive from a generator, send to a generator Skaperen 9 5,721 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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