Вопрос

I have a list namely:

scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"),
               (4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")]

I have to write a function which gives a dict which contains a letter to score mapping whenever the above list is passed.please help me writing the function...!!

Это было полезно?

Решение

Dict comprehension with two for loops to the rescue:

letter_score = {letter: score for score, letters in scrabble_scores
                              for letter in letters.split()}

E.g. for every letter in the string (separated by whitespace), produce a key and value pair in the output dictionary; the key is the letter, the value the score.

Demo:

>>> scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"),
...                (4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")]
>>> letter_score = {letter: score for score, letters in scrabble_scores
...                               for letter in letters.split()}
>>> letter_score
{'A': 1, 'C': 3, 'B': 3, 'E': 1, 'D': 2, 'G': 2, 'F': 4, 'I': 1, 'H': 4, 'K': 5, 'J': 8, 'M': 3, 'L': 1, 'O': 1, 'N': 1, 'Q': 10, 'P': 3, 'S': 1, 'R': 1, 'U': 1, 'T': 1, 'W': 4, 'V': 4, 'Y': 4, 'X': 8, 'Z': 10}
>>> letter_score['Q']
10

Bonus word score calculator:

>>> word = 'QUICK'
>>> sum(letter_score[c] for c in word)
20

where word is a uppercase string containing only (scrabble) letters, ignoring double- and triple-letter scoring.

Другие советы

Another, more verbose way:

def makeScoreDict(scrabble_scores):
        score_dict = {}
        for row in scrabble_scores:
            for letter in row[1].split():
                score_dict[letter] = row[0]
        return score_dict

In python, you can loop over pairs like this:

for score, letters in scrabble_scores:
    print("{}: {}".format(letters, score))

This will give as result:

"E A O I N R T L S U": 1
"D G": 2
...

In python, you can splits string containing spaces on those spaces:

>>> "E A O I N R T L S U".split()
['E', 'A', 'O', 'I', 'N', 'R', 'T', 'L', 'S', 'U']

Now just combine the two:

for score, letters in scrabble_scores:
    for letter in letters.split():
        # voila, you've got score and letter.

Is this what you want? Seems to be what you describe.

scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"),
               (4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")]
out_dict = {} 
for score in scrabble_scores:
    val = score[0]
    for letter in score[1].split():
        out_dict[letter] = val
print out_dict

A non-dict comprehension answer.

scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"),
           (4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")]

d = dict()
for value,letters in scrabble_scores:
    d.update(dict(zip(letters.split(),[value]*len(letters.split())))

I would definitely do this as a dict comp as well, and I'm sure that's what OP is going for, but dict(zip(iter,iter)) is a useful expression worth mentioning.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top