unable to perform list operation on dict keys, and values: - python-3.7

I tried the below code to perform list operation on a dictionary k, v like given below:
for k, v in dictionary:
print(f"{k}, {v}")
but getting error as giving below:
can't perform loop on dictionary directly.

Try using items() method it returns the dictionary as 2d array
for key , val in dicObj.items():
print(f"{key}, {val}")
or
for key in dicObj.keys():
print(f"{key}, {dicObj[key]}")

Related

How do i use joined() function after using sorted() in swift?

let var1 = "AnyCode".sorted()
print(var1.joined(separator:""))
ERROR: No exact matches in call to instance method 'joined'
I am trying to join the array after sorting the string. = "AnyCode"
I was expecting the output was = ACdenoy
But it is giving an error.
A Swift String is a collection of Characters, and sorted() applied to a collection returns an array with the collection elements in sorted order.
So var1 has the type [Character], and you can simply create a new string from that array with:
let var1 = "AnyCode".sorted()
print(String(var1)) // ACdenoy
Alternatively to Martin R's answer (but not better than that answer), you might have said
print(var1.map(String.init).joined())
...turning the characters to strings before trying to join the array elements.

Gets elements from 3 dimensional array

I'm new in Swift and do not know everything , i have this kind of an array
var arrayOFJumpForwardBack: [Int: [String: [String]]] =
[1: ["chapter": ["15 sec","30 sec","45 sec","60 sec"]]]
and , i'd like to know how can i get certain elements from this array?
Swift - Arrays & Dictionaries
The syntax of an array is as follows:
["value1","value2","value3"]
The syntax of a dictionary is as follows:
["ABC":"valueForABC","XYZ":"valueForXYZ"]
In this case you're using a dictionary, and the main difference with the array is that the order doesn't matter, each value is represented with a key, so with the following example:
Example:
Let's say we have a dictionary where we have stored the word "apple" whose key is "JHC" and "pear" whose key is "IOP". We want to print "I have an apple", then:
var myFridge:[String:String] = ["JHC":"apple","IOP":"pear"]
print("I have an \(myFridge["JHC"]!)")
A multidimensional array is just an array inside of another, x dimensions you want.
I'd rather prever to create a dictionary as above ↑

How to create an associative array from a range in D

Let's say I have an array of type Record[] and I want to create an associative array from it, key being rec.key. Is there an easy way to do it?
Yes, you can use std.array, std.typecons and std.algorithm libraries and construct this one-liner:
Record[Key] assocArray = array.map!( item => tuple( item.key, item ) ).assocArray;
It takes array, maps it to a tuple (Key, Record) and then takes that list of tuples and creates an associative array from it.

How do I use containers.Map in Matlab with a cell array as the keys and a vector of integers as the values

I have a cell array that contains words like 'a', 'b', and 'c' for example. What I want to be able to do is use Matlab's containers.Map to make a hash table that I can add values to for each key and be able to look them up quickly. I am able to do this if I do not initialize my containers.Map object beforehand as follows but it does not allow me (or at least I haven't found a way) to add more key/value pairs later and makes it so that I have to reinitialize the object during each iteration of a loop:
key = {'a','b','c'};
newmap = containers.Map(key,[1,2,3]);
My problem is that I need to be able to continually add new keys to the hash table and therefore cannot keep initializing the containers.Map object each time, I want one long hash table with all the keys and values that I get while in a loop.
Here is the code that I am trying to get working, I want to be able to add the keys to the containers.Map object newmap and their corresponding values at the same time. The keys are always strings in a cell array and the values are always integers:
key = {'a','b','c'};
val = [1,2,3];
newmap = containers.Map(); % containers.Map object initialization
newmap(key) = val;
My desired output would something like this:
newmap(key)
ans = 1 2 3
Attempts at solving this:
I have tried converting the cell array of keys using cellstr() and char() but haven't had any luck with these. I seem to keep getting this error when trying this:
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
Thanks for any help.
% Initialize
map = containers.Map('KeyType','char','ValueType','double');
% Assume you get these incrementally
key = {'a','b','c'};
val = [1,2,3];
% Add incrementally
for ii = 1:numel(key)
map(key{ii}) = val(ii);
end
You can retrieve all values at once for a given set of keys, but you will get a cell array. No way around it, but you can convert with cell2mat(), or retrieve incrementally with a loop map(key{ii})
% Retrieve all
values(map,key)
ans =
[1] [2] [3]

Scala create array of empty arrays

I am trying to create an array where each element is an empty array.
I have tried this:
var result = Array.fill[Array[Int]](Array.empty[Int])
After looking here How to create and use a multi-dimensional array in Scala?, I also tried this:
var result = Array.ofDim[Array[Int]](Array.empty[Int])
However, none of these work.
How can I create an array of empty arrays?
You are misunderstanding Array.ofDim here. It creates a multidimensional array given the dimensions and the type of value to hold.
To create an array of 100 arrays, each of which is empty (0 elements) and would hold Ints, you need only to specify those dimensions as parameters to the ofDim function.
val result = Array.ofDim[Int](100, 0)
Array.fill takes two params: The first is the length, the second the value to fill the array with, more precisely the second parameter is an element computation that will be invoked multiple times to obtain the array elements (Thanks to #alexey-romanov for pointing this out). However, in your case it results always in the same value, the empty array.
Array.fill[Array[Int]](length)(Array.empty)
Consider also Array.tabulate as follows,
val result = Array.tabulate(100)(_ => Array[Int]())
where the lambda function is applied 100 times and for each it delivers an empty array.