I want to initialize a list of set to a dictionary.I can directly enter the variables but couldn't initialize it and get input from user.My data structure should look something like this
d={1:[{1,2},{3,4}],2:[{2,3},{4,5,100}]}
So that,if i want to access the element 100 it could be done as d[2][1][2]
.I could define the data structure but couldn't initialize it.Could someone help me to initialize the structure.
If I understand correctly you want to initialise the array of sets automatically. Default dict seems the most appropriate way to do this:
from collections import defaultdict
a = defaultdict(lambda: [set(), set()])
a[100][2].add(2)
print(a.items())
This would initialise a list with two sets whenever accessing a non existing key.
Related
How I could append an element to an array like that:
Using this code I'm overriding the old data:
let toUpdate = [book.id]
self.refUsers.child(localUser.key!).child("booksPurchased").setValue(toUpdate, withCompletionBlock: { (error, _) in
You could use this method: firebase.firestore.FieldValue.arrayUnion()
Example with angularfire2:
this.afs.collection('collection').doc(id).update( {
array: firebase.firestore.FieldValue.arrayUnion( 'newItem' )
});
For more information: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldValue#arrayunion
In this case, you will have to read the existing data, then write it back with the new value added. Arrays like this are not always the best way to store lists of data if you want to perform a lot of append operations. For that, you're better off pushing data into a location using childByAutoId.
You could set the values of the keys in the array to true, and then set the value directly in an update.
So if 'newId' is the new item to add, maybe something like:
const update = {
[`/users/${localUser.key}/booksPurchased/${newId}`]: true]
}
firebase.db.ref().udpate(update);
Firebase docs example of an update:
https://firebase.google.com/docs/database/web/read-and-write
Reading and writing lists
Append to a list of data
Use the childByAutoId method to append data to a list in multiuser applications. The childByAutoId method generates a unique key every time a new child is added to the specified Firebase reference. By using these auto-generated keys for each new element in the list, several clients can add children to the same location at the same time without write conflicts. The unique key generated by childByAutoId is based on a timestamp, so list items are automatically ordered chronologically.
You can use the reference to the new data returned by the childByAutoId method to get the value of the child's auto-generated key or set data for the child. Calling getKey on a childByAutoId reference returns the auto-generated key.
You can use these auto-generated keys to simplify flattening your data structure. For more information, see the data fan-out example.
-https://firebase.google.com/docs/database/ios/lists-of-data
I have a python dictionary with keys as dataset names and values as the entire data frames themselves, see the dictionary dict below
[Dictionary of Dataframes ]
One way id to write all the codes manually like below:
csv = dict['csv.pkl']
csv_emp = dict['csv_emp.pkl']
csv_emp_yr= dict['csv_emp_yr.pkl']
emp_wf=dict['emp_wf.pkl']
emp_yr_wf=dict['emp_yr_wf.pkl']
But this will get very inefficient with more number of datasets.
Any help on how to get this done over a loop?
Although I would not recommend this method but you can try this:
import sys
this = sys.modules[__name__] # this is now your current namespace
for key in dict.keys():
setattr(this, key, dict[key])
Now you can check new variables made with names same as keys of dictionary.
globals() has risk as it gives you what the namespace is currently pointing to but this can change and so modifying the return from globals() is not a good idea
List can also be used like (limited usecases):
dataframes = []
for key in dict.keys():
dataframes.append(dict[key])
Still this is your choice, both of the above methods have some limitations.
When building a set of statistics from a dictionary, I process the various entries (such as by user). THus, I can build the various statistics for each user. While doing this, I also build the statistics for a dummy user that I can call "total". After the dictionary is completely built, I create a .csv file and output the statistics using the writerow method.
Since python iterates of the dictionary keys in no particular order, I want to cause the total user to print last. If I attempt to save the generated statistics into a save variable and then output it at the proper time, the save variable gets reset because python variables work by reference rather than value. That is the code
mystats = {}
totalstats = {}
for user in mydict
#perform calculations to generate mystats dictionary entries
if user == 'Total':
totalstats = mystats
else:
outfile.writerow(mystats)
outfile.writerow(totalstats)
However, the actual output of totalstats is whatever set of values had been put into mystats last.
Is there a decent way to show that totalstats is to keep the explicit values within mystats that I had at the time of the assignment or do I need to calculate all the statistics at the end or do
for stattype in mystats:
totalstats[stattype] = mystats[stattype]
While this works, I would rather have something of the type "totalstats = mystats' rather than do a large loop over the complete set of statistics or calculate the entire set of statistics for Total at the end of processing.
You can use copy.deepcopy:
from copy import deepcopy
totalstats = deepcopy(mystats)
If the dict doesn't contain mutable values then you can simply use dict.copy().
Essentially this is what I'm trying to do:
I have a set that I add objects to. These objects have their own equality method, and a set should never have an element equal to another element in the set. However, when attempting to insert an element, if it is equal to another element, I'd like to record a merged version of the two elements. That is, the objects have an "aux" field that is not considered in its equality method. When I'm done adding things, I would like an element's "aux" field to contain a combination of all of the "aux" fields of equal elements I've tried to add.
My thinking was, okay, before adding an element to the set, check to see if it's already in the set. If so, pull it out of the set, combine the two elements, then put it back in. However, the remove method in Python sets doesn't return anything and the pop method returns an arbitrary element.
Can I do what I'm trying to do with sets in Python, or am I barking up the wrong tree (what is the right tree?)
Sounds like you want a defaultdict
from collections import defaultdict
D = defaultdict(list)
D[somekey].append(auxfield)
Edit:
To use your merge function, you can combine the code people have given in the comments
D = {}
for something in yourthings:
if something.key in D:
D[something.key] = something.auxfield
else:
D[something.key] = merge(D[something.key], something.auxfield)
Is there a better way to implement a paging solution using dict than this?
I have a dict with image names and URLs.
I need to 16 key value pairs at a time depending on the user's request, i.e. page number.
It's a kind of paging solution.
I can implement this like:
For example :
dict = {'g1':'first', 'g2':'second', ... }
Now I can create a mapping of the keys to numbers using:
ordered={}
for i, j in enumerate(dict):
ordered[i]=j
And then retrieve them:
dicttosent={}
for i in range(paegnumber, pagenumber+16):
dicttosent[ordered[i]] = dict[ordered[i]]
Is this a proper method, or will this give random results?
Store g1, g2, etc in a list called imagelist
Fetch the pages using imagelist[pagenumber: pagenumber+16].
Use your original dict (image numbers to urls) to lookup the url for each of those 16 imagenames.
1) Will this give random results ?
Sort of.
Quoting from the official documentation about dict:
Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.
So for your purposes you can't know a priori on what will be the order of your iteration.
OrderedDict is what you're looking for: an OrderedDict is a dict that remembers the order that keys were first inserted.
2) Is this actually a proper method?
It doesn't seem so.
I don't know if there are library that will handle all that information for you (maybe someone else can tell you that), but it seems like you're trying to emulate the OrderedDict behaviour.
You can directly use an OrderedDict, or if you want to enumerate your info a list can do that.
It depends. If your dict doesn't change during the lifetime of the application and you don't care about ordering of the items in your dict you should be ok.
If not, you should probably use collections.OrderedDict or keep a sorted list of keys, depending on your requirements. Using normal dict doesn't give you any guarantees about iteration order, so after each modification of the input dict you can get different results.
Why not just create a dict that maps to your pages? You could start off with two lists, one containing your image names and the other containing the URLs.
perPage = 16
nameList = ['g1', 'g2', ... ]
urlList = ['first', 'second', ... ]
# This is a generator expression that can create
# the nested dicts. You can also use a simple
# for loop
pageDict = dict(( (i, dict(( (nameList[j], urlList[j])
for j in range(i*perPage, i*perPage+perPage))))
for i in range(len(nameList) / perPage)))
It indexes from 0, so your first page will be pageDict[0].
...Now that I look at it again, that generator expression looks kind of awful. :|