heisenberg3008
heisenberg3008

Reputation: 65

How to access value from a list and link two lists in python

I have been learning fundamentals of python and gotstuck on this problem : enter image description here

The code I could write so far :

def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity):
    bill_amount=0
    
    for i in reqd_gems:
        if(i=="Emerald"):
            bill_amount=____*1760
    return bill_amount

#List of gems available in the store
gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"]

#Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list=[1760,2119,1599,3920,3999]

#List of gems required by the customer
reqd_gems=["Ivory","Emerald"]

#Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity=[3,2]

bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)

I am unable to fetch the value from reqd_quantity and link it to the exact reqd_gem. Can someone pls help me understand the logic or provide the logic(so I can understand it by myself using the debugger). Do not need the logic for entire ques, but only how to link the quantity to that exact gem itself.

Upvotes: 1

Views: 319

Answers (5)

Zahid Adeel
Zahid Adeel

Reputation: 288

You can do something like this:


def calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity):
    gems_rate_mapping = {}
    for gem, price in zip(gems_list, price_list):
        gems_rate_mapping[gem] = price

    calculated_bill = 0
    for gem, qty in zip(reqd_gems, reqd_quantity):
        gem_price = gems_rate_mapping.get(gem)
        calculated_bill += qty * gem_price

    # apply 5% discount above 30k sale
    discount = 0
    if calculated_bill > 30000:
        discount = calculated_bill * (0.05)
    print(f"discount: {discount}")
    bill = calculated_bill - discount

    return bill


#List of gems available in the store
gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"]

#Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list=[1760,2119,1599,3920,3999]



#List of gems required by the customer
reqd_gems=["Ivory","Emerald"]

#Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity=[3,2]

bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity)
print(bill_amount)

Upvotes: 1

Hesam Korki
Hesam Korki

Reputation: 179

You can link each gem with its quantity using their list index:

for gem in requested_gems:
    if gem == ‘Emerald’:
        bill_amount = req_quantity[requested_gems.index(gem)] * 1760

This will solve your problem. However, don’t get accustomed to these types of solutions. One of the first steps for solving any problem is figuring out which data structure to use. I know you’re in the process of learning but a good data structure for this problem is considering an object for each purchase that has different attributes.

Upvotes: 1

Pi-R
Pi-R

Reputation: 654

If it can help : You can use gems_list.index('name_of_gem') to find the position of your reqd_gems then you have access to the price.

Upvotes: 2

Anant Kumar
Anant Kumar

Reputation: 641

Please refer the following, use dictionary to quickly solve the problem -

gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"]
price_list=[1760,2119,1599,3920,3999]
price_dict=dict(zip(gems_list, price_list))

reqd_gems=["Ivory", "Emerald"]
reqd_qty=[3,2]
reqd_dict=dict(zip(reqd_gems, reqd_qty))

total=0
for gem in reqd_gems:
    total+=price_dict[gem]*reqd_dict[gem]

Upvotes: 1

Andreas
Andreas

Reputation: 9217

#List of gems available in the store
gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"]
#Price of gems available in the store. gems_list and price_list have one-to-one correspondence
price_list=[1760,2119,1599,3920,3999]

# create dictionary based on both lists since there is a 1:1 relationship without duplicates
gem_dict = {gem_name:gem_price for (gem_name, gem_price) in zip(gems_list, price_list)}

#List of gems required by the customer
reqd_gems=["Ivory","Emerald"]
#Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence
reqd_quantity=[3,2]

# create dictionary based on both lists since there is a 1:1 relationship without duplicates
purchase_dict = {gem_name:gem_amount for (gem_name, gem_amount) in zip(reqd_gems, reqd_quantity)}

# now you can get the price from the gem_dict by simply using .get("NameOfGem")
total_normal = sum([gem_dict.get(gem_name)*gem_amount for (gem_name, gem_amount) in purchase_dict.items()])

Upvotes: 1

Related Questions