Python 如何将函数应用于名为tuple的集合中的所有元素
我需要为名为tuple的集合的每个元素应用一个函数Python 如何将函数应用于名为tuple的集合中的所有元素,python,python-collections,Python,Python Collections,我需要为名为tuple的集合的每个元素应用一个函数 Product = collections.namedtuple('Product', [ 'ticker_symbol', 'entity', 'unique_id', 'as_of_date', 'company_name', 'followers', 'items', 'linkedin', 'industry', 'date_added', 'date_updated', 'descrip
Product = collections.namedtuple('Product', [
'ticker_symbol', 'entity', 'unique_id', 'as_of_date', 'company_name',
'followers', 'items', 'linkedin', 'industry', 'date_added',
'date_updated', 'description', 'website', 'sector', 'ticker_industry'
])
response_rows = response_items.get('rows')
for response_row in response_rows:
logging.info(response_row)
results.append(Company(*response_row))
return results
我希望将以下函数应用于Company namedtuple中的每个元素:
def _ToString(value):
if not value:
logging.warning('Empty value')
return None
if isinstance(value, unicode):
return value.encode('utf-8')
else:
return str(value)
我最后用了这句话:
results.append(Company(*[_ToString(x) for x in response_row]))
哦,你好,我在那里看到了我的一些代码。你试过什么吗?你试过迭代元组并应用这个函数吗?我想检查是否有一个函数类似于pandas中的apply function to column。我只是迭代了results.append(Product(*[[u ToString(x)for x in response_row]),这样就有了一个解决方案。这个解决方案有什么问题?如果您询问有关
namedtuples
上可用的方法,可以查看。但是你找不到这样的方法。