multiply keys of dictionary to each other - python

the dictionary I have is:
teachers = [
{'Name':'Mahdi Valikhani', 'phoneN':'+989012345679', 'hours':6, 'payment':50000, 'salaries':[2]*[3]},
{'Name':'Ali Afaghi', 'phoneN':'+989011234567', 'hours':8, 'payment':45000},
{'Name':'Hossein Alizadeh', 'phoneN':'+989011234867', 'hours':8, 'payment':45000},
]
and I want to somehow multiply hours to payment to have the salary!
I have tried multiplying but it gives me an error and the error says you can not multiply strings into integers!
help please!

First of all remove 'salaries':[2]*[3] from the first dict, and then run
If you want to update the existing dictionaries.
for t in teachers:
t["salary"] = t["hours"] * t["payment"]
Note: make sure hours and payment should be numeric, if you are not sure, then you can convert it
for t in teachers:
try:
t["salary"] = int(t["hours"]) * float(t["payment"])
except ValueError:
pass # fallback case

Iterate over each dict and compute salaries and add new key and value like below:
teachers = [
{'Name':'Mahdi Valikhani', 'phoneN':'+989012345679', 'hours':6, 'payment':50000},
{'Name':'Ali Afaghi', 'phoneN':'+989011234567', 'hours':8, 'payment':45000},
{'Name':'Hossein Alizadeh', 'phoneN':'+989011234867', 'hours':8, 'payment':45000},
]
for dct in teachers:
dct['salaries'] = dct['hours']*dct['payment']
print(teachers)
Output:
[{'Name': 'Mahdi Valikhani', 'phoneN': '+989012345679', 'hours': 6, 'payment': 50000, 'salaries': 300000},
{'Name': 'Ali Afaghi', 'phoneN': '+989011234567', 'hours': 8, 'payment': 45000, 'salaries': 360000},
{'Name': 'Hossein Alizadeh', 'phoneN': '+989011234867', 'hours': 8, 'payment': 45000, 'salaries': 360000}]

Related

How to check a substring in a string up until a certain occurrence of a character in python?

I receive orders that contain the name of customer, price of order and the quantity of the order.
The format of the order looks like this: {'first order': ['Alex', '100#2']}
(100 refers to a price and 2 refers to a quantity).
So I have different orders: {'first order': ['Alex', '99#2'], 'second order': ['Ann', '101#2'], 'third order': ['Nick', '110#3']}
We need to compare the prices and see which is the highest price and which is the lowest.
I was thinking of doing this by cutting the substring into two parts, the first part before the '#' symbol and the second part after the '#' symbol, then extract the numbers from the first part and compare them with others.
What's the most efficient way you can tell to solve this issue?
Thank you
I'd suggest to transform the dictionary to a list of dictionaries and convert the string to two floats. For example:
orders = {
"first order": ["Alex", "99#2"],
"second order": ["Ann", "101#2"],
"third order": ["Nick", "110#3"],
}
orders = [
{
"order": k,
"name": name,
"price": float(pq.split("#")[0]),
"quantity": float(pq.split("#")[1]),
}
for k, (name, pq) in orders.items()
]
Then if you want to find highest and lowest price you can use min/max function easily:
highest = max(orders, key=lambda k: k["price"])
lowest = min(orders, key=lambda k: k["price"])
print(highest)
print(lowest)
Prints:
{'order': 'third order', 'name': 'Nick', 'price': 110.0, 'quantity': 3.0}
{'order': 'first order', 'name': 'Alex', 'price': 99.0, 'quantity': 2.0}

Columns with mixed datatype to be saved as str and jsonb in postgres with python

