Python中文
首页
教程
问答
标签
搜索
登录
注册
map(lambda,list)返回None而不是obj
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我想把属性添加到列表中的所有对象(django中的模型实例)。例如,列表为:</p> <pre><code>persons = [ {"age":"12","name":"John"}, {"age":"10","name":"Joye"} ] </code></pre> <p>我们假设我想把<code>person.age</code>的值放在<code>person.number</code>中,我有一个新的列表,所有人都有新的属性<code>number</code>,所以我的函数是:</p> <pre><code>new_persons_list=list(map(lambda person:setattr(person, 'number', person.age), persons)) </code></pre> <p>但是输出是<code>[None,None]</code>,而我期望输出是:</p> <pre><code>persons = [ {"age":12,"name":"John","number":12}, {"age":10,"name":"Joye","number":10} ] </code></pre> <p>如果可能的话,我不想在<code>map</code>中添加新函数,我想使用拉姆达。什么有问题吗?谢谢</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>这没那么容易,所以使用:</p> <pre><code>new_persons_list=list(map(lambda person:{**person.to_dict(), "number": person.age}, persons)) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
文本导入时标题行中的特殊字符
4 回答
文本小部件:在没有输入时更新并在循环后保持空闲
8 回答
文本小部件tkin
3 回答
文本小部件tkinter中的标签更改或文本外观更改是否有撤消功能?
4 回答
文本小部件tkinter复制图像选项
7 回答
文本小部件上的Python Tkinter ttk滚动条未缩放
8 回答
文本小部件上的滚动条可能需要根据制表符ord显示前进行滚动
9 回答
文本小部件不显示lis中的内容
6 回答
文本小部件不显示Unicode字符
5 回答
文本小部件中写入的行间距
10 回答
文本小部件中的文本作为变量
2 回答
文本小部件中的滚动条仅显示在底部
5 回答
文本小部件中的选项卡键空间计数
4 回答
文本小部件作为Lis
9 回答
文本小部件在主框架中扩展列宽
4 回答
文本小部件未使用删除功能清除
7 回答
文本小部件滚动动画(Tkinter、Python)
4 回答
文本居中。格式正确吗?
5 回答
文本差分算法
8 回答
文本已知时音频文件中的单词索引
6 回答