Context: I need to return certain keys AND their values which are present in every dictionary of a list
The problem : I just can't get it right, be it via iteration, filter or list logic...
Here is an example of a list that needs to be filtered:
[{
"id": "id1",
"date": "2021-01-05 03:05:06",
"prochain_joueur": "joueur1",
"joueurs": ["joueur1", "joueur2"],
"state": ["pio1","pio2"],
"gagnant": "Jorje"
},
{...},
{
"id": "id23",
"date": "2021-01-05 03:05:06",
"prochain_joueur": "joueur2",
"joueurs": ["joueur1", "joueur2"],
"state": ["pio1","pio2"],
"gagnant": "Castillo"
}]
And here is what needs to be returned:
[{
"id": "80a0a0d2-059d-4539-9d53-78b3f6045943",
"date": "2021-01-23 14:23:59",
"joueurs": ["jowic42", "robot-1"],
"gagnant": "jowic42"
},
{...},
{
"id": "80a0a0d2-059d-4539-9d53-78b3f6045943",
"date": "2021-02-23 14:23:59",
"joueurs": ["jowic42", "robot-1"],
"gagnant": "jowic42"
}]
I simply need to return the same list witouth the keys "prochain_joueur" and "state" and I just can't figure it out. Note that originally, the list is in Json, I need to return the last 20 entries and it needs to be returned in Json but, I actually know how to do that part!
I made it using this:
for dict_iter in list_dict:
for key in keys_to_delete:
if key in dict_iter.keys():
del dict_iter[key]
list_dict is your list of dictionaries, keys_to_deleteis a list of the keys you want out e.g. ["prochain_joueur", "state"]
You can make a list of the keys you don't want and then use a dict comprehension inside a list comprehension to filter them out. Given your original list as l:
unwanted = ["prochain_joueur" , "state"]
filtered = [{k:v for k, v in d.items() if k not in unwanted} for d in l]
Which will give you:
[{'id': 'id1',
'date': '2021-01-05 03:05:06',
'joueurs': ['joueur1', 'joueur2'],
'gagnant': 'Jorje'},
{'id': 'id23',
'date': '2021-01-05 03:05:06',
'joueurs': ['joueur1', 'joueur2'],
'gagnant': 'Castillo'}]
given:
list_of_keys = list containing all keys to be copied
list_of_dicts = list of dictionaroes to search
def return_keys(list_of_keys, list_of_dicts):
rslt = []
for dct in list_of_dicts:
td = {}
for k in list_of_keys:
if k in dct.keys():
td[k] = dct[k]
rslt.append(td)
return rslt
Here is one approach with pandas. You load your list of dictionaries in a pandas.DataFrame; the keys become your columns and the rows become your values. If a key does not exist in one of your dictionaries, the corresponding value in that row will be nan. So, you can delete all columns that have at least one nan value.
import pandas as pd
df = pd.DataFrame(your_list)
df.dropna(axis=1, inplace=True) # Delete columns with nan
>>> df.columns
Index(['id', 'date', 'joueurs', 'gagnant'], dtype='object')
And then convert your dataframe back to a list of dictionaries:
new_list = df.to_dict('records')
I need to split the string into substrings and pair them with the key value.
I'm new to the groovy language, so I'd appreciate it if you can help :)
I have this:
{"key": "a", "tag": ""},
{"key": "b", "tag": "one, two"}
I want to get this
{"key": "a", "tag": ""},
{"key": "b", "tag": "one"}
{"key": "b", "tag": "two"}
Use String::split(String) to split the strings.
Use Collection::flatten(Closure) to convert each entry object into any number of output objects (so for this particular problem: convert each entry with unsplit tag value into a separate entry for each comma-separated value in tag). You can also use Java 8 streams and the flatMap method to achieve the same result, flatten is just specific to Groovy (not necessarily better).
I don't think it would be good to give you a full solution, though, so I'll leave that up to you.
I need a query that can get me the document from a list of words for example if I use
select c from c join (SELECT distinct VALUE c.id FROM c JOIN word IN c.words WHERE word in('word1',word2) and tag in('motorcycle')) ORDER BY c._ts desc
it will bring both documents, I want to retrieve only the first one cause it matches the two words and not only one.
Document 1
"c": {
"id": "d0f1723c-0a55-454a-9cf8-3884f2d8d61a",
"words": [
"word1",
"word2",
"word3",
]}
Document 2
"c": {
"id": "d0f1723c-0a55-454a-9cf8-3884f2d8d61a",
"words": [
"word1",
"word4",
"word5",
]}
You should be able to cover this with two ARRAY_CONTAINS expressions in your WHERE clause (and no need for a JOIN):
SELECT c.id FROM c
WHERE ARRAY_CONTAINS(c.words, 'word1')
AND ARRAY_CONTAINS(c.words, 'word2')
This should return the id of your first document.
I have a couchbase document as
{
"last": 123,
"data": [
[0, 1.1],
[1, 2.3]
]
}
currently have code to upsert the document to change the last property and add values to the data array, however, cannot find a way to insert unique values only. I'd like to avoid fetching the whole document and doing the filtering in javascript. Is there any way in couchbase?
arrayAddUnique will fail, cause there are floats in the subarrays per couchbase docs.
.mutateIn(`document`)
.upsert("last", 234)
.arrayAppend("data", newDataArray)
.execute( ... )
I have a usecase where for a given result value i want to reverse lookup all the search conditions defined that will give this as result.
So, I have a set of search conditions defined in a table as key value list. Each entry in this table is a search query. Now, I have a random value in dataset which can be result of any search entries defined in the table. I want to lookup that table so that for this value i can get all the search queries possible where this value would appear as its result.
The search table consist of fields search_conditions, search_table among other fields.
Schema would be like
Search_Table
id (long)
search_table_id (long)
search_conditions (json array as text)
This is value of one such search condition
[
{
"key": "name",
"operator": "equals",
"value": "jeff"
},
{
"key": "age",
"operator": "between",
"value": [
20,
40
]
}
]
Value that i have to search can be say a random user {"name": "mr x", "age":12}.
This may not be exactly a technology based question but its solution may require technology. Any help will be appreciated. The concern is more about optimization as this has to be done in real time.