I need your advice but don't be horrified with the code below, please.
Situation: I call an API to retrieve the sales information. The response looks like the following:
[{'Id': 123,
'Currency': 'USD',
'SalesOrder': [{'Price': 2,
'Subitem': 1,
'Discount': 0.0,
'OrderQuantity': 1.0},
{'Price': 3,
'Subitem': 2,
'Discount': 0.0,
'OrderQuantity': 2.0}],
'Tax': 18},
{'Id': 124,
'Currency': 'USD',
'SalesOrder': [{'Price': 2,
'Subitem': 1,
'Discount': 0.0,
'OrderQuantity': 1.0},
{'Price': 3,
'Subitem': 2,
'Discount': 0.0,
'OrderQuantity': 2.0}],
'Tax': 18}]
Expected outcome: 1. 'Id' is a stand-alone column; 'Currency' is a stand-alone column. 2. As there could be a different number of 'Subitems', I thought of adding 'SalesOrder' as a json blob in postgres and then, query the json column. Thus, the end result is a postgres table with three columns.
id =[]
currency = []
salesOrder = []
#extracting values
for item in df:
id.append(item.get("Id")
currency.append(item.get("Currency"))
salesOrders.append(item.get("SalesOrder"))
#converting to a pandas df
df_id = pd.DataFrame(id)
df_currency = pd.DataFrame(currency)
df_sales_order = pd.DataFrame(salesOrder)
#concatenating cols
df_row = pd.concat([df_id, df_currency, df_sales_order], axis = 1)
#outputting results to a table
engine = create_engine('postgresql+psycopg2://username:password#endpoint/db')
with engine.connect() as conn, conn.begin():
df_row.to_sql('tbl', con=conn, schema='schema', if_exists='append', index = False)
Doubts: 1. If I try to implement the code above, the 'SalesOrder' list gets split into an X number of columns. Why so? How can I avoid it and keep it together?
2. I am not sure how to proceed with the mixture of data types (str + jsonb). Shall I load 'non-json' columns and then, update the table with the json column?
Instead of doing this "df_sales_order = pd.DataFrame(salesOrder)
", just create a column in the "df_currency" like df_currency["sales_order"] and fill it with the "item.get("SalesOrder")". This should solve the issue.

Filtering through a list with embedded dictionaries

I've got a json format list with some dictionaries within each list, it looks like the following:
[{"id":13, "name":"Albert", "venue":{"id":123, "town":"Birmingham"}, "month":"February"},
{"id":17, "name":"Alfred", "venue":{"id":456, "town":"London"}, "month":"February"},
{"id":20, "name":"David", "venue":{"id":14, "town":"Southampton"}, "month":"June"},
{"id":17, "name":"Mary", "venue":{"id":56, "town":"London"}, "month":"December"}]
The amount of entries within the list can be up to 100. I plan to present the 'name' for each entry, one result at a time, for those that have London as a town. The rest are of no use to me. I'm a beginner at python so I would appreciate a suggestion in how to go about this efficiently. I initially thought it would be best to remove all entries that don't have London and then I can go through them one by one.
I also wondered if it might be quicker to not filter but to cycle through the entire json and select the names of entries that have the town as London.
You can use filter:
data = [{"id":13, "name":"Albert", "venue":{"id":123, "town":"Birmingham"}, "month":"February"},
{"id":17, "name":"Alfred", "venue":{"id":456, "town":"London"}, "month":"February"},
{"id":20, "name":"David", "venue":{"id":14, "town":"Southampton"}, "month":"June"},
{"id":17, "name":"Mary", "venue":{"id":56, "town":"London"}, "month":"December"}]
london_dicts = filter(lambda d: d['venue']['town'] == 'London', data)
for d in london_dicts:
print(d)
This is as efficient as it can get because:
The loop is written in C (in case of CPython)
filter returns an iterator (in Python 3), which means that the results are loaded to memory one by one as required
One way is to use list comprehension:
>>> data = [{"id":13, "name":"Albert", "venue":{"id":123, "town":"Birmingham"}, "month":"February"},
{"id":17, "name":"Alfred", "venue":{"id":456, "town":"London"}, "month":"February"},
{"id":20, "name":"David", "venue":{"id":14, "town":"Southampton"}, "month":"June"},
{"id":17, "name":"Mary", "venue":{"id":56, "town":"London"}, "month":"December"}]
>>> [d for d in data if d['venue']['town'] == 'London']
[{'id': 17,
'name': 'Alfred',
'venue': {'id': 456, 'town': 'London'},
'month': 'February'},
{'id': 17,
'name': 'Mary',
'venue': {'id': 56, 'town': 'London'},
'month': 'December'}]

Extracting data from a json.loads list

I am trying to extract a specific column or layer, not sure what you want to call it.. that is inside a json object of which I have converted to what I think is a layered list but I have two problems, my check to see if "return" is in the list is not finding anything even though when printing jsonb I can see it is in the list, my second problem is how do I extract a certain column from a layer.. in this case I need the number "43343243" out the second layer and put into a variable I tried referencing it with jsonb["return"][0] and I got a key error..
My code:
def worker(pairtxt):
while (1 < 2):
balanceobject = requests.post(urlauth, headers=headers, data=paybytes)
json_stringb = str(balanceobject.content, 'utf8')
jsonb = json.loads(json_stringb)
print(jsonb)
if "return" in jsonb: #fails
print(jsonb["return"]["0"]) # key error
print(jsonb["return"]) # prints everything even layers under the number
My jsonb print output
{'success': 1, 'return': {'43343243': {'status': 0, 'pair': 'rgeg',
'type': 'sell', 'amount': 0.01000002, 'rate': 1.0,
'timestamp_created': 1502642258}}}
Because 43343243 is a key not a value, you need to get the keys of return :
jsonb = {'success': 1, 'return': {'43343243': {'status': 0, 'pair': 'rgeg', 'type': 'sell', 'amount': 0.01000002, 'rate': 1.0, 'timestamp_created': 1502642258}}}
numberWanted = list(jsonb['return'].keys())[0]
print(numberWanted) # => 43343243
I think you are looking at the output jsonb as a list (which is not true). jsonb is a dictionary. To verify that you can do this:
print(type(jsonb))
A dictionary has key-value pairs.
success is a key and 1 is the value.
return is a key and the value is another dictionary.
{
'43343243': {
'status': 0,
'pair': 'rgeg',
'type': 'sell',
'amount': 0.01000002,
'rate': 1.0,
'timestamp_created': 1502642258
}
}
If you want to access 43343243, then you can do jsonb['return']['43343243']

Python: How to store multiple values for one dictionary key

I want to store a list of ingredients in a dictionary, using the ingredient name as the key and storing both the ingredient quantity and measurement as values. For example, I would like to be able to do something like this:
ingredientList = {'flour' 500 grams, 'tomato' 10, 'mozzarella' 250 grams}
With the 'tomato' key, tomatoes do not have a measurement, only a quantity. Would I be able to achieve this in Python? Is there an alternate or more efficient way of going about this?
If you want lists just use lists:
ingredientList = {'flour': [500,"grams"], 'tomato':[10], 'mozzarella' :[250, "grams"]}
To get the items:
weight ,meas = ingredientList['flour']
print(weight,meas)
(500, 'grams')
If you want to update just ingredientList[key].append(item)
You could use another dict.
ingredientList = {
'flour': {'quantity': 500, 'measurement': 'grams'},
'tomato': {'quantity': 10},
'mozzarella': {'quantity': 250, 'measurement': 'grams'}
}
Then you could access them like this:
print ingredientList['mozzarella']['quantity']
>>> 250

Categories

Resources