[英]Passing named tuple field name for a ._replace as an argument from a function
So I have a named tuple, say:所以我有一个命名元组,说:
from collections import namedtuple
Symbol = namedtuple('Symbol', 'name code industry date_au open high low close volume weekday_au date_utc_unixtimestamp prev_volume', defaults = ('DEFAULT', 'DEF', 'DEFAULT', '01/01/1970', 0.0, 0.0, 0.0, 0.0, 0, 'Mon', 0, 0))
I make some test data:我做了一些测试数据:
ont_data = [
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '03/05/2020', open = 10.0, high = 12.0, low = 9.5, close = 11.0, volume = 1000, weekday_au = 'Sun', date_utc_unixtimestamp = 123),
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '15/05/2020', open = 12.0, high = 12.0, low = 5.5, close = 9.0, volume = 999, weekday_au = 'Fri', date_utc_unixtimestamp = 125),
Symbol(name = '1300 SMILES LIMITED', code ='ONT', industry = 'Tech', date_au = '17/01/2020', open = 4.0, high = 90.0, low = 54.5, close = 74.0, volume = 27, weekday_au = 'Wed', date_utc_unixtimestamp = 5)
]
And now I want to create a copy of this named tuple and do some replacements dynamically on a particular attribute:现在我想创建这个命名元组的副本,并对特定属性进行一些动态替换:
def set_prev_val(symbol, prev_values, field_to_update):
"""
Iterate over a copy of Symbol named tuple and replace some attribute values.
Return a copy of Symbol named tuple with replaced previous values of some field_to_update
args:
@symbol - A descending sorted named tuple representing stock symbol data
@prev_values - A list of previous values (from a descending sorted Symbol)
@field_to_update - A field in a Symbol named tuple to update
"""
symbol_copy = symbol
for row in range(0, len(symbol_copy)):
symbol_copy[row] = symbol[row]._replace(field_to_update = prev_values[row])
return symbol_copy
When I test this with say prev_values_data = [1,2,3]
I get:当我用
prev_values_data = [1,2,3]
测试这个时,我得到:
ValueError: Got unexpected field names: ['field_to_update']
Where as what I want is the prev_volume
fields to be updated with the values of prev_values_data
凡为我想是的
prev_volume
领域具有的值更新prev_values_data
I think instead of passing the string value of field_to_update
it is taking the actual name of the attribute in the named tuple as the name of the argument!!!我认为不是传递
field_to_update
的字符串值, field_to_update
将命名元组中属性的实际名称作为参数名称!!!
Is there a way for me to be able to pass some 'name' attribute to replace as an argument from a function to a ._replace
call in a named tuple?有没有办法让我能够将一些“名称”属性作为参数替换为从函数到命名元组中的
._replace
调用? (assuming the name
exists in the named tuple attribute names) (假设
name
存在于命名元组属性名称中)
Python 3.7蟒蛇 3.7
Create an anonymous dict
and unpack it:创建一个匿名
dict
并解压它:
symbol_copy[row] = symbol[row]._replace(**{field_to_update: prev_values[row]})
Normal keyword argument passing via field_to_update=prev_values[row]
is static;通过
field_to_update=prev_values[row]
传递的普通关键字参数是静态的; it's passing the left-hand side as a fixed string, not a variable (it doesn't even allow quoting it).它将左侧作为固定字符串传递,而不是变量(它甚至不允许引用它)。 But
dict
literal syntax would require quoting for a string key, so it treats field_to_update
as a variable, as expected.但是
dict
文字语法需要引用字符串键,因此它按预期将field_to_update
视为变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.