简体   繁体   English

列表解析,将2个值从字典列表传递给函数

[英]List comprehensions, passing 2 values to a function from a list of dictionaries

I have a data structure: 我有一个数据结构:

my_list = [0] {key1: [1, 2, 3]
               key2: [4, 5, 6]
               key3: .....
               key4: .....}

          [1] {key1: [.......]
               key2: [... etc.

That is, a list of 4 dictionaries, each dictionary having 4 keys and each key a list of 3 values. 也就是说,列出了4个词典,每个词典有4个键,每个键有3个值的列表。 Nice and consistent. 尼斯和一致。

I want to loop through each value in each list and update it with an external function. 我想遍历每个列表中的每个值,并使用外部函数对其进行更新。 This external function takes 2 arguments (the value I'm updating and the float value contained in its respective key). 这个外部函数接受2个参数(我正在更新的值和其各自键中包含的浮点值)。 It's a basic bit of math but the problem is in iterating through the files as it is getting complex and I'm getting lost. 这是数学的基本知识,但是问题在于遍历文件变得越来越复杂,我正迷路。

What I have done so far: 到目前为止,我所做的是:

def Efunction_conversion(my_list):
    converted_dict_list = []
    for i in range(0,4):
        new_dict = {key:[external_function(float(key), value) for key, value in my_list[i].iteritems()]} ##problems occur here
        converted_dict_list.append(new_dict)
    return converted_dict_list

The code is not working and it may be obvious to others why. 该代码无法正常工作,对于其他人来说为什么可能很明显。

The external function: 外部功能:

def external~_function(key, value):
    E = ((value - key)/key)**2
    return E

And the error, TypeError: unsupported operand type(s) for -: 'list' and 'float' 错误TypeError: unsupported operand type(s) for -: 'list' and 'float'

So the line main iteration line is passing a list instead of each element it seems. 因此,该行的主要迭代行正在传递一个列表,而不是它似乎显示的每个元素。

Inside your for loop, you are creating the dict with only a single key, and the looping over of the keys is happening only for the list comprehension - 在for循环内,您仅用一个键创建字典,而键的循环仅对于列表理解才发生-

[external_function(float(key), value) for key, value in my_list[i].iteritems()]

It is not happening for the dict as such. 字典本身并没有发生。 also, If I am not wrong, value is a list, so you are passing the whole list as parameter to the external function, which may not be what you want. 另外,如果我没看错, value是一个列表,因此您要将整个列表作为参数传递给外部函数,这可能不是您想要的。

A simple way to do this would be (for Python 2.7+ with dictionary comprehension) - 一种简单的方法是(对于具有字典理解功能的Python 2.7+)-

def Efunction_conversion(my_list):
    converted_dict_list = []
    for x in my_list:
         converted_dict_list.append({key:[external_function(float(key),y) for y in value] for key, value in x.iteritems()}
    return converted_dict_list

A one liner for this would be - 一个班轮是-

def Efunction_conversion(my_list):
    return [{key:[external_function(float(key),y) for y in value] for key, value in x.iteritems()} for x in my_list]

The problem is with your external function you could change it to this: 问题在于您的外部函数,您可以将其更改为:

def external_function(key, value): 
    E=0.0
    for v in value:
        E += ((v- key)/key)**2 
    return E

If you want it as a list: 如果您希望将其作为列表:

def external_function(key, value): 
    E=[]
    for v in value:
        E.append( ((v- key)/key)**2 )
    return E

your external_function required a float/int but it got a list so the error was thrown 您的external_function需要float / int但它有一个列表,因此引发了错误

If you are having a trouble writing/understanding a list/dict comprehension, indent it first before trying to write a one-liner: 如果您在编写/理解列表/字典理解时遇到麻烦,请先缩进它,然​​后再尝试编写单线:

my_list = [
    {'key1': [1, 2, 3],
     'key2': [4, 5, 6],}
]

def external_function(key, value):
    return key + str(value)

other_list = [
    {
        k: [external_function(k, el) for el in v]
        for k, v in d.iteritems()
    }
    for d in my_list
]

[{'key1': ['key11', 'key12', 'key13'], 'key2': ['key24', 'key25', 'key26']}]

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

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