2

I have two list

priceI = ['2,', '1,', '3,', '1,']
PirceII = ['49', '99', '19', '09']

I want to create a new list

Price = ['2,49', '1,99', '3,19', '1,09']

How can I realise this? And how can I realise this with Numpy?

I also want to calculate after with these prices, do I need to convert the , with a decimal . ?

Thanks for your help!

hpaulj
  • 201,845
  • 13
  • 203
  • 313
RobB
  • 373
  • 1
  • 2
  • 11
  • For clarification: your first list contains the full dollars/euro/whatever and the second list contains the decimals (cents/whatever) of the price? And you want (in the end) a numpy array with the correct price? – FlyingTeller Jul 09 '19 at 11:07
  • `Price = [float(m.replace(",", ".")+n) for m,n in zip(priceI, PirceII)]` ? – Rakesh Jul 09 '19 at 11:08
  • Thanks for your quick response! – RobB Jul 10 '19 at 09:21

5 Answers5

3

One way would be to zip both lists and map with list.join:

list(map(''.join, zip(priceI, PirceII)))
# ['2,49', '1,99', '3,19', '1,09']

To replace the commas with . start with:

priceI = [i.rstrip(',') for i in priceI]
list(map('.'.join, zip(priceI, PirceII)))
# list(map(float,(map('.'.join, zip(priceI, PirceII))))) # to cast to float
# ['2.49', '1.99', '3.19', '1.09']
yatu
  • 80,714
  • 11
  • 64
  • 111
  • 1
    do not forget to `str.replace` the comma with a period – WiseDev Jul 09 '19 at 11:10
  • You're welcome @RobB don' forget you can upvote and accept answers, see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – yatu Jul 10 '19 at 09:23
2

zip the 2 list then concatinate them:

[x + y for x, y in zip(priceI, PirceII)]
#-->['2,49', '1,99', '3,19', '1,09']
hichem jedidi
  • 121
  • 1
  • 3
1

Using map and lambda

rstrip-Return a copy of the string with trailing characters removed.

Ex.

priceI = ['2,', '1,', '3,', '1,']
PirceII = ['49', '99', '19', '09']
Price = list(map(lambda x, y:float("{}.{}".format(x.rstrip(','),y)), priceI, PirceII))
print(Price)

O/P:

[2.49, 1.99, 3.19, 1.09]
bharatk
  • 3,964
  • 5
  • 13
  • 28
0

You can use zip to loop multiple list

 priceI = ['2,', '1,', '3,', '1,']
 PirceII = ['49', '99', '19', '09']
 result=['{}{}'.format(p1,p2) for p1,p2 in zip(priceI,PirceII)]
 print(result)

Result is

['2,49', '1,99', '3,19', '1,09']

and for further use just loop through list and use some split method for future operation

0
     Price =[priceI[i]+j for i,j in enumerate(PirceII)]
     # Price=['2,49', '1,99', '3,19', '1,09']
Tulasi
  • 51
  • 2
  • 1
    please add context. Now your question is likely to be flagged by bots for removal due to pure code. – ZF007 Jul 10 '19 at 09:20