how to convert two dimension array to one dimension in python?
On a numpy array a
:
np.squeeze(a)
or
a.flatten()
Convert two dimensional array to one dimensional array in Java
We create a single Integer array with a length determines by the size method. Size takes the 2-dimensional array and finds the sum of all columns in each row and returns that sum. We check if the length is 0, and if so, we return. We then loop through all rows, followed by a loop through all columns in that row. We then assign the value at that row and column to that next index in the array. Unfortunately we can't do any math (to my knowledge) to determine the index based of row/column since they could be variable sizes.
public static int[] twoDConvert(int[][] nums) {
int[] combined = new int[size(nums)];
if (combined.length <= 0) {
return combined;
}
int index = 0;
for (int row = 0; row < nums.length; row++) {
for (int column = 0; column < nums[row].length; column++) {
combined[index++] = nums[row][column];
}
}
return combined;
}
private static int size(int[][] values) {
int size = 0;
for (int index = 0; index < values.length; index++) {
size += values[index].length;
}
return size;
}
Convert a 2D array into a 1D array
You've almost got it right. Just a tiny change:
public static int mode(int[][] arr) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
// tiny change 1: proper dimensions
for (int j = 0; j < arr[i].length; j++) {
// tiny change 2: actually store the values
list.add(arr[i][j]);
}
}
// now you need to find a mode in the list.
// tiny change 3, if you definitely need an array
int[] vector = new int[list.size()];
for (int i = 0; i < vector.length; i++) {
vector[i] = list.get(i);
}
}
How can you convert a 2 dimensional array into a 1 dimensional array in Java
public static String[] flatten(String[][] data) {
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < data.length; i++) {
for(int j = 0; j < data[i].length; j++){
list.add(data[i][j]);
}
}
return list.toArray(new String[0]);
}
Or add whole rows at one time:
for(int i = 0; i < data.length; i++) {
list.addAll( Arrays.asList(data[i]) );
}
Edit:
From comments on my answer it seems like this is what the OP wanted (i.e. converting each row of 2d array to some string representation of it):
public static String[] rowsToString(String[][] data) {
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < data.length; i++) {
String row = Arrays.toString(data[i]);
list.add( row.substring(1, row.length()-1) );
}
return list.toArray(new String[0]);
}
Converting 2 dimensional array to Single dimensional in C#?
You can use the Buffer.BlockCopy Method:
byte[,] bData = (byte[,])objTransLog;
byte[] baData = new byte[bData.Length];
Buffer.BlockCopy(bData, 0, baData, 0, bData.Length);
Example:
byte[,] bData = new byte[4, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
byte[] baData = new byte[bData.Length];
Buffer.BlockCopy(bData, 0, baData, 0, bData.Length);
// baData == { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
Convert a 2D JavaScript array to a 1D array
Try .concat()
:
var arrToConvert = [[0,0,1],[2,3,3],[4,4,5]];
var newArr = [];
for(var i = 0; i < arrToConvert.length; i++)
{
newArr = newArr.concat(arrToConvert[i]);
}
console.log(newArr);
Convert a one dimensional array to two dimensional array
Assuming you want to remove all quotation marks as per your question:
$oldArray = array('id,"1"', 'name,"abcd"', 'age,"30"')
$newArray = array();
foreach ($oldArray as $value) {
$value = str_replace(array('"',"'"), '', $value);
$parts = explode(',', $value);
$newArray[] = $parts;
}
Related Topics
Pip Installation /Usr/Local/Opt/Python/Bin/Python2.7: Bad Interpreter: No Such File or Directory
Find the Longest Substring in Alphabetical Order
How to Append Two Bytes in Python
Django - How to Retrieve Data in Database in Dropdownlist
Modulenotfounderror: What Does It Mean _Main_ Is Not a Package
Python: Pickle.Load() Raising Eoferror
Python Anaconda - How to Safely Uninstall
Pythonic Way to Check If Two Dictionaries Have the Identical Set of Keys
Python: How to Calculate the Sum of Numbers from a File
Python While Loop for Finding Prime Numbers
How to Do This Horizontally Instead of Vertically in Python
How to Add Thousand Separator to Numbers in Python Pandas Dataframe
Asking the User for Input Until They Give a Valid Response
How to Send Smtp Email for Office365 With Python Using Tls/Ssl
Pyside - Pyqt:How to Make Set Qtablewidget Column Width as Proportion of the Available Space
Subtract One Dataframe from Another Excluding the First Column Pandas
Valueerror: Feature_Names Mismatch: in Xgboost in the Predict() Function