Convert dictionary values into array
// dict is Dictionary<string, Foo>
Foo[] foos = new Foo[dict.Count];
dict.Values.CopyTo(foos, 0);
// or in C# 3.0:
var foos = dict.Values.ToArray();
Convert dictionary values to numpy arrays
You can do this with a small dict comprehension.
import numpy as np
def convert_to_array(dictionary):
'''Converts lists of values in a dictionary to numpy arrays'''
return {k:np.array(v) for k, v in dictionary.items()}
d = {
'date-1': [1.23, 2.34, 3.45, 5.67],
'date-2': [54.47, 45.22, 22.33, 54.89],
'date-3': [0.33, 0.589, 12.654, 4.36]
}
print(convert_to_array(d))
# {'date-1': array([1.23, 2.34, 3.45, 5.67]), 'date-2': array([54.47, 45.22, 22.33, 54.89]), 'date-3': array([ 0.33 , 0.589, 12.654, 4.36 ])}
Convert Dictionary values (arrays) into lists
For values do:
>>> d={'a':[1,2,3],'b':['a','b','c']}
>>> d.values()
dict_values([[1, 2, 3], ['a', 'b', 'c']])
>>> list(d.values())
[[1, 2, 3], ['a', 'b', 'c']]
For both keys and values:
>>> list(d.items())
[('a', [1, 2, 3]), ('b', ['a', 'b', 'c'])]
To answer your question do:
>>> import numpy as np
>>> d = {
'A': np.array([2623.8374, -1392.9608, 416.2083, -1596.7402,], dtype=np.float32),
'B': np.array([1231.1268, -963.2312, 1823.7424, -2295.1428,], dtype=np.float32),
}
>>> {k:v.tolist() for k,v in d.items()}
{'A': [2623.83740234375, -1392.9608154296875, 416.20831298828125, -1596.740234375], 'B': [1231.1268310546875, -963.231201171875, 1823.742431640625, -2295.142822265625]}
>>>
Convert Dictionary Values into Array in Python
Since you aggregated into a set, your numpy array isn't actually an array. To get the list back you can do list(list2array.item()) and index on that
If you change your list(userdic.values())[0] to list(userdic.values()[0]), your initial list will actually be a list instead of a set and the array will get initiated properly
Convert Dictionary having array value for keys into a list in Javascript
You could map the key/values in a new array.
var data = { a : [5, 10], b : [1, 12] , c : [6, 7]}, result = Object.entries(data).map(([k, v]) => [k, ...v]);
console.log(result);
How to convert dictionary to array
You can use a for loop to iterate through the dictionary key/value pairs to construct your array:
var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]
var arr = [String]()
for (key, value) in myDict {
arr.append("\(key) \(value)")
}
Note: Dictionaries are unordered, so the order of your array might not be what you expect.
In Swift 2 and later, this also can be done with map
:
let arr = myDict.map { "\($0) \($1)" }
This can also be written as:
let arr = myDict.map { "\($0.key) \($0.value)" }
which is clearer if not as short.
Turn dictionary values into an array python
You can do:
dict2 = {k: list(v) for k, v in dict1.items()}
How to convert Dictionary<int, long> to string array/list?
var strings = dict.Select(item => string.Format("{0}, {1}", item.Key, item.Value));
Note that this returns an enumerator. Whether you want the result in the form of string[]
or List<string>
you should use .ToArray()
or .ToList()
, respectively.
Can't convert dictionary to the array. Swift
Just delete the arrayLiteral:
thing and it will work!
var months_keys = Array(array!.keys) //that does not work
The arrayLiteral
initializer should be used like this:
var month_keys = Array(arrayLiteral: 1, 2, 3, 4)
// will produce an array with items: 1, 2, 3 and 4
What you should call instead is the (_: SequenceType)
initializer, since LazyMapCollection<[String : Int], String>
conforms to that protocol.
A few more tips for your code:
If a variable's value is not going to change, declare it with
let
, instead ofvar
.You can simplify this:
-
if (customerViewModel.customer._dynamicMonthCount != nil) {
var array = customerViewModel.customer._dynamicMonthCount
var months_keys = Array(array!.keys)
}
to this:
if let dictionary = customerViewModel.customer._dynamicMonthCount {
var months_keys = Array(dictionay!.keys)
}
Related Topics
Split String and Keep Delimiter in Sequence
How to Read Appsettings.Json With Array of Values
Using X-Alt-Desc/Applying HTML to Calendar Invites in Outlook
Regular Expression for Valid Filename
How to Check If Multiple Values in an Array Are Equal to One String
C# Download Big File from Server With Less Memory Consumption
How to Use a Nuget Package Within a Powershell Script
Convert Json File to C# and Stored in Database
How to Make Xmlserializer Ignore the Namespace on Deserialization
How to Call Wcf Service Method from Postman
How to Open the Default Chrome Profile Through Selenium, Chromedriver and Googlechrome
How to Print the Elements With Text Value That Contains in a List Selenium C#
Ssh.Net Sftp Get a List of Directories and Files Recursively
Lambda Where Id Does Not Exist in Another List
Map a Property to List of Object Using Automapper Createmap
How to Write Data on Multiple Lines But Within the Same Cell of Csv