Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #57636 > unrolled thread
| Started by | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| First post | 2017-04-20 06:58 +0000 |
| Last post | 2017-04-20 06:58 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.forth
.Re: Search for mapping solution "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-20 06:58 +0000
| From | "Robert L." <No_spamming@noWhere_7073.org> |
|---|---|
| Date | 2017-04-20 06:58 +0000 |
| Subject | .Re: Search for mapping solution |
| Message-ID | <od9m66$198u$1@gioia.aioe.org> |
> > 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 top | Article view | comp.lang.forth
csiph-web