3

I'm trying to use the built-in function enumerate() to label some points or vertices where each point is represented by its coordinates in a list(or set) of tuples which essentially looks like {(4,5), (6,8), (1,2)}

I want to assign a letter starting from "a" in ascending order to each tuple in this set, using enumerate() does exactly the same but It's written in a way that it returns the value of the index of each item so that it's a number starting from 0.

is there any way to do it other than writing my own enumerate()?

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Josh
  • 140
  • 8

4 Answers4

5

Check this out:

import string
tup = {(4,5), (6,8), (1,2)}
dic = {i: j for i, j in zip(string.ascii_lowercase, tup)}

This returns:

{'a': (4, 5), 'b': (6, 8), 'c': (1, 2)}
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Raghul Raj
  • 1,428
  • 9
  • 24
  • 3
    No nead for slicing, just `zip(string.ascii_lowercase,tup)` will already limit the result to the length of the shortest argument. – tobias_k Mar 10 '20 at 13:41
  • Yeah @tobias_k you're right. I've updated my answer now..Thanks for pointing out – Raghul Raj Mar 10 '20 at 13:43
4

This is enumerate's signature.

enumerate(iterable, start=0)

Use start as 65 for 'A' and 97 for 'a'.

lst=[(1,2),(2,3),(3,4),...]
for idx,val in enumerate(lst,65):
    print(chr(idx),val)

A (1, 2)
B (2, 3)
C (3, 4)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
0

Maybe this is a way to get what you want, using chr():

L = [(4,5), (6,8), (1,2)]
for k, v in enumerate(L):
    print(chr(65 + k), v)

Output :

A (4, 5)
B (6, 8)
C (1, 2)
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
0

The enumerate function is defined as follow :

enumerate(iterable, start=0)

I think just have to write your own enumerate or an wrapper around enumerate.

SidoShiro92
  • 169
  • 2
  • 16