Map Array of Strings to an Array of Integers

Java map an array of Strings to an array of Integers

Since String and Integer are both reference types you can simply call Stream::map to transform your array.

Integer[] boxed = Stream.of(myarray).map(Integer::valueOf).toArray(Integer[]::new);

Converting string to an int array using map

list(map(int, list(mystr)))

Should do the job. You can't split on an empty string, but Python supports casting str to list as a built-in. I wouldn't use map for situations like these though, but instead use a list comprehension:

[int(x) for x in mystr]

Map a NumPy array of strings to integers

You can use np.unique with the return_inverse argument:

>>> lookupTable, indexed_dataSet = np.unique(dataSet, return_inverse=True)
>>> lookupTable
array(['george', 'greg', 'kevin'],
dtype='<U21')
>>> indexed_dataSet
array([2, 1, 0, 2])

If you like, you can reconstruct your original array from these two arrays:

>>> lookupTable[indexed_dataSet]
array(['kevin', 'greg', 'george', 'kevin'],
dtype='<U21')

If you use pandas, lookupTable, indexed_dataSet = pd.factorize(dataSet) will achieve the same thing (and potentially be more efficient for large arrays).

Convert array of strings to an array of integers

To convert a string to number you have the to_i method.

To convert an array of strings you need to go through the array items and apply to_i on them. You can achieve that with map or map! methods:

> ["", "22", "14", "18"].map(&:to_i)
# Result: [0, 22, 14, 18]

Since don't want the 0 - just as @Sebastian Palma said in the comment, you will need to use an extra operation to remove the empty strings: (The following is his answer! Vote for his comment instead :D)

> ["", "22", "14", "18"].reject(&:empty?).map(&:to_i)
# Result: [22, 14, 18]

the difference between map and map! is that map will return a new array, while map! will change the original array.

How to convert a string of numbers to an array of numbers?

My 2 cents for golfers:

b="1,2,3,4".split`,`.map(x=>+x)

backquote is string litteral so we can omit the parenthesis (because of the nature of split function) but it is equivalent to split(','). The string is now an array, we just have to map each value with a function returning the integer of the string so x=>+x (which is even shorter than the Number function (5 chars instead of 6)) is equivalent to :

function(x){return parseInt(x,10)}// version from techfoobar
(x)=>{return parseInt(x)} // lambda are shorter and parseInt default is 10
(x)=>{return +x} // diff. with parseInt in SO but + is better in this case
x=>+x // no multiple args, just 1 function call

I hope it is a bit more clear.

Javascript - Converting string to number in an array of objects

You are over-complicating things; you just need to map each entry in reports to its reportId property and convert that to an integer

let reports = [ 
{reportId: "21", title: "Online", code: "ON" },
{reportId: "11", title: "Retail", code: "RE" },
{reportId: "61", title: "Walk-in", code: "WI" }
]

let ids = reports.map(function (item) {
return parseInt(item.reportId, 10);
});

console.log(ids)


Related Topics



Leave a reply



Submit