How to get most nested values? - python-2.7

I got a nested dictionary that looks like this:
d = {"my momma" : {"your momma" : 1}, "flying on" : {"a broom" : 2}}
I need to multiply all most nested values (the 1 and the 2) by 2.
How do I do this?
I just cant manage to access them.

Recursion, if you don't know how many nesting levels you will have:
INDEX = []
def disMantle(target, depth):
# ensure a list for given depth
while len(INDEX) <= depth: INDEX.append([])
# analyze given target
for key in target:
atype = type(target[key])
if atype == dict:
# next depth
disMantle(target[key], depth+1)
elif atype in [int,float]:
# record the numeric values
INDEX[depth].append({'key':key,'value':target[key]})
d = {"my momma" : {"your momma" : 1}, "flying on" : {"a broom" : 2.0}}
disMantle(d, 0)
print INDEX[-1]

for key in d:
for skey in d[key]:
d[key][skey] *= 2
print d

Related

pyomo: an indexed variable should be linear or integer depending on the variable index

I have a indexed variable New_UnitsBuilt[p] and this variabele should be integer for the index "GasPowerplant"
but linear for the index "batterystorage".
new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
model.New_UnitsBuilt = pyo.Var(new_units_built_set, domain=(pyo.NonNegativeIntegers if p="GasPowerplant" else NonNegativeReals)
Please help me how to do this in pyomo.
I am new in pyomo
Best Greetings
Gerhard
There are a couple ways you can accomplish this. For the following, I am assuming that your params.Installable_units = ["GasPowerplant", "batterystorage"]:
If the number of elements in new_units_built_set is small, then you can use a dictionary:
model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
model.New_UnitsBuilt = pyo.Var(model.new_units_built_set,
domain={"GasPowerplant": pyo.NonNegativeIntegers, "batterystorage": pyo.NonNegativeReals})
Or if there are a lot – or there is a simple formula to get the return value – you can use a function (rule):
model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
def _new_unitsbuilt_domain(m, p):
return pyo.NonNegativeIntegers if p=="GasPowerplant" else pyo.NonNegativeReals
model.New_UnitsBuilt = pyo.Var(model.new_units_built_set, domain=_new_unitsbuilt_domain)
Or you can just set everything to one value and override later (assuming you are using a ConcreteModel):
model.new_units_built_set = pyo.Set(initialize=list(params.Installable_units))
model.New_UnitsBuilt = pyo.Var(model.new_units_built_set, domain=pyo.NonNegativeReals)
model.New_UnitsBuilt["GasPowerplant"].domain = pyo.NonNegativeIntegers
All of these will produce:
>>> model.pprint()
1 Set Declarations
new_units_built_set : Size=1, Index=None, Ordered=Insertion
Key : Dimen : Domain : Size : Members
None : 1 : Any : 2 : {'GasPowerplant', 'batterystorage'}
1 Var Declarations
New_UnitsBuilt : Size=2, Index=new_units_built_set
Key : Lower : Value : Upper : Fixed : Stale : Domain
GasPowerplant : 0 : None : None : False : True : NonNegativeIntegers
batterystorage : 0 : None : None : False : True : NonNegativeReals
2 Declarations: new_units_built_set New_UnitsBuilt

Merge lists with 0 1 encoding

I have the following case in python:
a = [[0,0,1,0],
[0,0,0,1],
[1,0,0,1],
[1,0,1,1]]
b = [[1,1,0,0],
[1,0,0,1],
[0,1,0,0]]
c = [[1,0,1,0],
[0,0,1,0],
[0,1,0,0]]
d = [[1,0,1,0],
[0,0,1,0],
[0,0,0,0],
[0,0,0,1],
[1,0,0,0]]
a has length 4, b has length 3, c has length 3, d has length 4 and I have several more lists with variable length.
What I want is to construct a function that can merge the "sub lists" considering the columns, for example:
def combine(foo):
...
print(foo)
combine(a) = [1,0,1,1]
combine(b) = [1,1,0,1]
combine(c) = [1,1,1,0]
combine(d) = [1,0,1,1]
How can I do it?
Thanks for your help.

Unable to understand lists in dart consider the example provided

I am developing an app in flutter. For which I am using lists of map but there something that I am unable to undertand. Consider the following cases:
SCENARIO 1
void main() {
List<Map<String,String>> _reminders = [];
Map<String , String> _tempMap = {};
for (int i = 0; i < 5; i++) {
_tempMap.clear();
_tempMap.putIfAbsent('M' , () => 'm ' + i.toString());
_tempMap.putIfAbsent('D' , () => 'd : ' + i.toString());
_reminders.add(_tempMap);
// or _reminders.insert(i, _tempMap);
}
print(_reminders.toString());
return;
}
to which the result is as follows
[{M: m 4, D: d : 4}, {M: m 4, D: d : 4}, {M: m 4, D: d : 4}, {M: m 4, D: d : 4}, {M: m 4, D: d : 4}]
SCENARIO 2
void main() {
List<Map<String,String>> _reminders = [];
for (int i = 0; i < 5; i++) {
Map<String , String> _tempMap = {};
_tempMap.putIfAbsent('M' , () => 'm ' + i.toString());
_tempMap.putIfAbsent('D' , () => 'd : ' + i.toString());
_reminders.add(_tempMap);;
}
print(_reminders.toString());
return;
}
to which the result is as follows
[{M: m 0, D: d : 0}, {M: m 1, D: d : 1}, {M: m 2, D: d : 2}, {M: m 3, D: d : 3}, {M: m 4, D: d : 4}]
As far as I understand, these scenarios should give similar results. Also in my use case scenario 2 is the correct way as it gives me the result that I want. Please note the above examples have been changed to similify the question. The usage in my original code is much more complex.
Dart, like many other programming languages including java, stores objects as reference, and not contiguous memory blocks. In the first case, in all the iterations of the loop, you have added the same Map using the _reminders.add(_tempMap). Your intuition that "Everytime I add the Map, a copy is created of the current state of Map and that copy is appended to the list" is incorrect.
From my understanding, both are different
The problem is with _tempMap.clear(); in the SCENARIO 1. You have used the global variable for map object and when you apply clear inside the for loop all the previously added entries will be cleared and map becomes empty.
when i = 0 => {} => clear() => all entries will be cleared => New item inserted.
when i = 1 => {"Item inserted in 0th iteration"} => clear() => all entries will be cleared => New item inserted.
So for every iteration map is cleared and holds only last iterated value. After for loop is completed it contains only the last iterated value(i=4) since we are clearing the global map variable every time when a new iteration starts.
EDIT :
You can print the map values inside the for loop and can check yourself.
for (int i = 0; i < 5; i++) {
print('\n $i => ${_tempMap} \n');

dictionary: Unique relative values where values are of list type

I am getting the output of word2vec_basic.py in the following format
Nearest to key1 : node1, node2, node3 ..
Nearest to key2 : node2, node4, node5 ..
This implies that node2 is comparatively closer to key2 over key1 (Please correct me if I am wrong, as I am newbie here)
It would be great if I get the output in the following format
Nearest to key1 : node1, node3 , node6..
Nearest to key2 : node2, node4, node5 ..
That is, consider only the closest neighbor for clustering.
Suggestions for the same?
I am maintaining a python dictionary for the same of the following format:
{
key1: [node1,node2,node3],
key2: [node2,node4,node5]
}
But I required,
{
key1: [node1,node3,node6],
key2: [node2,node4,node5]
}
And for the above dictionary, I will be needing
Nearest to key1 : node1, node3 , node6..
Nearest to key2 : node2, node4, node5 ..
Could we do this in tensorflow itself, or should I define a function which takes dictionary as input and give me the required output?
For eg:
If we have a python dictionary of the following format:
{
a: ["abc","bcd","def"],
b: ["def","xyz"]
}
Here the values are list. I am looking for the following format from the above input:
{
a: ["abc","bcd"],
b: ["def","xyz"]
}
Suggestions are welcome on how I could achieve it.
Also, are there any python in built functions which could help me to reach the above output format?
dicts are unordered so which dupe gets removed is not guaranteed but you can keep a set of elements seen so far as you iterate over the items, updating/removing elements from the list/value if it has already been seen:
d = {
"a": ["abc","bcd","def"],
"b": ["def","xyz"]
}
seen = set()
for k,v in d.items():
d[k] = [seen.add(ele) or ele for ele in v if ele not in seen]
print(d)
This could output:
{'b': ['def', 'xyz'], 'a': ['abc', 'bcd']}
Or:
d = { "a": ["abc","bcd","def"], "b": ["xyz"]}
It completely depends on which key you hit first.
As you can see from this top answer with 436 upvotes, the removal logic is efficient and it maintains the order if required. To also to avoid the set.add lookup each time as in the link, you can set seen_add = seen.add and use seen._add(ele) in place of seen.add.
Since dictionaries entries in Python are unordered, you need to first build a separate dictionary keyed by node recording each list (or sequence) it's in as well as its index in that list so relative distances in each list can be compared to one another. After that's done, it can be referenced to determine whether each node should stay in each list it is in or not by making a second pass through the dictionary's contents.
d = {
"a": ["abc", "bcd", "def"],
"b": ["def", "xyz"]
}
def check_usage(k, elem_usage):
if len(elem_usage) == 1: # unique?
return True
else:
index = elem_usage[k] # within this elem's seq
for key,value in elem_usage.items():
if key != k:
if value < index:
return False
else:
return True
usage = {}
for key in d: # build usage dictionary
for index, item in enumerate(d[key]):
usage.setdefault(item, {})[key] = index
for k,seq in d.items():: # remove nodes that are closer in other lists
d[k] = [elem for elem in seq if check_usage(k, usage[elem])]
# display results
print('{')
for k in sorted(d):
print(' {!r}: {},'.format(k, d[k]))
print('}')
Output:
{
'a': ['abc', 'bcd'],
'b': ['def', 'xyz'],
}

Computing all values or stopping and returning just the best value if found

I have a list of items and for each item I am computing a value. Computing this value is a bit computationally intensive so I want to minimise it as much as possible.
The algorithm I need to implement is this:
I have a value X
For each item
a. compute the value for it, if it is < 0 ignore it completely
b. if (value > 0) && (value < X)
return pair (item, value)
Return all (item, value) pairs in a List (that have the value > 0), ideally sorted by value
To make it a bit clearer, step 3 only happens if none of the items have a value less than X. In step 2, when we encounter the first item that is less than X we should not compute the rest and just return that item (we can obviously return it in a Set() by itself to match the return type).
The code I have at the moment is as follows:
val itemValMap = items.foldLeft(Map[Item, Int)]()) {
(map : Map[Item, Int], key : Item) =>
val value = computeValue(item)
if ( value >= 0 ) //we filter out negative ones
map + (key -> value)
else
map
}
val bestItem = itemValMap.minBy(_._2)
if (bestItem._2 < bestX)
{
List(bestItem)
}
else
{
itemValMap.toList.sortBy(_._2)
}
However, what this code is doing is computing all the values in the list and choosing the best one, rather than stopping as a 'better' one is found. I suspect I have to use Streams in some way to achieve this?
OK, I'm not sure how your whole setup looks like, but I tried to prepare a minimal example that would mirror your situation.
Here it is then:
object StreamTest {
case class Item(value : Int)
def createItems() = List(Item(0),Item(3),Item(30),Item(8),Item(8),Item(4),Item(54),Item(-1),Item(23),Item(131))
def computeValue(i : Item) = { Thread.sleep(3000); i.value * 2 - 2 }
def process(minValue : Int)(items : Seq[Item]) = {
val stream = Stream(items: _*).map(item => item -> computeValue(item)).filter(tuple => tuple._2 >= 0)
stream.find(tuple => tuple._2 < minValue).map(List(_)).getOrElse(stream.sortBy(_._2).toList)
}
}
Each calculation takes 3 seconds. Now let's see how it works:
val items = StreamTest.createItems()
val result = StreamTest.process(2)(items)
result.foreach(r => println("Original: " + r._1 + " , calculated: " + r._2))
Gives:
[info] Running Main
Original: Item(3) , calculated: 4
Original: Item(4) , calculated: 6
Original: Item(8) , calculated: 14
Original: Item(8) , calculated: 14
Original: Item(23) , calculated: 44
Original: Item(30) , calculated: 58
Original: Item(54) , calculated: 106
Original: Item(131) , calculated: 260
[success] Total time: 31 s, completed 2013-11-21 15:57:54
Since there's no value smaller than 2, we got a list ordered by the calculated value. Notice that two pairs are missing, because calculated values are smaller than 0 and got filtered out.
OK, now let's try with a different minimum cut-off point:
val result = StreamTest.process(5)(items)
Which gives:
[info] Running Main
Original: Item(3) , calculated: 4
[success] Total time: 7 s, completed 2013-11-21 15:55:20
Good, it returned a list with only one item, the first value (second item in the original list) that was smaller than 'minimal' value and was not smaller than 0.
I hope that the example above is easily adaptable to your needs...
A simple way to avoid the computation of unneeded values is to make your collection lazy by using the view method:
val weigthedItems = items.view.map{ i => i -> computeValue(i) }.filter(_._2 >= 0 )
weigthedItems.find(_._2 < X).map(List(_)).getOrElse(weigthedItems.sortBy(_._2))
By example here is a test in the REPL:
scala> :paste
// Entering paste mode (ctrl-D to finish)
type Item = String
def computeValue( item: Item ): Int = {
println("Computing " + item)
item.toInt
}
val items = List[Item]("13", "1", "5", "-7", "12", "3", "-1", "15")
val X = 10
val weigthedItems = items.view.map{ i => i -> computeValue(i) }.filter(_._2 >= 0 )
weigthedItems.find(_._2 < X).map(List(_)).getOrElse(weigthedItems.sortBy(_._2))
// Exiting paste mode, now interpreting.
Computing 13
Computing 1
defined type alias Item
computeValue: (item: Item)Int
items: List[String] = List(13, 1, 5, -7, 12, 3, -1, 15)
X: Int = 10
weigthedItems: scala.collection.SeqView[(String, Int),Seq[_]] = SeqViewM(...)
res27: Seq[(String, Int)] = List((1,1))
As you can see computeValue was only called up to the first value < X (that is, up to 1)