快速提问。我找不到用于从for循环创建对象的语法(假设它存在)。这样做的正确方法是什么 -
letters = ['a', 'b', 'c']
objs = (letter[index]:index for letter, index in letters)
#Also tryed..
objs = ({letter[index]:index} for letter, index in letters)
有什么想法吗?
答案 0 :(得分:2)
coffee> letters = ['a', 'b', 'c']
[ 'a', 'b', 'c' ]
coffee> lettersToIndex = {}
{}
coffee> lettersToIndex[letter] = index for letter, index in letters
[ 0, 1, 2 ]
coffee> lettersToIndex
{ a: 0, b: 1, c: 2 }