简体   繁体   English

构建此词典的最pythonic方法是什么?

[英]What's the most pythonic way to build this dictionary?

I want to create a dictionary such that letters in the first row of a QWERTY keyboard are given a value of one, keys in the home row are given a value of zero, and keys in the bottom row are given a value of two. 我想创建一个字典,使QWERTY键盘第一行中的字母的值为1,主行中的键的值为0,而底部行的键的值为2。

I wanted to do this is one line, but I can't think of a way to do this. 我想做到这一点,但我想不出一种方法。

I know this will do the first row, but is there a way to get all of the rows in a single line? 我知道这会做第一行,但是有没有办法将所有行都放在一行中呢?

firstRow = {i:1 for i in "q w e r t y u i o p".split(' ')}

Edit, I thought of this: 编辑,我想到了这一点:

keyboardRating = dict({i:1 for i in "q w e r t y u i o p".split(' ')}.items() + {i:0 for i in "a s d f g h j k l".split(' ')}.items() + {i:2 for i in "z x c v b n m".split(' ')}.items())

but I'm looking for something much simpler. 但我正在寻找更简单的方法。

Well, first, there's this neat trick for getting the firstRow : 好吧,首先,有一个巧妙的技巧可以获取firstRow

firstRow = dict.fromkeys('qwertyuiop', 1)

Then we can just write 然后我们可以写

keys = {}
keys.update(dict.fromkeys('qwertyuiop', 1))
keys.update(dict.fromkeys('asdfghjkl', 2))
keys.update(dict.fromkeys('zxcvbnm,', 3))

Alternatively, you can try something like this: 另外,您可以尝试如下操作:

rows = {1: 'qwertyuiop', 0: 'asdfghjkl;', 2: 'zxcvbnm,.'}
keys = {c:k for k,row in rows.iteritems() for c in row}

Because of the amount of static data involved, I don't recommend trying to put it all on one line. 由于涉及的静态数据量大,所以我不建议您尝试将所有数据都放在一行上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM