This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Make dictionary from list with python [duplicate]
(5 answers)
Convert list into a dictionary [duplicate]
(4 answers)
Closed 4 years ago.
trying to figure out how to do this and have yet to find a good solution. I pulled this data out of an XML response. It was in a var tag. Now what I would like to do is create a dictionary out of it. The domain.com should be paired with the number right listed behind it.
This is the data:
[
'cb131.domain1.com', '147827',
'cb143.domain2.com', '147825',
'cb175.domain1.com', '147454',
'cb190.domain.com', '146210',
'cb201.domain.com', '146208',
'cb219.domain.com', '146042',
'cb225.domain.com', '146282',
'cb900.domain.com', '148461',
'cb901.domain.com', '148493',
'cb902.domain.com', '148495',
'cb903.domain.com', '148497',
'cb904.domain.com','148499',
'cb905.domain.com', '148501',
'cb906.domain.com', '148503',
'cb907.domain.com', '148505',
'cb908.domain.com', '148507',
'cb909.domain.com', '148509'
]
So for example cb131.domain1.com should be paired with 147827, cb143.domain2.com paired with 147825 and so on.
Drawing a blank on a good quick solution on how to do this. Hopefully someone can help.
Thanks!
Edited with answer I choose below:
I choose this answer and also to help anyone else I add a nice way to print out the results (data is the string I obtained):
import ast
i = iter(ast.literal_eval(data))
dic = dict(zip(i, i))
for key , value in dic.items():
print(key, " :: ", value)
This should do it. Assuming the list is saved to a variable l:
keys = l[::2]
vals = l[1::2]
dic = dict(zip(keys, vals))
You can create an iterator from the list after using ast.literal_eval to parse it from the input text, zip the iterator with itself, and pass the generated sequence of tuples to the dict constructor:
import ast
i = iter(ast.literal_eval(data))
dict(zip(i, i))
Assuming you have the above in a python array called data, you can do:
new_data = []
for i in range(0, len(data), 2):
new_data.append((data[i], data[i+1]))
Now new_data would be a list of tuples. You could certainly create a better data structure to hold these pairs if you want.
I do not yet know Python that I can write a snippet, but:
initialize an empty dictionary in Python
create a for loop counting index from 0 to length of your array in steps of two.
inside add a dictionary entry with key of value at index and value at index + 1
perhaps check for duplicates
Does this answer help you?
This is Python - quickly google'd:
dictionary = { }
for idx in range(0, len(data), 2)
dictionary[data[idx]] = data[idx + 1]
This question already has an answer here:
Firebase retrieve child keys but not values
(1 answer)
Closed 4 years ago.
Hey there i am trying to get the random generated key of datasets from a firebase.
like this:
projects
---12345
---67890
...
i just would like to get the key and safe it to another part in the Database.
I tried that:
but it gives me everything under the node.
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
console.log("Key" + JSON.stringify(snapshot.val()) )
});
}
can some one help me out with that? How to get only the Key generated by firebase?
i would like to get these Keys and save it to another dataset but i don't know how to access them?
Try the following:
getKEY(){
firebase.database().ref('project').once('value', function(snapshot) {
snapshot.forEach(function(child) {
var keys=child.key;
});
});
}
more info here:
key property
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 4 years ago.
I have a key value pair array in js
var tabList = {0:'#description', 1:'#media', 2:'#attributes', 3:'#calendar', 4:'#pricing'}
I'm using the keys to get the values in my code
ie. tabList[2] returns #attributes
I thought I could do the same in reverse to get the key
tabList[#media] and have it return 1
But this doesn't work
How can I fetch the key with only the value as input?
There are plenty of solutions here Swap key with value JSON
I will flip key with values 1st
var tabList = {0:'#description', 1:'#media', 2:'#attributes', 3:'#calendar', 4:'#pricing'}
let flipped=Object.assign({}, ...Object.entries(tabList).map(([k,v]) => ({ [v]: k })))
console.log(flipped);
console.log(flipped['#description']);
This question already has answers here:
mongoDB/mongoose: unique if not null
(4 answers)
Closed 5 years ago.
I have an email field set as unique, but it is not required.
The problem is that if the user does not enter anything Mongoose puts "null" in it. This causes duplicates because every user that does not enter the email field will have "null" assigned to it.
What is the standard practice to avoid this?
Thanks
Use a sparse unique index
If a document does not have a value for a field, the index entry for
that item will be null in any index that includes it. Thus, in many
situations you will want to combine the unique constraint with the
sparse option. Sparse indexes skip over any document that is missing
the indexed field, rather than storing null for the index entry.
db.collection.createIndex( { a: 1, b: 1 }, { unique: true, sparse: true } )
More information: https://docs.mongodb.com/v3.0/tutorial/create-a-unique-index/
This question already has answers here:
Create array in cookie with javascript
(10 answers)
Closed 9 years ago.
I am storing array in cookie through jQuery cookie plugin but each time I get it from cookie, it returns empty
I define array as:
var myArray = [];
myArray[ 0 ] = "hello";
myArray[ 1 ] = "world";
Set it in cookie
$.cookie('cookie-array', myArray);
But getting this cookie and printing, prints empty string
console.log($.cookie('cookie-array'));
EDIT:
Arrays define in other questions are object arrays no like this array I mentioned here. Also I dont want to user JSON library.
Have a look at:
https://code.google.com/p/cookies/
to store an array
$.cookie('COOKIE_NAME', escape(myArray.join(',')), {expires:1234});
to get it back
cookie=unescape($.cookie('COOKIE_NAME'))
myArray=cookie.split(',')