在python中使用2个词典

时间:2016-02-11 10:49:14

标签: python python-2.7

我试图创建一个代码,如果用户在输入中输入他的名字和他的中间名,那么它会将两个字典数字加在一起。

对于我想要的名字值:

    d['a'] = 0
    d['b'] = 60
    d['c'] = 100
    d['d'] = 160
    d['e'] = 200
    d['f'] = 240
    d['g'] = 280
    d['h'] = 320
    d['i'] = 400
    d['j'] = 420
    d['k'] = 500
    d['l'] = 520
    d['m'] = 540
    d['n'] = 620
    d['o'] = 640
    d['p'] = 660
    d['q'] = 700
    d['r'] = 720
    d['s'] = 780
    d['t'] = 800
    d['u'] = 840
    d['v'] = 860
    d['w'] = 880
    d['x'] = 940
    d['y'] = 960
    d['z'] = 980

我想要的中间名初始值:

    d['a'] = 1
    d['b'] = 2
    d['c'] = 3
    d['d'] = 4
    d['e'] = 5
    d['f'] = 6
    d['g'] = 7
    d['h'] = 8
    d['i'] = 9
    d['j'] = 10
    d['k'] = 11
    d['l'] = 12
    d['m'] = 13
    d['n'] = 14
    d['o'] = 14
    d['p'] = 15
    d['q'] = 15
    d['r'] = 16
    d['s'] = 17
    d['t'] = 18
    d['u'] = 18
    d['v'] = 18
    d['w'] = 19
    d['x'] = 19
    d['y'] = 19
    d['z'] = 19
到目前为止

代码:

first_name = raw_input("what is your first name?: ")
middle_initial = raw_input("What is your middle initial?: ")

    #First Name Initial Values
    d = {}
    d['a'] = 0
    d['b'] = 60
    d['c'] = 100
    d['d'] = 160
    d['e'] = 200
    d['f'] = 240
    d['g'] = 280
    d['h'] = 320
    d['i'] = 400
    d['j'] = 420
    d['k'] = 500
    d['l'] = 520
    d['m'] = 540
    d['n'] = 620
    d['o'] = 640
    d['p'] = 660
    d['q'] = 700
    d['r'] = 720
    d['s'] = 780
    d['t'] = 800
    d['u'] = 840
    d['v'] = 860
    d['w'] = 880
    d['x'] = 940
    d['y'] = 960
    d['z'] = 980

    lower = first_name.lower()
    first_initial = lower[0]

    if first_initial in d:
        print d[first_initial]

示例:

例如,如果我输入Josh作为名字而J输入中间名,则输出应为430。

如果你对我如何得到430感到困惑,我将420和10加在一起因为420是' j'第一个名字的第一个首字母' josh'和10是' j'对于中间名首字母。

3 个答案:

答案 0 :(得分:2)

我建议将(dictzip用于两个列表:

from string import lowercase as abc

d1 = dict( zip(abc, [0, 60, 100, 160, ...]) )
d2 = dict( zip(abc, [1,2,3,4,5,...]) )

...

答案 1 :(得分:1)

首先为名字得分和中间名得分制作2个词典,

firstNameScore = {'a':0, 'b':60, 'c':100, and so forth}
middleNameScore = {'a':1, 'b':2, 'c':3, and so forth}
first_name = raw_input("what is your first name?: ")
middle_initial = raw_input("What is your middle initial?: ")
# Access the value of initials from dictionaries.
score = firstNameScore[first_name[0]] + middleNameScore[middle_initial]
print score

答案 2 :(得分:1)

d = {}
d['a'] = 0
d['b'] = 60
d['c'] = 100
d['d'] = 160
d['e'] = 200
d['f'] = 240
d['g'] = 280
d['h'] = 320
d['i'] = 400
d['j'] = 420
d['k'] = 500
d['l'] = 520
d['m'] = 540
d['n'] = 620
d['o'] = 640
d['p'] = 660
d['q'] = 700
d['r'] = 720
d['s'] = 780
d['t'] = 800
d['u'] = 840
d['v'] = 860
d['w'] = 880
d['x'] = 940
d['y'] = 960
d['z'] = 980

dict = {}
dict['a'] = 1
dict['b'] = 2
dict['c'] = 3
dict['d'] = 4
dict['e'] = 5
dict['f'] = 6
dict['g'] = 7
dict['h'] = 8
dict['i'] = 9
dict['j'] = 10
dict['k'] = 11
dict['l'] = 12
dict['m'] = 13
dict['n'] = 14
dict['o'] = 14
dict['p'] = 15 
dict['q'] = 15
dict['r'] = 16
dict['s'] = 17
dict['t'] = 18
dict['u'] = 18
dict['v'] = 18
dict['w'] = 19
dict['x'] = 19
dict['y'] = 19

first_name = raw_input("what is your first name inital?: ")
middle_initial = raw_input("What is your middle initial?: ")

if first_name in dict.keys():
  value_to_add  = dict.get(first_name)

if middle_initial in d.keys():
 next_value_to_add = d.get(middle_initial)

print value_to_add,next_value_to_add
values_added = value_to_add + next_value_to_add
print values_added

这就是你要找的东西我认为......只是你不能拥有2个同名字典,你可以检查和更改东西......你使用了数组([] =数组)({} =字典)

乐意帮忙!