搜索映射解决方案 [英] Search for mapping solution
问题描述
在帖子中说明,我是一名java程序员,着迷于优雅的方式python解决迭代问题。也许你可以给我一个解决方案如何映射
以下
我有一个清单:
名称 - 数量 - 费用
lines = [[''fred'',''333'',''0,10''],[''sam'','' 444'',''1'',[''fred'',''333'',''0,50'']
现在我想拥有它在字典中(名称,成本)应该看起来像
{''fred'':''0,60'','sam'':''1''}
这是一种优雅的方式吗?我可以使用很多循环,但我认为,
有更好的方法。
谢谢,
Markus
" Markus Joschko" <乔**** @ phreaker.net>在消息中写道
news:be ************ @ ID-47851.news.dfncis.de ...姓名 - 编号 - 费用
lines = [[''fred'',''333'',''0,10''],['sam'','''444'','' 1''],[''fred'',''333'',''0,50'']
现在我想把它放在字典里(名字,费用)应该看起来像
{''fred'':''0,60'','sam'':''1''}
我会这样做:
lines = [[''fred'',''333'',''0.10''],['sam'','' 444'',''1'',[''fred'',''333'',''0.50'']
费用= {}
代表姓名,编号,价格:
费用[name] = costs.setdefault(名称,0)+浮动(价格)
打印费用
Achim
Markus Joschko写道:
在帖子中说明因为,我M Java程序员,着迷优雅
方式蟒解决迭代。也许你可以给我看一个如何映射的解决方案
我有一个清单:
名称 - 数量 - 成本
lines = [[''fred'',''333'',''0,10''],['sam'',''444'',''1'',[''fred' ','''333'',''0,50'']
现在我想把它放在字典里(名字,成本)应该看起来像
{''fred '':''0,60'',''sam'':''1''}
这是一种优雅的方式吗?我可以使用很多循环,但我认为,这样做有更好的方法。
lines = [[''fred' , '333' '', '' 0,10 ''],[ '' SAM '', '' 444 '', '' 1 ''],[ '' fred的 '', '333' '', ''0,50'']]
费用= {}
名称,项目,行数:
费用[名称] = costs.setdefault(name,0.0)+
float(price.replace('','',''。''))
打印费用
{''fred'':0.59999999999999998,'sam'':1.0}
关于Max M
Markus Joschko< jo **** @ phreaker。净>写道:
[...]我有一个清单:
nName - 数字 - 费用
[...]lines = [[''fred'',''333'',''0,10''],[ '' SAM '', '' 444 '', '' 1 ''],[ '' fred的 '', '333' '', '' 0,50 '']]
[...]
请注意,元组是为那种''迷你对象''使用而设计的(和
不是主要用作不可变列表的。)
lines = [(''fred'',''333'',''0,10''),('' sam'','''444'',''1''),(''fred'',''333'',''0,50'')
当然如果你最终得到一个
3元素列表而不是3元组的列表,如果构建
列表很方便,那就不是灾难了拉链或其他什么。
John
Hi,
stated in a post befor, I''m a java programmer, fascinated about the elegant
way python solves iterations. Maybe you can show me a solution how to map
the following
I have a List:
Name - Number - Costs
lines = [[''fred'',''333'',''0,10''],[''sam'',''444'',''1''],[''fred'',''333'',''0,50'']]
Now I want to have it in a dictionary(name,costs) Should look like
{''fred'':''0,60'' , ''sam'':''1''}
What''s an elegant way to do it? I can use a lot of loops, but I assume, that
there is a better way of doing so.
Thanks,
Markus
"Markus Joschko" <jo****@phreaker.net> wrote in message
news:be************@ID-47851.news.dfncis.de...Name - Number - Costs
lines = [[''fred'',''333'',''0,10''],[''sam'',''444'',''1''],[''fred'',''333'',''0,50'']]
Now I want to have it in a dictionary(name,costs) Should look like
{''fred'':''0,60'' , ''sam'':''1''}
I would do it like this:
lines = [[''fred'',''333'',''0.10''],[''sam'',''444'',''1''],[''fred'',''333'',''0.50'']]
costs = {}
for name,number,price in lines:
costs[name] = costs.setdefault(name,0)+float(price)
print costs
Achim
Markus Joschko wrote:
Hi,
stated in a post befor, I''m a java programmer, fascinated about the elegant
way python solves iterations. Maybe you can show me a solution how to map
the following
I have a List:
Name - Number - Costs
lines = [[''fred'',''333'',''0,10''],[''sam'',''444'',''1''],[''fred'',''333'',''0,50'']]
Now I want to have it in a dictionary(name,costs) Should look like
{''fred'':''0,60'' , ''sam'':''1''}
What''s an elegant way to do it? I can use a lot of loops, but I assume, that
there is a better way of doing so.
lines = [[''fred'',''333'',''0,10''],[''sam'',''444'',''1''],[''fred'',''333'',''0,50'']]
costs = {}
for name, items, price in lines:
costs[name] = costs.setdefault(name, 0.0) +
float(price.replace('','',''.''))
print costs
{''fred'': 0.59999999999999998, ''sam'': 1.0}
regards Max M
Markus Joschko <jo****@phreaker.net> writes:
[...]I have a List:
nName - Number - Costs
[...]lines = [[''fred'',''333'',''0,10''],[''sam'',''444'',''1''],[''fred'',''333'',''0,50'']]
[...]
Note that tuples were designed for that sort of ''mini-object'' use (and
were not intended primarily as immutable lists).
lines = [(''fred'',''333'',''0,10''), (''sam'',''444'',''1''), (''fred'',''333'',''0,50'')]
though of course it''s no disaster if you end up with a list of
3-element lists instead of 3-tuples, if it''s convenient to build the
list with zip or whatever.
John
这篇关于搜索映射解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!