array

numpy中ndarray中选出若干个元素并输出(ndarray.take())

情到浓时终转凉″ 提交于 2020-01-19 00:34:52
官方文档take用法解释 先随机生成一个矩阵a import numpy as np a = np . random . randint ( 1 , 9 , size = 9 ) . reshape ( ( 3 , 3 ) ) 结果 array ( [ [ 7 , 3 , 2 ] , [ 8 , 7 , 5 ] , [ 3 , 7 , 3 ] ] ) 如果希望提取某个axis的若干切片 ndarray.take(indices,axis) 如,提取a的第一行和第三行 a . take ( [ 0 , 2 ] , axis = 0 ) 输出 array ( [ [ 7 , 3 , 2 ] , [ 3 , 7 , 3 ] ] ) 如果希望提取第一列和第三列,类似地 a . take ( [ 0 , 2 ] , axis = 1 ) 结果 array ( [ [ 7 , 2 ] , [ 8 , 5 ] , [ 3 , 3 ] ] ) 如果没有指定axis a . take ( [ 1 , 2 , 8 ] ) 它会先把a扁平化为一维矩阵 结果 array ( [ 3 , 2 , 3 ] ) 当然indice可以是任意顺序,如我们希望第三行重复两次,再和第一行组合成矩阵 如 a . take ( [ 2 , 2 , 0 ] , axis = 0 ) 结果 array ( [ [ 3 , 7

数据结构之排序基础知识笔记

北战南征 提交于 2020-01-18 22:14:17
概述 最近在温习数据结构,现在针对排序算法写一篇随笔,其中附带原理说明和代码。 插入排序 直接插入排序(Straight Insertion Sort)的基本思想是:把n个待排序的元素看成为一个有序表和一个无序表。开始时有序表中只包含1个元素,无序表中包含有n-1个元素,排序过程中每次 从无序表中取出第一个元素,将它插入到有序表中的适当位置,使之成为新的有序表,重复n-1次可完成排序过程。 假设{20,30,40,10,60,50}中的前3个数已经排列过,是有序的了;接下来对10进行排列。示意图如下: 图中将数列分为有序区和无序区。我们需要做的工作只有两个:(1)取出无序区中的第1个数,并找出它在有序区对应的位置。(2)将无序区的数据插入到有序区;若有必要的话,则对有序区中的相关数据进行移位。 时间复杂度和稳定性 直接插入排序的时间复杂度是O(N2)。 假设被排序的数列中有N个数。遍历一趟的时间复杂度是O(N),需要遍历多少次呢?N-1!因此,直接插入排序的时间复杂度是O(N*N)。 直接插入排序是稳定的算法,它满足稳定算法的定义。 算法稳定性 -- 假设在数列中存在a[i]=a[j],若在排序之前,a[i]在a[j]前面;并且排序之后,a[i]仍然在a[j]前面。则这个排序算法是稳定的。 插入排序JAVA实现 1 import static javaTest.SortUtils

13-调整数组顺序使奇数位于偶数前面

混江龙づ霸主 提交于 2020-01-18 21:57:03
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。 function reOrderArray ( array ) { // oddBegin主要是用作奇数的索引,oddCount是用作偶数的索引,newArray用来存储,以空间换时间,复杂度为O(n) let oddBegin = 0 , oddCount = 0 ; const newArray = [ ] ; for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] & 1 ) { oddCount ++ ; } } for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] & 1 ) { newArray [ oddBegin ++ ] = array [ i ] ; } else { newArray [ oddCount ++ ] = array [ i ] ; } } return newArray ; } function reOrderArray ( array ) { // write code here for ( var i = 0 ; i <

Pointers and Dynamic Allocation of Memory

蓝咒 提交于 2020-01-18 21:29:52
METHOD 1: Consider the case where we do not know the number of elements in each row at compile time, i.e. both the number of rows and number of columns must be determined at run time. One way of doing this would be to create an array of pointers to type int and then allocate space for each row and point these pointers at each row. Consider: 1 #include <stdio.h> 2 3 int main() 4 { 5 int nrows=5;   6 int ncols=10; 7 int row; 8 int **rowptr; 9 rowptr=malloc(nrows*sizeof(int *)); //分配5行(int *)型一维数组大小的空间 10 if (NULL==rowptr) 11 { 12 puts("\nFailure to allocate room for row pointers.\n"); 13 exit(0)

ES6:数组方法(一) Array.form() Array.of()

六月ゝ 毕业季﹏ 提交于 2020-01-18 09:05:18
下一篇: ES6:数组方法(二) find() findIndex() some() every() Array.form() 将可迭代对象转化为数组 以数组和字符串为例,他们的原型( __proto__ )有所不同:数组是Array,字符串是Object 其所能使用的方法(仅截取部分)也各不同: 而通过 Array.form() 方法,则可以将 可迭代对象 转化为数组,可以使用数组的丰富强大的各种方法了: //以字符串为例 const arr = [ 1 , 2 , 3 , 4 , 5 ] ; const str = '123456' ; console . log ( str ) ; const strToArr = Array . from ( str ) ; console . log ( str ) ; console . log ( strToArr ) ; 可以看出使用 Array.form() 方法后不会改变原变量 Array.of() 弥补new Array()构造函数的不足 当使用 new Array() 创建一个新数组时,只有一个参数时他会创建一个有这个参数数量的空元素数组,多个参数时会创建这些参数构成的数组 使用 Array.of() 方法会统一规则:无论几个参数都会创建由参数构成的数组 来源: CSDN 作者: 苦呀君 链接: https://blog

1296. Divide Array in Sets of K Consecutive Numbers

百般思念 提交于 2020-01-18 06:56:28
Given an array of integers nums and a positive integer k , find whether it's possible to divide this array into sets of k consecutive numbers Return True if its possible otherwise return False . Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output: true Explanation: Array can be divided into [1,2,3,4] and [3,4,5,6]. Example 2: Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3 Output: true Explanation: Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11]. Example 3: Input: nums = [3,3,2,2,1,1], k = 3 Output: true Example 4: Input: nums = [1,2,3,4], k = 3 Output: false

Numpy 的常用属性 和创建数组

白昼怎懂夜的黑 提交于 2020-01-18 03:28:32
1、集中常见的 numpy 的属性   ndim:维度   shape:行数和列数   size:元素的个数 >>> import numpy as np # 导入 numpy 模块。np是为了使用方便的简写 >>> array = np.array([[1,2,3],[2,3,4]]) # 列表转换为矩阵 >>> print(array) [[1 2 3] [2 3 4] ] >>>print(‘number of ndim:’, array.ndim) # 维度 number of mdim: 2 >>> print('shape:',array.shape) # 行数和列数 shape:(2, 3) >>>print(‘size:’,array.aize) # 元素个数 size : 6 2、Numpy 创建 array 2.1、关键字   array:创建数组   dtype:制定数据类型   zeros:创建数据全为 0   ones:创建数据全为 1   empty:创建数据接近 0   arrange:按指定范围创建数据   linspace:创建线段 # 创建数组>>> a = np.array([2, 23, 4])>>> print(a) [2, 23, 4]# 指定类型>>> a = np.array([2, 23, 4], dtype = np.int)>>

Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性

前提是你 提交于 2020-01-18 02:42:32
一、Numpy数组创建 part 1:np.linspace(起始值,终止值,元素总个数 import numpy as np ''' numpy中的ndarray数组 ''' ary = np.array([1, 2, 3, 4, 5]) print(ary) ary = ary * 10 print(ary) ''' ndarray对象的创建 ''' # 创建二维数组 # np.array([[],[],...]) a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(a) # np.arange(起始值, 结束值, 步长(默认1)) b = np.arange(1, 10, 1) print(b) print("-------------np.zeros(数组元素个数, dtype='数组元素类型')-----") # 创建一维数组: c = np.zeros(10) print(c, '; c.dtype:', c.dtype) # 创建二维数组: print(np.zeros ((3,4))) print("----------np.ones(数组元素个数, dtype='数组元素类型')--------") # 创建一维数组: d = np.ones(10, dtype='int64') print(d, '; d.dtype

SOME/IP-SD- Entries Array

守給你的承諾、 提交于 2020-01-17 06:45:10
Entries Array When SOME/IP-SD find or offers Service Instances or handles subscriptions this is done by so called entries, which are transported in the entry array of the SOME/IP-SD message. 当SOME / IP-SD寻找或提供服务实例或处理订阅时,这由所谓的entry完成,entry在SOME / IP-SD 报文的entry数组中传输。 Length of Entries Array The length of the first field of the Entries Array shall be 32 bits. Entries Array的第一个字段的长度应为32位。 The first field of the Entries Array shall carry the amount of bytes of the Entries Array (excluding this 32 bit field carrying the length information). Entries Array的第一个字段应携带Entries Array的字节数量(不包括携带长度信息的32位字段)。

选择排序(Java)

若如初见. 提交于 2020-01-16 13:24:07
排序步骤(从小到大): 01.首先假设未排序的数组中,第一个数为最小数 02.从剩余未排序元素中继续寻找最小的数 03.将剩余未排序中最小的数和假设最小数进行比较,将较小的数排到前面 图示(图源菜鸟教程): Java实现: package com.code; class ArraySort{ public void SelectionSort(int array[]){ for (int i = 0 ; i < array.length - 1 ; i ++) //控制一共需要比较次数 { int min = i; for (int j = i + 1 ; j < array.length ; j ++) //控制每一次未排序数需要比较的次数 { if(array[j] < array[min]) { min = j; } } if(array[i] > array[min]) //将假设最小数与未排序中最小的数进行位置交换 { int tmp = 0; tmp = array[i]; array[i] = array[min]; array[min] = tmp; } } System.out.print("排序后的数组为:"); for (int i = 0 ; i < array.length ; i ++) //输出排序后的数组 { System.out.print