Python Forum
Assigning Value To Period Seperated List - 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: Assigning Value To Period Seperated List (/thread-27463.html)



Assigning Value To Period Seperated List - jo15765 - Jun-07-2020

I have period separated list of values, like this
1.1.1.1
1.2.1.1
1.1.2.1
etc and these can be duplicated. I need a way to assign a alpha value to them so for example

1.1.1.1 = A
1.2.1.1 = B
1.1.2.1 = C
and so on. How could I do this with python?


RE: Assigning Value To Period Seperated List - Yoriz - Jun-07-2020

I don't understand what you are wanting to do but when assigning the variable goes first
A = '1.1.1.1'



RE: Assigning Value To Period Seperated List - bowlofred - Jun-07-2020

In what form are these values? Is it a string?

If so, you can just use them as a dictionary key, and set the value to what you want.

>>> d = {}
>>> d['1.1.1.1'] = 'A'
>>> d['1.2.1.1'] = 'B'
>>> d['1.1.2.1'] = 'C'
>>> d
{'1.1.1.1': 'A', '1.2.1.1': 'B', '1.1.2.1': 'C'}



RE: Assigning Value To Period Seperated List - jo15765 - Jun-07-2020

@Yoriz --

i have an excel workbook that will have period separated values.

As an example like the below
1.1.1.1
1.2.1.1
1.1.2.1
2.1.1.1
2.1.2.1
2.2.2.1
1.1.1.1
1.1.1.1
2.1.1.1
In the above list we have duplicate values. I need each unique value in the list to be assigned an alphabetical value, starting with A.

(using proper python this time)
A = 1.1.1.1
B = 1.2.1.1
C = 1.1.2.1
D = 2.1.1.1
E = 2.1.2.1
F = 2.2.2.1
A = 1.1.1.1
A = 1.1.1.1
D = 2.1.1.1
how can I auto assign the alphabetical value to the period separated values?


--------------------------------------------------------------------------------------------------------

@bowlofred -

I am scanning the excel workbook and creating the period separated list like so

    mr2 = ws2.max_row
    mc2 = ws2.max_column

    for y in range(2, mr2):
        strValue = ''
        for x in range(2, mc2):
            strValue = strValue + '.' + str(ws2.cell(y, x).value)
        strValue = strValue[1:]
        ws2.cell(y, mc2 + 1).value = strValue
Does that answer your question?


RE: Assigning Value To Period Seperated List - bowlofred - Jun-07-2020

Put all your excel strings into a dictionary. Then loop through all the keys and assign your alphabetical values.

import string
import pprint

excel_vals = [
        "1.1.1.1",
        "1.2.1.1",
        "1.1.2.1",
        "2.1.1.1",
        "2.1.2.1",
        "2.2.2.1",
        "1.1.1.1",
        "1.1.1.1",
        "2.1.1.1",
        ]

# Put excel_vals into a dictionary.
d = dict.fromkeys(excel_vals)

# Assign alpha value to each key in the dictionary
for k,v in zip(d, string.ascii_uppercase):
    d[k] = v

pp = pprint.PrettyPrinter()
pp.pprint(d)
Output:
{'1.1.1.1': 'A', '1.1.2.1': 'C', '1.2.1.1': 'B', '2.1.1.1': 'D', '2.1.2.1': 'E', '2.2.2.1': 'F'}



RE: Assigning Value To Period Seperated List - jo15765 - Jun-07-2020

That looks like exactly what I'm after. I guess my last hurdle is how do I read in an entire column using openpyxl?

I have
mc2 = ws2.max_column
I'd then want to read all data from mc2 into the dictionary excluding the first row (that would be the header)


RE: Assigning Value To Period Seperated List - Yoriz - Jun-07-2020

If the initial question is resolved please click set solved and start a new thread for a separate question.