I have this (which works):
我有这个(可以用的):
def make_key(x):
return "{0}/{1}/{2}".format(x.f1, x.f2, x.f3)
def make_value(x):
return (x.f7, x.f1, x.f4, x.f9,
x.f2, x.f8, x.f10, x.f11, x.f12, x.f17,
x.f18, x.f19, x.f14, x.f15,
x.f16, x.f17, x.f20)
row_data = {}
for x in records:
key = make_key(x.f1,x.f2,x.f3)
value = make_value(x)
row_data[key] = value
I tried this:
我试着这样的:
row_data = dict([(make_key(x), make_value(x))] for x in records)
I get this error:
我得到这个错误:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Edit:
编辑:
records
is a list of objects that have field properties f1, ...fn
记录是具有字段属性f1,…fn的对象的列表
2 个解决方案
#1
5
You need to drop the list wrapper; just produce the tuples:
您需要删除列表包装;只是生产元组:
row_data = dict((make_key(x), make_value(x)) for x in records)
You were producing a list with one element each, a tuple.
您正在生成一个列表,每个元素都有一个元组。
If you are using Python 2.7 or newer, you can also use a dictionary comprehension:
如果您使用的是Python 2.7或更新版本,还可以使用字典理解:
row_data = {make_key(x): make_value(x) for x in records}
Your make_value
could be expressed by using a operator.itemgetter()
object:
您的make_value可以通过使用operator.itemgetter()对象来表示:
from operator import itemgetter
make_value = itemgetter(
'f7', 'f1', 'f4', 'f9', 'f2', 'f8', 'f10', 'f11', 'f12', 'f17',
'f18', 'f19', 'f14', 'f15', 'f16', 'f17', 'f20')
The make_key()
function can make use of the fact that you can pull out attributes from an object; {0.f1}
would interpolate the f1
attribute of the first positional argument to the str.format()
method; make use of this to create a bound str.format()
method that takes just one positional argument:
make_key()函数可以利用从对象中提取属性的事实;{ 0。f1}将第一个位置参数的f1属性插入到string .format()方法;使用这个方法创建一个绑定的str.format()方法,它只需要一个位置参数:
make_key = '{0.f1}/{0.f2}/{0.f3}'.format
#2
0
A few different things you can do to make it simpler :)
你可以做一些不同的事情来简化它:)
No need for a separate make_key function definition, you already have one:
不需要单独的make_key函数定义,您已经有了一个:
make_key = '{0.f1}/{0.f2}/{0.f3}'.format
Beyond that, depending on your Python version you can also use dict comprehensions:
除此之外,根据您的Python版本,您还可以使用字典理解:
row_data = {make_key(x): make_value(x) for x in records}
#1
5
You need to drop the list wrapper; just produce the tuples:
您需要删除列表包装;只是生产元组:
row_data = dict((make_key(x), make_value(x)) for x in records)
You were producing a list with one element each, a tuple.
您正在生成一个列表,每个元素都有一个元组。
If you are using Python 2.7 or newer, you can also use a dictionary comprehension:
如果您使用的是Python 2.7或更新版本,还可以使用字典理解:
row_data = {make_key(x): make_value(x) for x in records}
Your make_value
could be expressed by using a operator.itemgetter()
object:
您的make_value可以通过使用operator.itemgetter()对象来表示:
from operator import itemgetter
make_value = itemgetter(
'f7', 'f1', 'f4', 'f9', 'f2', 'f8', 'f10', 'f11', 'f12', 'f17',
'f18', 'f19', 'f14', 'f15', 'f16', 'f17', 'f20')
The make_key()
function can make use of the fact that you can pull out attributes from an object; {0.f1}
would interpolate the f1
attribute of the first positional argument to the str.format()
method; make use of this to create a bound str.format()
method that takes just one positional argument:
make_key()函数可以利用从对象中提取属性的事实;{ 0。f1}将第一个位置参数的f1属性插入到string .format()方法;使用这个方法创建一个绑定的str.format()方法,它只需要一个位置参数:
make_key = '{0.f1}/{0.f2}/{0.f3}'.format
#2
0
A few different things you can do to make it simpler :)
你可以做一些不同的事情来简化它:)
No need for a separate make_key function definition, you already have one:
不需要单独的make_key函数定义,您已经有了一个:
make_key = '{0.f1}/{0.f2}/{0.f3}'.format
Beyond that, depending on your Python version you can also use dict comprehensions:
除此之外,根据您的Python版本,您还可以使用字典理解:
row_data = {make_key(x): make_value(x) for x in records}