node.js: using foreach with a multidimensional array - node.js

im trying to call a function with every line of an array which is an array itself. So far I always get an error with my code, which I cant seem to straighten out. Can someone tell me how to do it right?
The output should be "['a', 4], ['b', 1], ['j', 2], ['c', 3], ['d', 5], ['e', 2], ['f', 2], ['g', 6]".
var testqueue = [['a', 4], ['b', 1], ['j', 2], ['c', 3], ['d', 5], ['e', 2], ['f', 2], ['g', 6]];
function test(cb){
testqueue.foreach(function(item){
console.log(item);
});
cb('done');
}
test(function(result){
console.log(result);
});

Sorry it was only a typo :(
it should be forEach (capital E) and everything works fine.

Related

Converting/Merging a dictionary based on 2nd dictionary

I am on Python 3.9
What I have is two dictionaries.
dict1= {'ZS': [1, 2], 'ZP': [3], 'XS': [4, 5], 'XP': [6, 7]}
dict2= {'a': {}, 'b' : {}}
how do I take every two keys of dict1 and add them as dict-value in dict2.
Like below
required dict
result_dict= {'a': {'ZS': [1, 2], 'ZP': [3]}, 'b' : {'XS': [4, 5], 'XP': [6, 7]}}
The problem of your question is that you didn't explain clearly what is the criteria of the splitting.
Another problem is that when you iniciate a dictionary inside a dictionary, like you did in dict2. if you send 'a' or 'b' values to a variable for example c:
c = dict2['a']
It won't create a new { }, it will pass the reference of the '{ }' inside dict2['a'] or dict2['b'] and when you change c it will change both c and dict2['a'] value
That is quite hacky, got stuck while trying to make a solution hehehe.
If the criteria you are using is the first letter of the key, you can do:
dict1 = {'ZS': [1, 2], 'ZP': [3], 'XS': [4, 5], 'XP': [6, 7]}
dict2 = {'a': {}, 'b': {}}
result_dict = {}
for x in dict2.keys():
result_dict[x] = {}
for key in dict1.keys():
if key[0:1] == "Z":
result_dict['a'][key] = dict1[key]
elif key[0:1] == "X":
result_dict['b'][key] = dict1[key]
print(dict1)
print(dict2)
print(result_dict)
{'ZS': [1, 2], 'ZP': [3], 'XS': [4, 5], 'XP': [6, 7]}
{'a': {}, 'b': {}}
{'a': {'ZS': [1, 2], 'ZP': [3]}, 'b': {'XS': [4, 5], 'XP': [6, 7]}}
You could do something like:
keys1 = list(dict1)
keys2 = list(dict2)
result_dict = {
keys2[i]: {
keys1[2*i]:dict1[keys1[2*i]],
keys1[2*i+1]:dict1[keys1[2*i+1]]
}
for i in range(len(keys2))
}
But in my opinion it is a really bad habit to trust dictionary key's order (unless you use OrderedDict)

Groovy list of lists to map with first list as keys

How would I go in Groovy idiomatically (with collection methods?) from
[['a', 'b', 'c'], [1, 2, 3], [4, 5, 6]]
to
[[a: 1, b: 2, c: 3], [a: 4, b: 5, c: 6]]
def x=[['a', 'b', 'c'], [1, 2, 3], [4, 5, 6]]
x.tail().collect{v-> x.head().indexed().collectEntries{i,k-> [k, v[i]] } }

Add two lists onto another list which [duplicate]

This question already has answers here:
How to merge lists into a list of tuples?
(10 answers)
Closed 4 years ago.
So I have two lists right now.
list1 = ['A', 'B', 'C', 'D']
list2 = [1, 2, 3, 4]
How can I merge the lists together to make it look something like this:
list3 = [['A', 1], ['B', 2], ['C', 3], ['D', 4]]
Basically I want to make a list inside a list.
I've been trying for loops but nothing seems to be working for me.
Try this:
list1 = ['A', 'B', 'C', 'D']
list2 = [1, 2, 3, 4]
result = [list(i) for i in zip(list1,list2)]
print(result)
Output:
[['A', 1], ['B', 2], ['C', 3], ['D', 4]]

Python - How does appending in a list and changing an element in a list differs in case of shallow copy

Okay, so I had a doubt regarding the difference between the append and change operation in lists with respect to shallow copy.
Below is the output:
***After Shallow Copy***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]
***After appending in list a (Here appending in list a doesn't affect list b)***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]
***After appending in list b (Same as above appending in b doesn't affect a)***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [2, 'b', False]]
***After changing 2 to 10 in list a (But changing an element gets reflected in both)***
a = [[1, 10, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 10, 3], ['a', 'b', 'c'], [True, False], [2, 'b', False]]
***After changing a to z in list b (Same as above changed an element in b and reflected in both)***
a = [[1, 10, 3], ['z', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 10, 3], ['z', 'b', 'c'], [True, False], [2, 'b', False]]
STRAIGHT OUTA Python's DOCUMENTATION:
A shallow copy constructs a new compound object and then (to the
extent possible) inserts references into it to the objects found in
the original.
A deep copy constructs a new compound object and then,
recursively, inserts copies into it of the objects found in the
original.
For more info, refer here: https://realpython.com/copying-python-objects/

How to filter similar edges in a list?

I have a list which contains all the edges of an undirected weighted graph.I further sorted it in increasing order of their edge weights.
The list is as follows
lis = [['B', 'C', 1], ['C', 'B', 1], ['B', 'A', 2], ['C', 'A', 2], ['A', 'C', 2], ['A', 'B', 2], ['D', 'C', 3], ['C', 'D', 3], ['B', 'D', 5], ['D', 'B', 5]]
Now in the list, ['B', 'C', 1], ['C', 'B', 1] means the same thing.That is an edge BC has a weight of 1 and edge CB has a weight of 1.Similarly we have edges AB, BA and so on in the list.
How do I keep only one of the two edges and not both since they both mean the same thing.So for ['B', 'C', 1], ['C', 'B', 1], I only want to keep say ['B', 'C', 1].How do I do that?
I found a similar question but I am not really sure how to implement it in my case.
Removing duplicate edges from graph in Python list
I tried to implement the solution posted in the link for my situtation but doesn't work
def normalize(t):
[n1, n2, dist] = t
if n1 < n2:
return t
else:
return [n2, n1, dist]
unique_edges = set(map(normalize, lis))
Simply sort each item and remove duplicates:
>>> lis = sorted([sorted(item) for item in lis])
>>> [lis[i][::-1] for i in range(len(lis)) if i == 0 or lis[i] != lis[i-1]]
[['C', 'B', 1], ['B', 'A', 2], ['C', 'A', 2], ['D', 'C', 3], ['D', 'B', 5]]

Resources