如果您想了解如何在python中按行对二维数组排序?和如何在python中按行对二维数组排序的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在python中按行对二维数组排序?的各个方面,并为
如果您想了解如何在python中按行对二维数组排序?和如何在python中按行对二维数组排序的知识,那么本篇文章将是您的不二之选。我们将深入剖析如何在python中按行对二维数组排序?的各个方面,并为您解答如何在python中按行对二维数组排序的疑在这篇文章中,我们将为您介绍如何在python中按行对二维数组排序?的相关知识,同时也会详细的解释如何在python中按行对二维数组排序的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- 如何在python中按行对二维数组排序?(如何在python中按行对二维数组排序)
- C#实现对二维数组排序的方法
- java – 如何按字典顺序对二维数组进行排序?
- Java根据一列对二维数组排序
- PHP 对二维数组排序
如何在python中按行对二维数组排序?(如何在python中按行对二维数组排序)
我有2d数组,尺寸为3x10,我想按从低到高的第二行中的值进行排序。
答案1
小编典典Python本身没有“
2d数组”-它具有(1d)内置列表,以及(1d)标准库模块array中的数组。有第三方库,例如numpy
它确实提供了Python的使用多维数组,当然你会提这样的第三方库,如果你是“Python中的”使用其中的一部分,而不是只是说,
正确的 ? -)
因此,我假设“ 2d数组”是指列表列表,例如:
lol = [ range(10), range(2, 12), range(5, 15) ]
或类似内容-
即包含3个项目的列表,每个项目都是包含10个项目的列表,而“第二行”将是子列表项lol[1]
。是的,有很多假设,但是您的问题是如此的令人困惑,以至于无法避免做出假设-
如果您不喜欢尝试读心术的人(可能是失败的话),请编辑您的Q以更精确地进行澄清,并举一个例子!因为您目前无法避免。
因此,在这些假设下,您可以按照对第二个子列表进行排序所需的顺序对3个子列表中的每个列表进行排序,例如:
indices = range(10)indices.sort(key = lol[1].__getitem__)for i, sublist in enumerate(lol): lol[i] = [sublist[j] for j in indices]
此处的一般方法是对索引范围进行排序,然后仅使用该适当排序的范围对正在播放的所有子列表进行重新排序。
如果您实际上有其他问题,当然会有不同的解决方案;-)。
C#实现对二维数组排序的方法
本文实例讲述了C#实现对二维数组排序的方法。分享给大家供大家参考。具体实现方法如下:
/// <summary> /// A generic routine to sort a two dimensional array of a specified type based on the specified column. /// </summary> /// <param name="array">The array to sort.</param> /// <param name="sortCol">The index of the column to sort.</param> /// <param name="order">Specify "DESC" or "DESCENDING" for a descending sort otherwise /// leave blank or specify "ASC" or "ASCENDING".</param> /// <remarks>The original array is sorted in place.</remarks> /// <see cref="http://stackoverflow.com/questions/232395/how-do-i-sort-a-two-dimensional-array-in-c"/> private static void Sort<T>(T[,] array,int sortCol,string order) { int colCount = array.GetLength(1),rowCount = array.GetLength(0); if (sortCol >= colCount || sortCol < 0) throw new System.ArgumentOutOfRangeException("sortCol","The column to sort on must be contained within the array bounds."); DataTable dt = new DataTable(); // Name the columns with the second dimension index values,e.g.,"0","1",etc. for (int col = 0; col < colCount; coL++) { DataColumn dc = new DataColumn(col.ToString(),typeof(T)); dt.Columns.Add(dc); } // Load data into the data table: for (int rowindex = 0; rowindex < rowCount; rowindex++) { DaTarow rowData = dt.NewRow(); for (int col = 0; col < colCount; coL++) rowData[col] = array[rowindex,col]; dt.Rows.Add(rowData); } // Sort by using the column index = name + an optional order: DaTarow[] rows = dt.Select("",sortCol.ToString() + " " + order); for (int row = 0; row <= rows.GetUpperBound(0); row++) { DaTarow dr = rows[row]; for (int col = 0; col < colCount; coL++) { array[row,col] = (T)dr[col]; } } dt.dispose(); }
希望本文所述对大家的C#程序设计有所帮助。
java – 如何按字典顺序对二维数组进行排序?
int[][] source = { { 3,5,6,1},{ 3,3,-6},{ -1,-3,-5,{ 124,43,55,-66} };
我们如何按字典顺序对多维数组源进行排序?
所以,结果,我希望它是:
[ [ -1,-6],[ 3,1],[124,-66] ]
这个网站上的很多问题似乎只建议按每个数组的第一个元素或第二个,第三个等进行排序,但不考虑整个数组.
解决方法
Arrays.compare
的新方法,它允许您按字典顺序比较两个给定的数组.
文档中对Arrays.compare
的简短描述:
If the two arrays share a common prefix then the lexicographic
comparison is the result of comparing two elements,as if by
Integer.compare(int,int),at an index within the respective arrays
that is the prefix length. Otherwise,one array is a proper prefix of
the other and,lexicographic comparison is the result of comparing the
two array lengths.
鉴于您想要修改源数组,那么使用Arrays.sort应该足够了:
Arrays.sort(source,Arrays::compare);
鉴于你想要一个新的数组作为结果,那么我将采用流方式:
int[][] sorted = Arrays.stream(source) .sorted(Arrays::compare) .toArray(int[][]::new);
Java根据一列对二维数组排序
如何解决Java根据一列对二维数组排序?
根据一列 对二维数组进行排序第一列是日期格式为“ yyyy.MM.dd HH:mm”的日期,第二列是字符串。
因为您说的是二维数组,所以我假设“格式日期…”表示字符串。这是用于对String [] []的二维数组进行排序的代码:
import java.util.Arrays;
import java.util.Comparator;
public class Asdf {
public static void main(final String[] args) {
final String[][] data = new String[][] {
new String[] { "2019.07.25 20:24", "Message A" },
new String[] { "2019.07.25 20:17", "Message G" },
new String[] { "2019.07.25 20:25", "Message B" },
new String[] { "2019.07.25 20:30", "Message D" },
new String[] { "2019.07.25 20:01", "Message F" },
new String[] { "2019.07.25 21:08", "Message E" },
new String[] { "2019.07.25 19:54", "Message R" } };
Arrays.sort(data, new Comparator<String[]>() {
@Override
public int compare(final String[] entry1, final String[] entry2) {
final String time1 = entry1[0];
final String time2 = entry2[0];
return time1.compareto(time2);
}
});
for (final String[] s : data) {
System.out.println(s[0] + " " + s[1]);
}
}
}
输出:
2019.07.25 19:54 Message R
2019.07.25 20:01 Message F
2019.07.25 20:17 Message G
2019.07.25 20:24 Message A
2019.07.25 20:25 Message B
2019.07.25 20:30 Message D
2019.07.25 21:08 Message E
解决方法
在Java中,我的数组中有一个数据,如下所示
2019.07.25 20:24 Message A
2019.07.25 20:17 Message G
2019.07.25 20:25 Message B
2019.07.25 20:30 Message D
2019.07.25 20:01 Message F
2019.07.25 21:08 Message E
2019.07.25 19:54 Message R
我想根据第一列对其进行排序,所以我的最终数据可以像这样
2019.07.25 19:54 Message R
2019.07.25 20:01 Message F
2019.07.25 20:17 Message G
2019.07.25 20:24 Message A
2019.07.25 20:25 Message B
2019.07.25 20:30 Message D
2019.07.25 21:08 Message E
第一列是格式为“ yyyy.MM.dd HH:mm”的日期,第二列是字符串。
PHP 对二维数组排序
现在有一个二维数组
$arr = array( array(''date''=>''2011-04-21'',''wu''=>''test''), array(''date''=>''2014-04-21'',''wu''=>''good''), array(''date''=>''2011-12-22'',''wu''=>''great''), array(''date''=>''2012-05-21'',''wu''=>''hahah''), array(''date''=>''2013-08-13'',''wu''=>''test''), array(''date''=>''2011-04-25'',''wu''=>''yes''), array(''date''=>''2014-04-14'',''wu''=>''no''), );
我想将它按照里面date日期来重新排序,得到这样的新数组:
立即学习“PHP免费学习笔记(深入)”;
$arr2 = array( array(''date''=>''2011-04-21'',''wu''=>''test''), array(''date''=>''2011-04-25'',''wu''=>''yes''), array(''date''=>''2011-12-22'',''wu''=>''great''), array(''date''=>''2012-05-21'',''wu''=>''hahah''), array(''date''=>''2013-08-13'',''wu''=>''test''), array(''date''=>''2014-04-14'',''wu''=>''no''), array(''date''=>''2014-04-21'',''wu''=>''good''), );
请问应该怎样做呢?
回复内容:
现在有一个二维数组
$arr = array( array(''date''=>''2011-04-21'',''wu''=>''test''), array(''date''=>''2014-04-21'',''wu''=>''good''), array(''date''=>''2011-12-22'',''wu''=>''great''), array(''date''=>''2012-05-21'',''wu''=>''hahah''), array(''date''=>''2013-08-13'',''wu''=>''test''), array(''date''=>''2011-04-25'',''wu''=>''yes''), array(''date''=>''2014-04-14'',''wu''=>''no''), );
我想将它按照里面date日期来重新排序,得到这样的新数组:
立即学习“PHP免费学习笔记(深入)”;
$arr2 = array( array(''date''=>''2011-04-21'',''wu''=>''test''), array(''date''=>''2011-04-25'',''wu''=>''yes''), array(''date''=>''2011-12-22'',''wu''=>''great''), array(''date''=>''2012-05-21'',''wu''=>''hahah''), array(''date''=>''2013-08-13'',''wu''=>''test''), array(''date''=>''2014-04-14'',''wu''=>''no''), array(''date''=>''2014-04-21'',''wu''=>''good''), );
请问应该怎样做呢?
直接上代码
$tmp = array(); foreach ($arr as $a) { $tmp[$a[''date''] . ''_'' . $a[''wu''] . ''_'' . rand(1000000, 9999999)] = $a; } asort($tmp); $arr = array_values($tmp);
使用 array_multisort
参考:http://cn2.php.net/array_multisort
php$sort = array( ''direction'' => ''SORT_ASC'', //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序 ''field'' => ''date'', //排序字段 ); $arrSortTmp = array(); foreach($arr AS $uniqid => $row){ foreach($row AS $key=>$value){ $arrSortTmp[$key][$uniqid] = $value; } } array_multisort($arrSortTmp[$sort["field"]],constant($sort["direction"]), $arr); var_dump($arr);
$arr = array(
array(''date''=>''2011-04-21'',''wu''=>''test''),
array(''date''=>''2014-04-21'',''wu''=>''good''),
array(''date''=>''2011-12-22'',''wu''=>''great''),
array(''date''=>''2012-05-21'',''wu''=>''hahah''),
array(''date''=>''2013-08-13'',''wu''=>''test''),
array(''date''=>''2011-04-25'',''wu''=>''yes''),
array(''date''=>''2014-04-14'',''wu''=>''no''),
);
array_multisort($arr, SORT_ASC);
print_r($arr);
今天关于如何在python中按行对二维数组排序?和如何在python中按行对二维数组排序的介绍到此结束,谢谢您的阅读,有关C#实现对二维数组排序的方法、java – 如何按字典顺序对二维数组进行排序?、Java根据一列对二维数组排序、PHP 对二维数组排序等更多相关知识的信息可以在本站进行查询。
本文标签: