Python dict converted to VB.net - python

I would like to find out if there is a way to easily convert a dictionary in Python to VB.net.
for example if I had a function that took a list and I want to make it when passing it in Python it would be:
function(['Hello', 'World!'])
If I wanted to do that in VB.net I would:
function({"Hello", "World"})
But I want to know if that is possible with dict's. Such as this in Python:
function({'key': 'value', 'key2': 'value2'})
If converting that to VB.net is not possible then is there a way I could pass a list of strings and convert it from a list to a dictionary?

Dictionary and SortedDictionary are inbuilt types in the .NET Framework, so you should be fine.
This is C# - don't know VB:
Dictionary dct = new Dictionary() { { "a", 1 }, { "b", 2 } };
That creates the dictionary and adds two members to it.
Cheers -

Related

Reading dictionary in the list - python

I have a list like this (coming out of a json file reading):
list = [{'sheet_name': 'Daily', 'id_column': 'A', 'frequency': 'daily'}]
I want to read the value of the key 'id_column' but when I use:
list[0]['id_column']
it doesnt work
it considers each letter as a index in the list but not the dictionary. So this is what i get when trying to read indexes:
list[0] = [
list[1] = {
How can i read the key value in the dictionary in the list.
thanks
Because the data you retrieved is in the form of 'String' or 'JsonArray of String'
You can use json.loads() python module to convert to actual Python List or Dictionary
import json
data = json.loads(list)
print(data[0]['id_column'])
Please refer for more details,
https://pythonexamples.org/python-json-to-list/
Note: I notice that you are defined the list name as 'list' it is highly recommended to avoid naming variables or functions which equals python builtins

Node.js equivalent data types for python's list, tuple and dictionary

For anyone migrating from python to node.js, it will be useful to know the equivalent data types in node.js for lists, tuples and dictionaries.
Do they exist? If not, are there node.js modules to achieve equivalent data structures in node.js?
Where Python has lists, JavaScript has arrays. These are declared using the exact same syntax as Python, with square brackets and commas:
var names = ["Foo", "Bar", "Baz"];
names[0]; # Foo
As an alternative to dictionaries you can use associative arrays, which are actually just JavaScript objects. Again, the syntax is similar to Python:
var dictionary = {"name": "Foo", "email": "foo#bar.com"};
dictionary["name"]; # Foo
JavaScript doesn't have tuples, but there is a tuple library on NPM, although I've never used it.
If you want to experiment with arrays and associative arrays, I'd recommend repl.it - the right-hand column is a REPL (read-eval-print loop), just like Python's equivalent when you open a terminal and type py.

Get a FIRST value of json dictionary in python

I have json like this, in python.
{
"unknown1": {
"somekeys": "somevalues"
}
"unknown2": {
"somekeys": "somevalues"
}
"unknown3": {
"somekeys": "somevalues"
}
}
Is there a way to get ONLY unknown1 dictionary?
Unknowns are really unknowns and are different each time.
The speed of this operation is critical.
(I know how to load the json with json.loads)
No, there is no way to do this
In the comments, you write:
But isn't there a way to, maybe, iterate throu this json so it give me unknown1 dictionary on the first iteration or something like this? Or it would be as random as iterateing thru python dictionary?
That's exactly correct. When you load json into python using json.load() or json.loads, the json string is converted into a python dict. So, once it's loaded, you should treat it like any other dict, because it is one.
try to use some loop system
like this
first_value
for i in JsonFile:
first_value = i
break
first_value is the first dictionary

How can I print the value of variables in Python if they are dictionary-based?

I'm making a basic dungeon simulator in Python, and I want to know how to work with the variables I'm using. They aren't just like the normal variables; the structure looks like this:
weapons: {
swordType: {
daggers: {
cardboard: {
brokenCardboardDagger: {
damage: 1,
critDamage: 3,
}
}
}
}
}
(There are more swords and more materials, but I am just providing an outline as to what they look like in the code)
So, looking at this type of variable, how would I, for example, print the damage of a sword? Or, how would I concatenate it so that the line of code would look something like this?
print "Your sword's damage is" + [special variable code]
Thanks for the help.
They are perfectly normal; you have nested dictionaries. Use as many indexes as needed:
weapons['swordType']['daggers']['cardboard']['brokenCardboardDagger']['damage']
If you want to have that 'path' stored in a separate variable, use a loop to extract each subsequent nested value, or use reduce(). See Any Functional Programming method of traversing a nested dictionary?

Best way to encode tuples with json

In python I have a dictionary that maps tuples to a list of tuples. e.g.
{(1,2): [(2,3),(1,7)]}
I want to be able to encode this data use it with javascript, so I looked into json but it appears keys must be strings so my tuple does not work as a key.
Is the best way to handle this is encode it as "1,2" and then parse it into something I want on the javascript? Or is there a more clever way to handle this.
You might consider saying
{"[1,2]": [(2,3),(1,7)]}
and then when you need to get the value out, you can just parse the keys themselves as JSON objects, which all modern browsers can do with the built-in JSON.parse method (I'm using jQuery.each to iterate here but you could use anything):
var myjson = JSON.parse('{"[1,2]": [[2,3],[1,7]]}');
$.each(myjson, function(keystr,val){
var key = JSON.parse(keystr);
// do something with key and val
});
On the other hand, you might want to just structure your object differently, e.g.
{1: {2: [(2,3),(1,7)]}}
so that instead of saying
myjson[1,2] // doesn't work
which is invalid Javascript syntax, you could say
myjson[1][2] // returns [[2,3],[1,7]]
If your key tuples are truly integer pairs, then the easiest and probably most straightforward approach would be as you suggest.... encode them to a string. You can do this in a one-liner:
>>> simplejson.dumps(dict([("%d,%d" % k, v) for k, v in d.items()]))
'{"1,2": [[2, 3], [1, 7]]}'
This would get you a javascript data structure whose keys you could then split to get the points back again:
'1,2'.split(',')
My recommendation would be:
{ "1": [
{ "2": [[2,3],[1,7]] }
]
}
It's still parsing, but depending on how you use it, it may be easier in this form.
You can't use an array as a key in JSON. The best you can do is encode it. Sorry, but there's really no other sane way to do it.
Could it simply be a two dimensional array? Then you may use integers as keys

Categories

Resources