Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #57636

.Re: Search for mapping solution

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject .Re: Search for mapping solution
Date 2017-04-20 06:58 +0000
Organization Aioe.org NNTP Server
Message-ID <od9m66$198u$1@gioia.aioe.org> (permalink)

Show all headers | View raw


> > 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}


[['fred','333','0,10'],['sam','444','1'],['fred','333','0,50']].
  reduce(Hash.new(0)){|hash,(name,_,cost)|
    hash[name] += cost.sub(',','.').to_f; hash}

  ===>
{"fred"=>0.6, "sam"=>1.0}


In Forth?


-- 
Africans gang-rape and clitorectomize Finnish girl; government arrests Finn
whom they accuse of complaining:  http://www.wvwnews.net/story.php?id=6746

Back to comp.lang.forth | Previous | Next | Find similar | Unroll thread


Thread

.Re: Search for mapping solution "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-20 06:58 +0000

csiph-web