Python Forum
Assigning Value To Period Seperated List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning Value To Period Seperated List
#1
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?
Reply
#2
I don't understand what you are wanting to do but when assigning the variable goes first
A = '1.1.1.1'
Reply
#3
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'}
Reply
#4
@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?
Reply
#5
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'}
Reply
#6
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)
Reply
#7
If the initial question is resolved please click set solved and start a new thread for a separate question.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse/read from file seperated by dots giovanne 5 1,130 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Assigning a new value to variable uriel 1 1,616 Dec-04-2021, 02:59 PM
Last Post: Underscore
  assigning a variable :( gr3yali3n 0 1,336 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Find data using a period of time in SQLITE3 SmukasPlays 2 2,197 Jul-30-2020, 02:02 PM
Last Post: SmukasPlays
  Assigning variables Godserena 4 2,212 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,963 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Assigning an item from a list xlev 1 1,469 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  Fix the solution of “Binary period” sonia 0 6,428 Jul-22-2019, 02:46 PM
Last Post: sonia
  assigning index 3Pinter 6 3,040 Jan-18-2019, 10:07 PM
Last Post: nilamo
  Not adding and assigning? 00712411 7 4,242 Oct-10-2018, 07:11 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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