BIG = { "Brand" : ["Clothes" , "Watch"], "Beauty" : ["Skin", "Hair"] }
SMA = { "Clothes" : ["T-shirts", "pants"], "Watch" : ["gold", "metal"],
"Skin" : ["lotion", "base"] , "Hair" : ["shampoo", "rinse"]}
I want to combine this data
like this...
BIG = {"Brand" : [ {"Clothes" : ["T-shirts", "pants"]}, {"Watch" : ["gold", "metal"]} ],...
Please tell me how to solve this problem.
First off, those are dictionaries and not lists. Also, I do no know your intention behind merging two dictionaries in that representation.
Anyways, if you want the output to be exactly as you mentioned, then this is the way to do it -
BIG = { "Brand" : ["Clothes" , "Watch"], "Beauty" : ["Skin", "Hair"] }
SMA = { "Clothes" : ["T-shirts", "pants"], "Watch" : ["gold", "metal"],"Skin" : ["lotion", "base"] , "Hair" : ["shampoo", "rinse"]}
for key,values in BIG.items(): #change to BIG.iteritems() in python 2.x
newValues = []
for value in values:
if value in SMA:
newValues.append({value:SMA[value]})
else:
newValues.append(value)
BIG[key]=newValues
Also, BIG.update(SMA) will not give you the right results in the way you want them to be.
Here is a test run -
>>> BIG.update(SMA)
>>> BIG
{'Watch': ['gold', 'metal'], 'Brand': ['Clothes', 'Watch'], 'Skin': ['lotion', 'base'], 'Beauty': ['Skin', 'Hair'], 'Clothes': ['T-shirts', 'pants'], 'Hair': ['shampoo', 'rinse']}
Firstly, you need to iterate on first dictionary and search the pair of key in second dictionary.
BIG = { "Brand" : ["Clothes" , "Watch"], "Beauty" : ["Skin", "Hair"] }
SMA = { "Clothes" : ["T-shirts", "pants"], "Watch" : ["gold", "metal"], "Skin" : ["lotion", "base"] , "Hair" : ["shampoo", "rinse"]}
for key_big in BIG:
for key_sma in BIG[key_big]:
if key_sma in SMA:
BIG[key_big][BIG[key_big].index(key_sma)] = {key_sma: SMA.get(key_sma)}
print BIG
The result of code:
>>> {'Brand': [{'Clothes': ['T-shirts', 'pants']}, {'Watch': ['gold', 'metal']}], 'Beauty': [{'Skin': ['lotion', 'base']}, {'Hair': ['shampoo', 'rinse']}]}
Related
I am running into difficulties trying to create a modified key when manipulating a dictionary. Here the key needs to be changed from the original dict key to 'x' plus the dict key. How can that be done? My attempt is shown:
inventory = {k:updateMap(m,
{'partNumber': m['part'],
'partName': m['desc'],
'bbox': {
'xmin' : bboxes[k].x,
'xmax' : bboxes[k].x + bboxes[k].w,
'ymin' : bboxes[k].y,
'ymax' : bboxes[k].y + bboxes[k].h
}
}) for k,m in
['x%d' %k1,m1
for k1,m1 in inventoryRaw.items()]}
Here is the syntax error Unresolved reference m1:
How should the nested comprehension be modified?
The problem here is the tuple needs to be explicitly spelled out :
for k,m in [('x%s'%k1,m1)
This works:
inventory = {'x%s'%k:updateMap(m,
{'partNumber': m['part'],
'partName': m['desc'],
'objectClass': 'part',
'bbox': {
'xmin' : bboxes[k].x,
'xmax' : bboxes[k].x + bboxes[k].w,
'ymin' : bboxes[k].y,
'ymax' : bboxes[k].y + bboxes[k].h
}
}) for k,m in [('x%s'%k1,m1)
for k1,m1 in
inventoryRaw.items()]}
I am creating a rest-api using the Python+Flask_Restful combo, find it amazing.
Currently, i am allowing user to run a sql-query using the browser and it works fine, but the problem is, headers information is not displayed in the response.
Here is the code that i am using:
class RunCustomeSQL(Resource):
def get(self, enter_db_name, query):
if not os.path.isfile(enter_db_name+'.db'):
raise BadRequest("Database Doesn't Exist. Please select a valid database")
conn = sqlite3.connect(enter_db_name+'.db')
search_out = []
cursor = conn.execute(query)
row = None
for row in cursor:
search_out.append(row)
if not row: #This means empty response
raise BadRequest("No Results Found")
conn.commit()
conn.close()
return search_out
While, this code works great, it doesn't print the header values in the json-response. The current response is :
[
[
"dusiri_bibi",
"11",
"----------",
" srt/None ",
"14.30 MB",
"2017-12-13 23:43:54",
"C:/Test_Software/vc_redist.x64.exe"
],
]
Expected Output :
[
[
"Machine Name" : "dusiri_bibi",
"LABEL" : "11",
"PERMISSIONS" : "----------",
"USER" : " srt/None ",
"SIZE" : "14.30 MB",
"CREATED" : "2017-12-13 23:43:54",
"FILENAME" : "C:/Test_Software/vc_redist.x64.exe"
],
]
All the above text such as "machine name, label etc." are my table headers, I am not sure how to print them along with my output.
What if the user runs select user, size from table_name only
or
What if the user runs select * from table_name
In both scenario's, the output should display the table headers
Thanks
UPDATE #1 (25 April) : I managed to answer my first question and able to display a proper json response if the user selects the SELECT * statement in SQL but still facing issue with the second piece of it
Here is the answer to first part if anyone is looking for it : Using Regex
row = None
if re.search(r'(?<=SELECT)(.*)(?=FROM)',query, re.IGNORECASE).group(1) == ' * ':
for row in cursor:
search_out.append({'NAME' : row[0], 'LABEL_NUMBER' : row[1], 'PERM' : row[2], 'USER' : row[3] , 'SIZE' : row[4], 'DATE' : row[5], 'FILENAME' : row[6]})
if not row: #This means empty response
raise BadRequest("No Results Found")
Part II : Unanswered Query:
For the second piece, i now have two list :
list_1 : [[u'LABEL_NUMBER', u'PERM', u'FILENAME']]
list_2 : [(u'11', u'----------', u'C:/Test_Software/26.avi'), (u'11', u'----------', u'C:/Test_Software/6.avi'), (u'11', u'-rwx------', u'C:/Test_Software/Debug/Current_Frame1.avi'), (u'10', u'-rwxrwx---', u'C:/Windows/WinSxS/boxed-split.avi')]
As you can see, i have two list and i want to combine them into a dict to show the response like this:
[
{
LABEL_NUMBER : '11' ,
PERM : '-----------',
FILENAME : 'C:/Test_Software/26.avi'
},
...
....
......
{
LABEL_NUMBER : '10' ,
PERM : '-rwxrwx---',
FILENAME : 'C:/Windows/WinSxS/boxed-split.avi'
},
]
i am using the following code to do the same :
chunks = [list_2[idx:idx+3] for idx in range(0, len(list_2), 3)]
output = []
for each in chunks:
output.append(dict(zip(list_1, each)))
print(output)
But, this is failing with "TypeError: unhashable type: 'list'", i understand that lists are mutable and which is why i am getting this error but then how can i get the desired dict response? what am i doing wrong here?
You can use a list comprehension combined with zip for this:
list_1 = [[u'LABEL_NUMBER', u'PERM', u'FILENAME']]
list_2 = [(u'11', u'----------', u'C:/Test_Software/26.avi'), (u'11', u'----------', u'C:/Test_Software/6.avi'), (u'11', u'-rwx------', u'C:/Test_Software/Debug/Current_Frame1.avi'), (u'10', u'-rwxrwx---', u'C:/Windows/WinSxS/boxed-split.avi')]
d = [dict(zip(list_1[0], i)) for i in list_2]
Result:
[{'FILENAME': 'C:/Test_Software/26.avi',
'LABEL_NUMBER': '11',
'PERM': '----------'},
{'FILENAME': 'C:/Test_Software/6.avi',
'LABEL_NUMBER': '11',
'PERM': '----------'},
{'FILENAME': 'C:/Test_Software/Debug/Current_Frame1.avi',
'LABEL_NUMBER': '11',
'PERM': '-rwx------'},
{'FILENAME': 'C:/Windows/WinSxS/boxed-split.avi',
'LABEL_NUMBER': '10',
'PERM': '-rwxrwx---'}]
General question: how can I search a specific key:value pair in a JSON using Python?
Details for the specific case: I'm reading ~ 45'000 JSON objects, each one of them look like this one.
As you can see, inside every JSON there are several dictionaries that have the same keys (but different values): "facetName, "facetLabel", "facetValues".
I'm interested in the dictionary that starts with "facetName": "soggettof", that goes like:
{
"facetName": "soggettof",
"facetLabel": "Soggetto",
"facetValues": [
[
"chiesa - storia - documenti",
"chiesa - storia - documenti",
"1"
],
[
"espiazione - mare mediterraneo <bacino> - antichita - congressi - munster - 1999",
"espiazione - mare mediterraneo <bacino> - antichita - congressi - munster - 1999",
"1"
],
[
"lega rossa combattenti - storia",
"lega rossa combattenti - storia",
"1"
],
[
"pavia - storia ecclesiastica - origini-sec. 12.",
"pavia - storia ecclesiastica - origini-sec. 12.",
"1"
],
[
"pavia <diocesi> - storia - origini-sec. 12.",
"pavia <diocesi> - storia - origini-sec. 12.",
"1"
],
[
"persia - sviluppo economico - 1850-1900 - fonti diplomatiche inglesi",
"persia - sviluppo economico - 1850-1900 - fonti diplomatiche inglesi",
"1"
]
Please note, that not all the JSON objects have that.
How can I grab the values of the facetValues list, but only in the dictionary that I'm interested in?
I found your question a little confusing, partially because the data shown in it was not really the JSON-object you needed to extract the information from—but instead was just an example of a sub-JSON-object you wanted to extract it from. Fortunately you had a link to the outermost container JSON-object (even though the data in corresponding sub-JSON-object in it was different). Here's the data from that link:
json_obj = {"numFound":1,"start":0,"rows":3,"briefRecords":[{"progressivoId":0,"codiceIdentificativo":"IT\\ICCU\\LO1\\0120590","autorePrincipale":"Savoia, Carlo","titolo":"Per la inaugurazione dell'Asilo infantile Strozzi nei locali della caserma Filippini già convento della Vittoria / parole di mons. Carlo Savoia","pubblicazione":"Mantova : Tip. Eredi Segna, 1870","livello":"Monografia","tipo":"Testo a stampa","numeri":[],"note":[],"nomi":[],"luogoNormalizzato":[],"localizzazioni":[],"citazioni":[]}],"facetRecords":[{"facetName":"level","facetLabel":"Livello bibliografico","facetValues":[["Monografia","m","1"]]},{"facetName":"tiporec","facetLabel":"Tipo di documento","facetValues":[["Testo a stampa","a","1"]]},{"facetName":"nomef","facetLabel":"Autore","facetValues":[["savoia, carlo","savoia, carlo","1"]]},{"facetName":"soggettof","facetLabel":"Soggetto","facetValues":[["mantova - asili infantili","mantova - asili infantili","1"]]},{"facetName":"luogof","facetLabel":"Luogo di pubblicazione","facetValues":[["mantova","mantova","1"]]},{"facetName":"lingua","facetLabel":"Lingua","facetValues":[["italiano","ita","1"]]},{"facetName":"paese","facetLabel":"Paese","facetValues":[["italia","it","1"]]}]}
It's important to have this outermost container because it is through it you will have to drill-down to the portion you want. Once you have the actual data It's often helpful to reformat it to make its structure clear. You can do this by hand, or have the computer do it via a print(json.dumps(json_obj, indent=2)), although the results from that can sometimes have a little too much white space in them (which can be counterproductive).
That being the case here, below is the more succinct version I came up doing it manually that still let's me see the overall layout of the data:
json_obj = {"numFound" : 1,
"start" : 0,
"rows" : 3,
"briefRecords" : [
{"progressivoId" : 0,
"codiceIdentificativo" : "IT\\ICCU\\LO1\\0120590",
"autorePrincipale" : "Savoia, Carlo",
"titolo" : "Per la inaugurazione dell'Asilo infantile Strozzi nei locali della caserma Filippini già convento della Vittoria / parole di mons. Carlo Savoia",
"pubblicazione" : "Mantova : Tip. Eredi Segna, 1870",
"livello" : "Monografia",
"tipo" : "Testo a stampa",
"numeri" : [],
"note" : [],
"nomi" : [],
"luogoNormalizzato" : [],
"localizzazioni" : [],
"citazioni" : []
}
],
"facetRecords" : [
{"facetName" : "level" ,
"facetLabel" : "Livello bibliografico" ,
"facetValues" : [["Monografia" , "m" , "1"]]},
{"facetName" : "tiporec" ,
"facetLabel" : "Tipo di documento" ,
"facetValues" : [["Testo a stampa" , "a" , "1"]]},
{"facetName" : "nomef" ,
"facetLabel" : "Autore" ,
"facetValues" : [["savoia, carlo" , "savoia, carlo" , "1"]]},
{"facetName" : "soggettof" ,
"facetLabel" : "Soggetto" ,
"facetValues" : [["mantova - asili infantili" , "mantova - asili infantili" , "1"]]},
{"facetName" : "luogof" ,
"facetLabel" : "Luogo di pubblicazione" ,
"facetValues" : [["mantova" , "mantova" , "1"]]},
{"facetName" : "lingua" ,
"facetLabel" : "Lingua" ,
"facetValues" : [["italiano" , "ita" , "1"]]},
{"facetName" : "paese" ,
"facetLabel" : "Paese" ,
"facetValues" : [["italia" , "it" , "1"]]}
]
}
Once you have something like this, it's usually fairly easy to determine what code is needed. In this case it's:
target_facet_name = "soggettof"
for record in json_obj["facetRecords"]:
if record["facetName"] == target_facet_name:
for value in record["facetValues"]:
print(value)
Since facetRecords is a list, a linear search through them as shown is required to find the one(s) wanted.
inventory = {
'gold' : 500,
'pouch' : ['flint', 'twine', 'gemstone'], # Assigned a new list to 'pouch' key
'backpack' : ['xylophone','dagger', 'bedroll','bread loaf'],
'pocket': ['seashell','strange berry','lint']
}
how to Add 50 to the number stored under the 'gold' key
Elementary, Watson:
inventory['gold'] += 50
Consider the following List of Dictionaries(retrieved from database):
education = [{
"schools" : "Nelson Mandela Metropolitan University",
"studied_from" : new Date("16/1/2012 02:00:00"),
"studied_to" : new Date("25/1/2055 02:00:00"),
"qualifications" : " National Diploma",
"fields_of_study" : "Industrial engineering"
}, {
"schools" : "Massachusetts Institute of Technology",
"studied_from" : new Date("16/1/2009 02:00:00"),
"studied_to" : new Date("25/1/2020 02:00:00"),
"qualifications" : "B Tech",
"fields_of_study" : "Information Technology"
}]
I am trying to get the data from one dict to be returned.
I have tried the following:
for edu in context.education:
studied_to.append(edu.studied_to)
qualification = edu.qualifications
fields_of_study = edu.fields_of_study
school = edu.schools
recent = max(studied_to)
if recent:
print recent
print qualification
print fields_of_study
This is not working as it always returns the data for the data in the last dict,
I also tried putting the if-statement inside the for loop, but this did not work either as then it runs for all entries.
I ultimately want to use these values to generate on an HTML page.
Your code unfortunately makes no sense at all. You seem to be appending some variables to lists, and redefining other variables with the latest version, without ever actually returning the dictionary you want.
It seems however that what you're actually after is the dict with the maximum "studied_to" value. So, you should just replace all that code with max with a key:
most_recent = max(education, key=lambda s: s['studied_to'])
(Also, do please try and post valid syntax for the language you're using. The first snippet appears to be Javascript, not Python.)
I think you are trying to get the data for the most recent education. You should use a variable to hold the most recent date so far and compare that to the entries in your list. See this as a reference:
max_date = None
for edu in education:
# this line is for initialization
if max_date is None:
max_date = edu["studied_to"]
# check if we have a recent entry at hand
if max_date <= edu["studied_to"]:
max_date = edu["studied_to"]
qualification = edu["qualifications"]
fields_of_study = edu["fields_of_study"]
print max_date
print qualification
print fields_of_study
With some correction of syntax:
from operator import itemgetter
from datetime import datetime
education = [{
"schools" : "Nelson Mandela Metropolitan University",
"studied_from" : datetime.strptime("16/1/2012 02:00:00", "%d/%m/%Y %H:%M:%S"),
"studied_to" : datetime.strptime("25/1/2055 02:00:00", "%d/%m/%Y %H:%M:%S"),
"qualifications" : " National Diploma",
"fields_of_study" : "Industrial engineering"
}, {
"schools" : "Massachusetts Institute of Technology",
"studied_from" : datetime.strptime("16/1/2009 02:00:00", "%d/%m/%Y %H:%M:%S"),
"studied_to" : datetime.strptime("25/1/2020 02:00:00", "%d/%m/%Y %H:%M:%S"),
"qualifications" : "B Tech",
"fields_of_study" : "Information Technology"
}]
most_recent = max(education, key=itemgetter('studied_to'))
Thanks to all, your answers were really helpful. I took Daniel's answer and modified it:
edu = sorted(education, key=lambda studied_to: studied_to)
Now I can call edu[0] to get the value of the most recent education:
>>> edu[0]["studied_to"]
datetime.datetime(2055, 1, 25, 2, 0)
>>> edu[-1]["studied_to"]
datetime.datetime(2020, 1, 25, 2, 0)
No matter what ed[0] will always be the latest education.