开发者

python - assign letter of the alphabet to each value in a list

I have a list of values in a for loop. e.g. myList = [1,5,7,3] which I am using to create a bar chart (using google charts)

I want to label each value with a letter of the alphabet (A-Z) e.g. A = 1, B = 5, C = 7, D = 3

What is the best way to do this running through a for loop

e.g.

for x in myList:
    x.label = LETTER OF THE ALPHABET

The l开发者_如何学Pythonist can be any length in size so wont always be just A to D

EDIT

myList is a list of objects not numbers as I have put in example above. Each object will have a title attached to it (could be numbers, text letters etc.), however these titles are quite long so mess things up when displaying them on Google charts. Therefore on the chart I was going to label the chart with letters going A, B, C, ....to the lenth of the myList, then having a key on the chart cross referencing the letters on the chart with the actual titles. The length of myList is more than likely going to be less than 10 so there would be no worries about running of of letters.

Hope this clears things up a little


If you want to go on like ..., Y, Z, AA, AB ,... you can use itertools.product:

import string
import itertools

def product_gen(n):
    for r in itertools.count(1):
        for i in itertools.product(n, repeat=r):
            yield "".join(i)

mylist=list(range(35))

for value, label in zip(mylist, product_gen(string.ascii_uppercase)):
    print(value, label)
#   value.label = label

Part of output:

23 X
24 Y
25 Z
26 AA
27 AB
28 AC
29 AD


import string
for i, x in enumerate(myList):
    x.label = string.uppercase[i]

This will of course fail if len(myList) > 26


import string

myList = [1, 5, 7, 3]
labels = [string.uppercase[x+1] for x in myList]
# ['C', 'G', 'I', 'E']


for i in range(len(myList)):
    x.label = chr(i+65)

More on the function here.


charValue = 65
for x in myList:
  x.label = chr(charValue)
  charValue++

Be careful if your list is longer than 26 characters


First, if myList is a list of integers, then,

for x in myList:
    x.label = LETTER OF THE ALPHABET

won't work, since int has no attribute label. You could loop over myList and store the labels in a list (here: pairs):

import string

pairs = []
for i, x in enumerate(myList):
    label = string.letters(i) # will work for i < 52 !!
    pairs.append( (label, x) )

# pairs is now a list of (label, value) pairs

If you need more than 52 labels, you can use some random string generating function, like this one:

import random

def rstring(length=4):
    return ''.join([ random.choice(string.uppercase) for x in range(length) ])


Since I like list comprehensions, I'd do it like this:

[(i, chr(x+65)) for x, i in enumerate([1, 5, 7, 3])]

Which results in:

[(1, 'A'), (5, 'B'), (7, 'C'), (3, 'D')]


import string

for val in zip(myList, string.uppercase):
    val[0].label = val[1]


You can also use something like this:

from string import uppercase

res = ((x , uppercase[i%26]*(i//26+1)) for i,x in enumerate(inputList))

Or you can use something like this - note that this is just an idea how to deal with long lists not the solution:

from string import uppercase
res = ((x , uppercase[i%26] + uppercase[i/26]) for i,x in enumerate(inputList))


Are you looking for a dictionary, where each of your values are keyed to a letter of the alphabet? In that case, you can do:

from string import lowercase as letters

values = [1, 23, 3544, 23]
mydict = {}
for (let, val) in zip(letters, values):
    mydict[let] = val

<<< mydict == {'a': 1, 'c': 23, 'b': 3544, 'd': 23}

<<< mydict['a'] == 1

You'll have to add additional logic if you need to handle lists longer than the alphabet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