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
  • 221,503
  • 14
  • 230
  • 353
RobB
  • 383
  • 1
  • 2
  • 12
  • 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
  • 86,083
  • 12
  • 84
  • 139
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
  • 141
  • 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
  • 4,202
  • 5
  • 16
  • 30
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
  • 59
  • 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