Python中文
首页
教程
问答
标签
搜索
登录
注册
如何更改列表中字典中特定键的值类型?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<pre><code>list = [ {'rank': '1', 'name': 'Gus’s World Famous Fried Chicken', 'stars': '4.0', 'numrevs': '549', 'price': '$$'}, {'rank': '2', 'name': 'South City Kitchen - Midtown', 'stars': '4.5', 'numrevs': '1777', 'price': '$$'} ] </code></pre> <p>我需要将<code>numrevs</code>更新为<code>int</code></p> <p>到目前为止,我有这个,但我无法让它工作:</p> <pre><code>list = [dt.update({k : int(v)}) for dt in rankings for k['numrevs'], v in dt.items()] </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>对于列表中的每个dict,创建一个新dict,其中每个键的值与当前dict相同,但要用int替换的值除外</p> <p>这里我调用当前列表<code>list1</code>,以避免变量名和内置类型名之间的混淆</p> <pre><code>list2 = [ {k:(int(v) if k=='numrevs' else v) for k,v in d.items()} for d in list1 ] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python猜字gam
5 回答
Python猜字游戏
9 回答
Python猜字游戏?
7 回答
Python猜字游戏不会在玩家猜测所有字母时结束
1 回答
Python猜字游戏在有重复字符的单词上失败
9 回答
Python猜想
6 回答
Python猜数字游戏,而循环没有响应
1 回答
python猜测gam的代码反馈
2 回答
python猜测gam的变量问题
5 回答
python猜测具体路径?
5 回答
Python猜测我的号码(反向号码猜测)UnboundLocalE
9 回答
python猜测该数字在猜测过度后再试一次
2 回答
Python猜猜单词游戏bug
10 回答
Python猜猜游戏如何再现
1 回答
Python猜猜游戏服务器
1 回答
Python猜猜谁的游戏我没有得到我预期的输出
10 回答
Python猜谜游戏
9 回答
Python猜谜游戏,值错误:randrange()的空范围
2 回答
Python猜谜游戏,带尝试选项
6 回答
Python猜谜游戏,我的赢的条件打印赢和输
5 回答