slice

Pandas Dataframe select multiple discontinuous columns/slices

人盡茶涼 提交于 2019-12-10 16:19:45
问题 I have dataframe with >100 columns. I am trying to select the columns 0~32 and #83. It seems that 1 slice works fine with the code below. df_new = df[df.columns[0:32]] It does not work with 2 slices code below though. How do I fix the problem? df_new = df[df.columns[0:32, 83]] 回答1: Use the np.r_ indexer in conjunction with iloc , like this: df.iloc[:, np.r_[0:32, 83]] np.r_[0:32, 83] array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,

Slicing a scipy sparse matrix using a boolean mask

房东的猫 提交于 2019-12-10 16:12:34
问题 I have encountered a difference in how slicing a scipy sparse matrix works in 0.10.0 and 0.10.1. Consider the following piece of code: from numpy import array, ravel from scipy.sparse import csr_matrix mat = csr_matrix(array([[1, 0, 0], [0,1,0], [1,0,0]])) desired_cols = ravel(mat.sum(0)) > 0 print mat[:, desired_cols].A In scipy 0.10.0, I get what I expect to get: [[1 0] [0 1] [1 0]] In 0.10.1 and 0.12.0, I get [[0 0 1] [1 1 0] [0 0 1]] I am not sure if this is a bug or I am doing something

Comparing slices in python

与世无争的帅哥 提交于 2019-12-10 15:53:45
问题 Inspecting the slice class in Python with dir() , I see that it has attributes __le__ and __lt__ . Indeed I saw that the following code works: slice(1, 2) < slice(3, 4) # True However, I cannot see which logic is implemented for this comparison, nor its usecase. Can anyone point me to that? I am not asking about tuple comparison. Even if slice and tuple are compared the same way, I don't think this makes my question a duplicate. What's more, I also asked for a possible usecase of slice

Speed up numpy nested loop

試著忘記壹切 提交于 2019-12-10 15:45:51
问题 I am writing a simulation for a wireless network in python using numpy and cython, where suppose there is a number of nodes no_nodes scattered randomly on the 2d plane which send out some waveforms and their respective receivers, again scattered randomly on the 2d plane. Each transmitting node produces a waveform I call output (each one can produce an output of different length). What I want to do is to sum these outputs from each node to one big waveform that will be the input to each

Do Clearing slices in golang guarantees garbage collection?

谁都会走 提交于 2019-12-10 15:40:01
问题 I wanted to implement time based slots for holding data using golang slices. I managed to come up with a go program like this and it also works. But I have few questions regarding garbage collection and the general performance of this program. Does this program guarantee garbage collection of items once slice is equated to nil? And while shuffling slices, I hope this program does not do any deep copying. type DataSlots struct { slotDuration int //in milliseconds slots [][]interface{}

Golang - Using chan slice inside struct

喜夏-厌秋 提交于 2019-12-10 14:57:28
问题 I am trying to use a slice chan type inside a struct, similar to the code below. However, when I try to receive at test := <-c.slice The program hangs. Is there a way to do this? package main import "fmt" type blah struct { slice chan [][]int } func main() { slice := make([][]int, 3) c := blah{make(chan [][]int)} slice[0] = []int{1, 2, 3} slice[1] = []int{4, 5, 6} slice[2] = []int{7, 8, 9} go func() { test := <- c.slice test = slice c.slice <- test }() fmt.Println(<-c.slice) } 回答1: The first

Golang string format using slice values

烂漫一生 提交于 2019-12-10 14:19:21
问题 Here I am trying to create a query string for my API from a slice containing strings. ie. where={"node_name":"node1","node_name":"node_2"} import ( "fmt" "strings" ) func main() { nodes := []string{"node1", "node2"} var query string for _, n := range nodes { query += fmt.Sprintf("\"node_name\":\"%s\",", n) } query = strings.TrimRight(query, ",") final := fmt.Sprintf("where={%s}", query) fmt.Println(final) } Here is goplayground link. What is the best way to get the result? 回答1: Your solution

Python Numpy Structured Array (recarray) assigning values into slices

血红的双手。 提交于 2019-12-10 13:07:17
问题 The following example shows what I want to do: >>> test rec.array([(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)], dtype=[('ifAction', '|i1'), ('ifDocu', '|i1'), ('ifComedy', '|i1')]) >>> test[['ifAction', 'ifDocu']][0] (0, 0) >>> test[['ifAction', 'ifDocu']][0] = (1,1) >>> test[['ifAction', 'ifDocu']][0] (0, 0) So, I want to assign the values (1,1) to test[['ifAction', 'ifDocu']][0] . (Eventually, I want to do something like test

Python list slicing efficiency

柔情痞子 提交于 2019-12-10 12:43:37
问题 In the following code: def listSum(alist): """Get sum of numbers in a list recursively.""" sum = 0 if len(alist) == 1: return alist[0] else: return alist[0] + listSum(alist[1:]) return sum is a new list created every time when I do listSum(alist[1:]) ? If yes, is this the recommended way or can I do something more efficient? (Not for the specific function -this serves as an example-, but rather when I want to process a specific part of a list in general.) Edit: Sorry if I confused anyone, I

JavaScript call() and Prototype - Slice Function

此生再无相见时 提交于 2019-12-10 07:22:37
问题 I'm reading the MDN Article on slice in JavaScript. I understand everything except the 2nd example in the section titled Array-Like Objects . It says we can simplify the first example by making slice our own function as so: var unboundSlice = Array.prototype.slice; var slice = Function.prototype.call.bind(unboundSlice); function list() { return slice(arguments); } var list1 = list(1, 2, 3); // [1, 2, 3] What I don't understand is how call can come right after prototype on the second line. I