user506710
user506710

Reputation:

Skip a letter in dictionary

I need to assign the uppercase to numbers in a dictionary but with one letter S not there.

ie.

in this alphadict = dict((x, i) for i, x in enumerate(string.ascii_uppercase)) I have currently all the alphabets of the dictionary.

What is the simplest way to delete the entry for S and shift rest of the values to the left.

If there is some other way to create this dictionary do tell.....


I am also in need of this..... Now I get a number from user..... This number in the dictionary should be assigned S and all the other dictionary items can be readjusted....

ie say the user gives me 3 the dictionary should look like

0- A 1- B 2- C 3- S and rest follow from D to Z without S.......

Please help..... Thanks a lot....

Upvotes: 1

Views: 261

Answers (5)

martineau
martineau

Reputation: 123501

If I understand you properly, the simplest thing to do seems like it would be to first create a list with the letters in the order you want, and then convert that into a dictionary:

import string

sans_S = [c for c in string.ascii_uppercase if c is not 'S']
user_choice = 3
alphabet = sans_S[0:user_choice] + ['S'] + sans_S[user_choice:]

print alphabet
# ['A', 'B', 'C', 'S', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
#  'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

# now create dictionary using modified list
alphadict = dict((x, i) for i, x in enumerate(alphabet))

# print out alphadict sorted by item values (not a necessary part of answer)
revdict = dict( (v,k) for k,v in alphadict.iteritems() )
print '{',
for v in sorted(alphadict.itervalues()):
    print "%r:%2d," % (revdict[v], v),
print '}'

# { 'A': 0, 'B': 1, 'C': 2, 'S': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8,
#   'I': 9, 'J':10, 'K':11, 'L':12, 'M':13, 'N':14, 'O':15, 'P':16, 'Q':17,
#   'R':18, 'T':19, 'U':20, 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25, }

Upvotes: 0

Sven Marnach
Sven Marnach

Reputation: 602355

If you want to do operations like the one in your update to the question, I would store the data in a list instead of a dictionary right from the beginning:

l = list(string.ascii_uppercase)
l.remove('S')
print l
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
l.insert(3, 'S')
print l
['A', 'B', 'C', 'S', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

The letters can now be accessed by there indices just as if they were in a dictionary.

Upvotes: 0

Kabie
Kabie

Reputation: 10663

for Q2

def makealphabet(i):
    a=list(string.ascii_uppercase)
    a[i:i]=a.pop(a.index('S'))
    return ''.join(a)

Upvotes: 0

Rosh Oxymoron
Rosh Oxymoron

Reputation: 21065

alphadict = dict((x, i) for i, x in enumerate(string.ascii_uppercase) if x != 'S')

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838896

The simplest way is to remove the letter 'S' before you create the dictionary.

Use string.ascii_uppercase.replace('S', '') instead of string.ascii_uppercase.

Upvotes: 3

Related Questions