Python Forum
Subtracting values between two dictionaries/ floating point numbers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subtracting values between two dictionaries/ floating point numbers
#6
Dictionary do not give you a free pass on line length Wink
Here is formatted code,i use VS Code and black set to format code.
So i not done all this manually.
import operator

#### taking user information
user_purchase_location = 'Mall'
user_sale_destination = 'Gold Shop'

#### dicts of items available to buy and sell at locations along with UEC price.
buying_olisar = {"Medical Supply": 17.01}

selling_olisar = {
    "Agricium": 25.60,
    "Aluminum": 1.25,
    "Beryl": 4.26,
    "Chlorine": 1.57,
    "Corundum": 2.53,
    "Diamond": 6.90,
    "Distilled Spirits": 4.95,
    "Fluorine": 2.80,
    "Gold": 6.07,
    "Hydrogen": 1.02,
    "Iodine": 0.41,
    "Laranite": 28.91,
    "Processed Food": 1.39,
    "Quartz": 1.44,
    "Scrap": 1.67,
    "Stims": 3.40,
    "Titanium": 8.27,
    "Tungsten": 3.90,
}

buying_levski = {
    "Agricultural Supply": 1.11,
    "Aluminum": 1.20,
    "Hydrogen": 0.98,
    "Iodine": 0.38,
    "Quartz": 1.37,
}
selling_levski = {
    "Agricium": 25.60,
    "Altruciatoxine": 11.63,
    "Beryl": 4.25,
    "Chlorine": 1.56,
    "Corundum": 2.53,
    "Diamond": 6.07,
    "Distilled Spirits": 4.95,
    "Fluorine": 2.80,
    "Gold": 6.07,
    "Laranite": 28.25,
    "Medical Supply": 18.00,
    "Processed Food": 1.38,
    "Scrap": 1.68,
    "Stims": 3.40,
    "Titanium": 8.27,
    "Tungsten": 3.90,
    "Widdow": 24.00,
}
buying_grim_hex = {
    "Chlorine": 1.44,
    "Flurine": 2.63,
    "Hydrogen": 0.97,
    "Iodine": 0.34
    }

selling_grim_hex = {
    "Altruciatoxine": 11.80,
    "distilled spirits": 4.95,
    "medical suppies": 18.05,
    "processed food": 1.36,
    "stims": 3.40,
    "Widdow": 24.0,
}

#### Sorting the dict into ascending value instead of alphabetical
sort_buying_olisar = sorted(buying_olisar, key=lambda tup: tup[1])
sort_selling_olisar = sorted(selling_olisar, key=lambda tup: (tup[1], tup[0]))
sort_buying_levski = sorted(buying_levski, key=lambda tup: tup[1])
sort_selling_levski = sorted(selling_levski, key=lambda tup: (tup[1], tup[0]))
#sort_buying_grim_hex = sorted(buying_arc_corp_mining_area_157, key=lambda tup: tup[1])
#sort_selling_grim_hex = sorted(selling_arc_corp_mining_area_157, key=lambda tup: (tup[1], tup[0]))

#### picking two dicts (buy/sell) and filtering them to print only common elements.
def common(curr, other):
    return set(curr.keys()).intersection(set(other.keys()))

tradable_items = common(buying_levski, selling_olisar)

#### calculating the prices between possible sales.
prices = {k: selling_olisar[k] - buying_levski[k] for k in tradable_items}

most_profitable_resourse = max(prices.items(), key=operator.itemgetter(1))[0]

#### sorting and printing the most profitable purchase.
profit_ammount = prices
profit_sorted = f"{profit_ammount[most_profitable_resourse]:.2f}"
print(
    f"The most profitable item to buy at {user_purchase_location.title()} is {most_profitable_resourse.title()}\n\
and will make a net profit of {profit_sorted} per unit when sold at {user_sale_destination.title()}"
)
Now it work for me,i have comment out line 78,79 as eg buying_arc_corp_mining_area_157 is not set.

Next time do not post all together working and not working code it can be confusing,
make a working example with eg Dictionary and code that gives problem with Traceback.

Here my test run:
E:\div_code\xy
λ ptpython -i test1.py
# The out from running code
The most profitable item to buy at Mall is Quartz
and will make a net profit of 0.07 per unit when sold at Gold Shop

# Interactive session to test some values 
>>> tradable_items
{'Iodine', 'Aluminum', 'Hydrogen', 'Quartz'}

>>> most_profitable_resourse
'Quartz'

>>> profit_sorted
'0.07'

>>> sort_selling_olisar
['Laranite', 'Scrap', 'Beryl', 'Agricium', 'Chlorine', 'Diamond', 'Distilled Spirits', 'Titanium', 'Aluminum', 'Fluorine', 'Corundum', 'Gold', 'Iodine', 'Processed Food', 'Stims', 'Quartz', 'Tungsten', 'Hydrogen']

>>> sort_buying_levski
['Agricultural Supply', 'Aluminum', 'Iodine', 'Quartz', 'Hydrogen']

>>> prices
{'Iodine': 0.02999999999999997, 'Aluminum': 0.050000000000000044, 'Hydrogen': 0.040000000000000036, 'Quartz': 0.06999999999999984}  
Reply


Messages In This Thread
RE: Subtracting values between two dictionaries/ floating point numbers - by snippsat - Mar-04-2019, 01:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with subtracting values using SQLite & Python Extra 10 3,414 May-10-2022, 08:36 AM
Last Post: ibreeden
  Subtracting datetimes [index] Mark17 2 2,482 Aug-21-2021, 12:11 AM
Last Post: Larz60+
  5 variants to invert dictionaries with non-unique values Drakax1 2 2,614 Aug-31-2020, 11:40 AM
Last Post: snippsat
  Accessing values in list of dictionaries pythonnewbie138 2 2,137 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  Indexing problem while iterating list and subtracting lbtdne 2 2,138 May-14-2020, 10:19 PM
Last Post: deanhystad
  floating point not increasing properly rakeshpe43 4 2,409 Apr-30-2020, 05:37 AM
Last Post: rakeshpe43
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,609 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Complex floating issue arshad 2 11,848 Nov-05-2019, 03:26 PM
Last Post: arshad
  floating point arithmetic exDeveloper 2 2,118 Sep-25-2019, 04:33 PM
Last Post: DeaD_EyE
  finding the closest floating point number in a list Skaperen 17 8,300 Sep-19-2019, 10:39 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020