slice

How to avoid re-implementing sort.Interface for similar golang structs

孤街醉人 提交于 2020-01-12 03:29:15
问题 There is one problem bothering me in Golang. Say I have 2 structs: type Dog struct { Name string Breed string Age int } type Cat struct { Name string FavoriteFood string Age int } And when I try to sort []*Dog and []*Cat by Age , I have to define 2 different sort struct like: type SortCat []*Cat func (c SortCat) Len() int {//..} func (c SortCat) Swap(i, j int) {//..} func (c SortCat) Less(i, j int) bool {//..} type SortDog []*Dog func (c SortDog) Len() int {//..} func (c SortDog) Swap(i, j

go map slice array channel 传参

青春壹個敷衍的年華 提交于 2020-01-11 11:23:03
1 package main 2 3 import ( 4 "fmt" 5 //"net/http" 6 ) 7 8 func modifyMap(s map[string]interface{}) { 9 s["a"]="a" 10 } 11 func modifyArray(a [5]int) { 12 a[2]=100 13 } 14 func modifySlice(b []int) { 15 b[0]=0 16 } 17 func modifyChannels(ch chan int) { 18 ch<-2; 19 } 20 func main() { 21 s := make(map[string]interface{}) 22 s["a"]="b" 23 fmt.Println(s); 24 modifyMap(s); 25 fmt.Println(s); 26 a := [5]int{1,2,3,4,5} 27 fmt.Println(a); 28 modifyArray(a) 29 fmt.Println(a); 30 b := []int{1,2,3,4,5} 31 fmt.Println(b); 32 modifySlice(b); 33 fmt.Println(b); 34 ch :=make(chan int,10) 35 fmt.Println(len

How to nicely handle [:-0] slicing?

a 夏天 提交于 2020-01-11 10:48:07
问题 In implementing an autocorrelation function I have a term like for k in range(start,N): c[k] = np.sum(f[:-k] * f[k:])/(N-k) Now everything works fine if start = 1 but I'd like to handle nicely the start at 0 case without a conditional. Obviously it doesn't work as it is because f[:-0] == f[:0] and returns an empty array, while I'd want the full array in that case. 回答1: Don't use negative indices in this case f[:len(f)-k] For k == 0 it returns the whole array. For any other positive k it's

go - 复合类型 array, slice, map

萝らか妹 提交于 2020-01-11 05:56:09
Go 语言支持复合类型: 数组:array 切片:slice 指针:pointer 字典:map 通道:chan 结构体:struct 接口:interface 1. array 同一类型数据的集合 var arr [n]type //声明type类型一维数组 var arr [m][n]type //声明type类型二维数组 多维数组以此类推 也可以用 := 声明 arr := [n]type{元素1[,元素2, ...]} 其中n可以用 "..." 三个点表示,系统会根据元素个数来确定 下标只能为 int 类型,而 php 还支持 string 类型的下标 1.1 数组长度 len(arr) 注:数组长度在定义后就不可变 1.2 遍历: a . 循环通过过数组下标访问 arr[0] ~ arr[(len(arr))] b . range arr, 有两个返回值 第一个为数组下标,第二个为元素的值,与php遍历数组相似 for k, v := range array { fmt.Printf("arr[%d] = %d \t", k, v) } foreach ($arr as $k => $v) { printf("arr[%d] = %d \t", $k, $v); //echo '$arr[' . $k . "] = " . $v . "\t"; } 1.3

Fortran equivalent of numpy.where() function?

老子叫甜甜 提交于 2020-01-10 05:28:09
问题 I would like to do something like this in Fortran: program where real :: a(6) = (/ 4, 5, 6, 7, 8, 9 /) print *, a(a>7) end program In Python I would typically do this with NumPy like this: import numpy a = numpy.array([ 4, 5, 6, 7, 8, 9]) print a[numpy.where(a>7)] #or print a[a>7] I've played around, but nothing has worked thus far, but I'm guessing it is fairly simple. 回答1: I'll extend slightly the answer by @VladimirF as I suspect you don't want to limit yourself to the exact print example.

Pandas DataFrame slicing by day/hour/minute

吃可爱长大的小学妹 提交于 2020-01-10 03:59:45
问题 I have pandas Dataframe with datetime index like 'YYYY-MM-DD HH:MM:SS'. Index Parameter 2007-05-02 14:14:08 134.8 2007-05-02 14:14:32 134.8 2007-05-02 14:14:41 134.8 2007-05-02 14:14:53 134.8 2007-05-02 14:15:01 134.8 2007-05-02 14:15:09 134.8 ...... 2007-05-30 23:08:02 105.9 2007-05-30 23:18:02 105.9 2007-05-30 23:28:02 105.9 2007-05-30 23:38:03 105.8 It is possible to get slice a DataFrame by year df['2007'] or by month df['2007-05'] ? But when I've tried to slice DataFrame by day, for

Subsetting data.table set by date range in R

谁都会走 提交于 2020-01-09 06:25:34
问题 I have a large dataset in data.table that I'd like to subset by a date range. My data set looks like this: testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04", "2013-10-05","2013-11-06")), yr = c(2013,2013,2013,2013,2013), mo = c(07,08,09,10,11), da = c(02,03,04,05,06), plant = LETTERS[1:5], product = as.factor(letters[26:22]), rating = runif(25)) I'd like to be able to choose a date range directly from the as.Date column without using the yr , mo , or da columns.

Python Array Slice With Comma?

点点圈 提交于 2020-01-09 02:13:09
问题 I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0]))) Now, I know that when slicing an array, the first number is start, the next is end, and the last is step, but what does the comma after the end number designate? Thanks. 回答1: It is being used to extract a specific column from a 2D array. Refer to the first examples here. So your

How to extend a Scala list to enable slicing not by explicit position but by given predicate/condition

本小妞迷上赌 提交于 2020-01-08 18:01:10
问题 For trait Item case class TypeA(i: Int) extends Item case class TypeB(i: Int) extends Item consider a Scala list of items such as val myList = List(TypeA(1), TypeB(11), TypeB(12), TypeA(2), TypeB(21), TypeA(3), TypeB(31)) The goal is to define a new slice method that can be applied onto myList and which takes a predicate or condition as argument; for instance myList.slice { x => x.isInstanceOf[TypeA] } would deliver List(List(TypeA(1), TypeB(11), TypeB(12)), List(TypeA(2), TypeB(21)), List

解决引入MapView报错的问题

只愿长相守 提交于 2020-01-08 10:02:35
一.异常内容 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xxx.xxx, PID: 18070 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.activity.MapActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class com.esri.arcgisruntime.mapping.view.MapView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2566) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626) at android.app.ActivityThread.access$1100(ActivityThread.java:170) at android.app.ActivityThread$H