对于想了解Numpy二维数组-从指定索引的读者,本文将提供新的信息,我们将详细介绍二维数组索引python,并且为您提供关于c语言练习二维数组年平均降水量月平均降水量、java-基础-二维数组、jav
对于想了解Numpy 二维数组 - 从指定索引的读者,本文将提供新的信息,我们将详细介绍二维数组索引python,并且为您提供关于c语言练习 二维数组 年平均降水量 月平均降水量、java - 基础 - 二维数组、java 二维数组、Java 二维数组(矩阵)孤岛搜索的有价值信息。
本文目录一览:Numpy 二维数组 - 从指定索引(二维数组索引python)
如何解决Numpy 二维数组 - 从指定索引
假设我有一个二维 numpy 数组:
mat=[[5,5,3,6,3],[3,2,7,8,1],[7,0]]
和索引向量:
vec=[3,1,2]
我需要的是从相应的索引中取出 3 个元素。
例如,向量中的第一个元素,对应于矩阵中的第一行是 3。
因此,我需要从第一行的索引 3(基于 0)中取出 3 个元素,即 6。
所以我需要的是 [6,None]
。
最终输出应该是:
[[6,None],[2,8],[5,0]]
我尝试使用 take
和花哨的索引,但无法获得所需的输出。
任何帮助将不胜感激!
解决方法
你可以这样做 -
import numpy as np
mat=np.array([[5,5,3,6,3],[3,2,7,8,1],[7,0]])
mat = np.hstack((mat,np.ones((3,3))*np.nan))
vec=np.array([3,1,2])
idx = vec[:,None] + np.arange(0,3)
print(mat[np.arange(3)[:,None],idx])
给予 -
[[ 6. 3. nan]
[ 2. 7. 8.]
[ 5. 2. 0.]]
首先将原始数组附加三列 inf
或 None
或其他内容。然后通过添加从 0 开始的连续整数并简单地索引原始矩阵,从 vec
创建一个二维索引数组。
c语言练习 二维数组 年平均降水量 月平均降水量
#define YEARS 5
#define MONTHES 12
int main(void)
{
const float rain[YEARS][MONTHES] =
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
float rain_year=0;
int i;
int j;
float rain_month = 0;
for (i = 0; i < YEARS; i++)
{
for (j = 0; j < MONTHES; j++)
{
rain_year = rain_year + rain[i][j];
}
printf("sum rain of year %d is %.1f,average rain is %.1f\n", i + 1,rain_year,rain_year/MONTHES);
rain_year = 0;
}
for (j = 0; j < MONTHES; j++)
{
for (i = 0; i < YEARS; i++)
{
rain_month = rain_month + rain[i][j];
}
printf("%d month rain is %.1f\n", j + 1, rain_month/YEARS);
}
return 1;
感想:二维数组总是感觉没有那么一维数组直观,写起来比较费脑子,要多写啊。
java - 基础 - 二维数组
声明:
int[][] array;
初始化
静态: int[][] array = { {1,2,3}, {2,3,4}, {10,20,30}};
动态: int[][] array = new int[2][3];
遍历:
import java.util.*;
class test{
public static void main(String[] args){
int[][] a = new int[][]{{1,2},{3,4},{5}};
int[][] b = {{10,20},{30},{40,50}};
String[][] c;//定义和初始化分开时必须有new
c = new String[][]{{"a","b"},{"c","d"},{"e"}};
//遍历和一维数组类似
System.out.println("-----a-----");
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a[i].length; j++){
System.out.println(a[i][j]);
}
}
System.out.println("-----b-----");
for(int[] e1:b){
for(int e2:e1){
System.out.println(e2);
}
}
System.out.println("-----c-----");
for(String[] e1:c){
for(String e2:e1){
System.out.println(e2);
}
}
}
}
int[][] a = new int[3][]; //可以编译运行,但是如果使用如a[0][0]会报空指针异常,此时a[0][0]是空
a[0] = new int[2];
a[1] = new int[2];
a[2] = new int[1];
java 二维数组
多维数组可以看成以数组为元素的数组
二维数组的定义
第一种方式: int[][] arr = new int[3][4];
第二种方式:int[][] arr = new int[3][];
第三种方式: int[][] arr = {{1,2},{3,4,5,6},{7,8,9}
class Demo03
{
public static void main(String[] args)
{
//1.定义二维数组
int[][] arr=new int[2][3];
arr[1][0]=5;
arr[1][1]=6;
System.out.println(arr[1][0]);//5
System.out.println(arr[1][1]);//6
System.out.println(arr[0][0]);//0
System.out.println(arr[1]);//0 地址
System.out.println(arr);//地址
}
}
class Demo04
{
public static void main(String[] args)
{
//2.定义数组
int[][] arr=new int[3][];
arr[0]=new int[3];
arr[1]=new int[2];
arr[2]=new int[1];
arr[0][0]=1;
arr[1][0]=2;
//遍历
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
class Demo05
{
public static void main(String[] args)
{
//3.数组定义
String[][] arr={
{"hello","小红帽"},
{"b","c","a"},
{"章鱼哥","小丸子"}
};
//遍历
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.println(arr[i][j]);
}
}
}
}
内存图
二维数组元素遍历求和
获取数组长度:
a.length获取的二维数组第一维数组的长度,a[0].length才是获取第二维第一个数组长度。
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
sum+=arr[i][j];
}
}
数组异常
内存图
数组越界异常
每个数组的索引都有一个范围,即0~length-1。在访问数组的元素时,索引不能超出这个范围,否则程序会报错
空指针异常
在使用变量引用一个数组时,变量必须指向一个有效的数组对象,如果该变量的值为null,则意味着没有指向任何数组,此时通过该变量访问数组的元素会出现空指针异常,接下来通过一个案例来演示这种异常,如下所示
class Demo02
{
public static void main(String[] args)
{
int[] arr={4,5,6};
//数组一旦创建,长度不可变(不允许再添加或者删除元素)
//java.lang.ArrayIndexOutOfBoundsException
//数组越界异常:访问了超出数组长度的元素
//修改代码
//System.out.println(arr[2]);
arr=null;
//java.lang.NullPointerException
//你访问了空的对象或者容器
//修改代码(检测null的位置,加判断)
if(arr!=null){
System.out.println(arr[2]);
}
}
}
Java 二维数组(矩阵)孤岛搜索
一、二维数组岛屿
1、问题描述
假设一个二维数组,完全有0和1两个数字组成,假设将所有的0认为是海洋,单独的数字1或者连成一片的数字1认为是岛屿,那么求整个二维数组中有多少岛屿,并且打印每个岛屿中的坐标点。
{0,1,0,1,1,0,1,0},
{1,0,0,0,1,0,0,0},
{0,0,1,0,0,1,0,1},
{0,1,0,1,0,0,1,0},
{0,1,1,1,1,0,1,0},
{0,0,1,0,1,0,1,0},
首先,我们可以将数组看做一个围棋棋盘,0或者空位认为是海洋,以下面为例。
2、关于围棋
围棋,一种策略性两人棋类游戏,中国古时称“弈”,西方名称“Go”。流行于东亚国家(中、日、韩、朝),属琴棋书画四艺之一。围棋起源于中国,传为帝尧所作,春秋战国时期即有记载。隋唐时经朝鲜传入日本,流传到欧美各国。围棋蕴含着中华文化的丰富内涵,它是中国文化与文明的体现。
所以,作为中国人,传统很重要,特别是人工智能火热的时代,不学点围棋知识怎么行呢?
二、算法实现
算法原理:
map区域集合法,按顺序遍历,map key值为区号,value 为坐标集合。
- 发现1,找出坐标去每个区查询,找出能连通该坐标的所有区,合并区域到最小key值集合中,并将当前加入该坐标集合中。
- 未发现1,给1创建一个新区,将1的坐标存入该区集合中
1、核心方法
public static class Node {
int x;
int y;
@Override
public String toString() {
return "{" +
"x=" + x +
", y=" + y +
''}'';
}
}
public static void printIsLands(int[][] inputMatrix){
if(inputMatrix==null) return;
int ROW = inputMatrix.length;
int COL = inputMatrix[0].length;
final Map<String, Set<Node>> zoneMapList = new HashMap<>();
for (int i=0;i<ROW;i++){
for (int j=0;j<COL;j++){
int val = inputMatrix[i][j];
if(val==0){
continue;
}
Set<String> mapKeys = findLinkableZoneMapList(zoneMapList,i,j);
if(!mapKeys.isEmpty()){
combineMaps(zoneMapList,mapKeys,i,j);
}else{
Node node = new Node();
node.x = i;
node.y = j;
Set<Node> nodes = new HashSet<>();
nodes.add(node);
zoneMapList.put(makeKey(node),nodes);
}
}
}
for (Map.Entry<String,Set<Node>> entry : zoneMapList.entrySet()){
String name = entry.getKey();
Set<Node> nodes = entry.getValue();
System.out.println(name+"=" + nodes);
}
}
private static String makeKey(Node node) {
return node.x+"_"+node.y;
}
private static void combineMaps(Map<String, Set<Node>> zoneMapList, Set<String> mapKeys, int x, int y) {
Set<Node> setNode = new HashSet<>();
Iterator<String> keysIterator = mapKeys.iterator();
String firstKey = null;
do{
if(!keysIterator.hasNext()) break;
String key = keysIterator.next();
if(key==null) continue;
if(firstKey==null){
firstKey = key;
}
if(zoneMapList.containsKey(key)) {
setNode.addAll(zoneMapList.remove(key));
}
}while (true);
Node n = new Node();
n.x = x;
n.y = y;
setNode.add(n);
zoneMapList.put(firstKey,setNode);
}
private static Set<String> findLinkableZoneMapList(Map<String, Set<Node>> zoneMapList, int x, int y) {
Set<String> mapKeys = new HashSet<>();
for (Map.Entry<String,Set<Node>> entry : zoneMapList.entrySet()){
Set<Node> nodes = entry.getValue();
String key = entry.getKey();
if(nodes==null || nodes.isEmpty()) continue;
Iterator<Node> iterator = nodes.iterator();
do{
if(!iterator.hasNext()){
break;
}
Node node = iterator.next();
if(node==null){
iterator.remove();
continue;
}
if(node.x==(x-1) && node.y==(y)){
mapKeys.add(key);
break;
}
if(node.x==(x+1) && node.y==(y)){
mapKeys.add(key);
break;
}
if(node.x==(x) && node.y==(y-1)){
mapKeys.add(key);
break;
}
if(node.x==(x) && node.y==(y+1)){
mapKeys.add(key);
break;
}
}while (true);
}
return mapKeys;
}
三、测试
public class SecretIsland {
public static void main(String[] args) {
int[][] islandMap = {
{0,1,0,1,1,0,1,0},
{1,0,0,0,1,0,0,0},
{0,0,1,0,0,1,0,1},
{0,1,0,1,0,0,1,0},
{0,1,1,1,1,0,1,0},
{0,0,1,0,1,0,1,0},
};
Map<Integer, Point[]> zoneMap = findIsland(islandMap);
printIsland(zoneMap);
}
}
打印结果如下
0_1=[{x=0, y=1}]
1_0=[{x=1, y=0}]
0_3=[{x=0, y=4}, {x=0, y=3}, {x=1, y=4}]
2_2=[{x=2, y=2}]
3_1=[{x=4, y=4}, {x=5, y=4}, {x=4, y=3}, {x=5, y=2}, {x=4, y=2}, {x=3, y=1}, {x=4, y=1}, {x=3, y=3}]
0_6=[{x=0, y=6}]
2_5=[{x=2, y=5}]
2_7=[{x=2, y=7}]
3_6=[{x=3, y=6}, {x=5, y=6}, {x=4, y=6}]
可见,该矩阵有9个孤岛
四、递归算法
public static int getIslandMatrix(int[][] matrix) {
if (matrix == null) {
return 0;
}
int ROW = matrix.length;
if (ROW == 0) {
return 0;
}
int COL = matrix[0].length;
int N = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
int val = matrix[i][j];
if (val != 1) {
continue;
}
N++;
DFS(matrix, i, j);
}
}
return N;
}
private static void DFS(int[][] matrix, int i, int j) {
if (i < 0 || j < 0) {
return;
}
if (i >= matrix.length) {
return;
}
if (j >= matrix[0].length) {
return;
}
if (matrix[i][j] != 1) {
return;
}
matrix[i][j] = 2;
DFS(matrix, i - 1, j);
DFS(matrix, i + 1, j);
DFS(matrix, i, j + 1);
DFS(matrix, i, j - 1);
}
递归改进法,大量数据处理时,使用递归容易造成StackOverflowError,因此我们可以进行一些改进,我们知道,递归的改进往往需要栈来完成
public static int getIslandMatrix(int[][] matrix) {
if (matrix == null) {
return 0;
}
int ROW = matrix.length;
if (ROW == 0) {
return 0;
}
int COL = matrix[0].length;
int N = 0;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
int val = matrix[i][j];
if (val != 1) {
continue;
}
N++;
DFS(matrix, i, j);
}
}
return N;
}
private static void DFS(int[][] matrix, int i, int j) {
Stack<Point> stacks = new Stack<>();
stacks.push(new Point(i,j));
while (!stacks.empty()){
Point point = stacks.pop();
int x = point.x;
int y = point.y;
if (x < 0 || y < 0) {
continue;
}
if (x >= matrix.length) {
continue;
}
if (y >= matrix[0].length) {
continue;
}
if (matrix[x][y] != 1) {
continue;
}
matrix[x][y] = 2;
stacks.push(new Point(x - 1,y));
stacks.push(new Point(x + 1,y));
stacks.push(new Point(x ,y+1));
stacks.push(new Point(x ,y-1));
}
}
关于Numpy 二维数组 - 从指定索引和二维数组索引python的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c语言练习 二维数组 年平均降水量 月平均降水量、java - 基础 - 二维数组、java 二维数组、Java 二维数组(矩阵)孤岛搜索等相关内容,可以在本站寻找。
本文标签: