定义拼字字母分布的定义。

时间:2022-12-15 09:56:48

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...!!

我必须写一个函数,它给出了一个命令,它包含一个字母,在上面的列表通过时,它就可以进行映射。请帮我写这个功能…!!

5 个解决方案

#1


3  

Dict comprehension with two for loops to the rescue:

命令的理解与两个for循环的救援:

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.

如果单词是大写的字符串,只包含(scrabble)字母,忽略双字母和三字母的评分。

#2


1  

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

#3


0  

In python, you can loop over pairs like this:

在python中,可以像这样循环:

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:

在python中,可以在这些空间中分割包含空格的字符串:

>>> "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.

#4


0  

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

#5


0  

A non-dict comprehension answer.

non-dict理解答案。

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.

我当然也会这样做,我确定OP就是这么做的,但是dict(zip(iter,iter))是一个有用的表达式,值得一提。

#1


3  

Dict comprehension with two for loops to the rescue:

命令的理解与两个for循环的救援:

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.

如果单词是大写的字符串,只包含(scrabble)字母,忽略双字母和三字母的评分。

#2


1  

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

#3


0  

In python, you can loop over pairs like this:

在python中,可以像这样循环:

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:

在python中,可以在这些空间中分割包含空格的字符串:

>>> "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.

#4


0  

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

#5


0  

A non-dict comprehension answer.

non-dict理解答案。

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.

我当然也会这样做,我确定OP就是这么做的,但是dict(zip(iter,iter))是一个有用的表达式,值得一提。