slice

Spliting a list into sizes specified by another list

让人想犯罪 __ 提交于 2019-12-11 04:45:30
问题 Let say there's a list X and another list num_items that specify the number of items that should be in the sublist, I can split the list manually as such: >>> x = list(range(10)) >>> x [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> num_items = [4, 4, 2] >>> slice1 = x[:num_items[0]] >>> slice2 = x[len(slice1):len(slice1)+num_items[1]] >>> slice3 = x[len(slice1)+len(slice2):] >>> slice1, slice2, slice3 ([0, 1, 2, 3], [4, 5, 6, 7], [8, 9]) There will be two cases where the last few slices can become

Fortran performance when passing array slices as arguments

点点圈 提交于 2019-12-11 04:35:06
问题 I like fortran's array-slicing notation ( array(1:n) ), but I wonder whether I take a performance hit if I use them when it's not necessary. Consider, for example, this simple quicksort code (It works, but obviously it's not taking care to pick a good pivot): recursive subroutine quicksort(array, size) real, dimension(:), intent(inout) :: array integer, intent(in) :: size integer :: p if (size > 1) then p = partition(array, size, 1) call quicksort(array(1:p-1), p-1) call quicksort(array(p+1

How to return the maximum element of a list in Python?

旧巷老猫 提交于 2019-12-11 03:10:20
问题 I am trying to simplify this function at his maximum, how can I do? def eleMax(items, start=0, end=None): if end is None: end = len(items) return max(items[start:end]) I though of def eleMax(items, start=0, end=-1): return max(items[start:end]) But the last element is deleted from the list. Thank for your help. 回答1: You can just remove these two lines: if end is None: end = len(items) The function will work exactly the same: >>> a=[5,4,3,2,1] >>> def eleMax(items, start=0, end=None): ...

Subset Pandas DataFrame based on annual returning period covering multiple months

不问归期 提交于 2019-12-11 03:07:19
问题 This question is similar to Selecting Pandas DataFrame records for many years based on month & day range, but both the question and answer doesn't seem to cover my case import pandas as pd import numpy as np rng = pd.date_range('2010-1-1', periods=1000, freq='D') df = pd.DataFrame(np.random.randn(len(rng)), index=rng, columns=['A']) df.head() A 2010-01-01 1.098302 2010-01-02 -1.384821 2010-01-03 -0.426329 2010-01-04 -0.587967 2010-01-05 -0.853374 Now I would like to subset my DataFrame based

Slicing array by using another array as the slice indices along axis

≡放荡痞女 提交于 2019-12-11 02:47:55
问题 Say I have an array that looks like the following: arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] And I have another array slicer = [1,3,2] . I want to apply these values as the slice index over axis 0 measure along axis 1. This doesn't work (and in fact contains no way of specifying that the along part is axis 1 in an ndarray) but suppose I tried arr[:slicer, :] I would hope to obtain, out = [[1, 2, 3], [nan, 5, 6], [nan, 8, nan]] which is the combination of applying the slice arr[:1, :] , arr[:3,

Limit results autocomplete jquery ui with slice function

放肆的年华 提交于 2019-12-11 02:29:31
问题 I need to limit results (max 10) in this autocomplete jquery ui script. I know I have to use the slice function, but I'm not able to place it correctly inside the script. Thanks in advance for your help. $(document).ready(function() { var myArr = []; $.ajax({ type: "GET", url: "events.xml", // change to full path of file on server dataType: "xml", success: parseXml, complete: setupAC, failure: function(data) { alert("XML File could not be found"); } }); function parseXml(xml) { //find every

perl discard first array element in map operation

流过昼夜 提交于 2019-12-11 02:24:00
问题 I'm starting to harness the power of perl map, and have run into a question that I couldn't find an answer to. Basically I am parsing the return of a unix command which has a header line that I don't need, and then 2 lines of information per item. Currently, I am doing this: (undef, @ret) = map { [split /\n/] } split(/(?:Host: )/, `cat /proc/scsi/scsi`); Which works fine to skip the header and give me one array element per "useful" line of text. However, I want to build a hash instead, which

Split multidimensional array and then slice it [closed]

我与影子孤独终老i 提交于 2019-12-11 01:45:21
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I have a column of paths: C:\Series1\Season1\Ep1 C:\Series1\Season2\Ep1 C:\Series2\Season1\Ep1 C:\Series2\Season2\Ep1 C:\Series3\Season1\Ep1 I now want to split the array into a multidimensional one, so it looks like this in the end: +---+----+---------+---------+-----+ | | 1 | 2 | 3 | 4 | +---+----+---------+--

Parsing explicit arrays

拈花ヽ惹草 提交于 2019-12-11 01:43:14
问题 I'm trying to parse this type of response from a server: [[1,"a","b",2,"000000",[[1,2,3],[1,2,3]],"x","y","z",[[1,2,3],[1,2,3]]]] Besides writing my own hack'ish parser for this type of messages is there a standard way to interpret this that I'm not aware of? 回答1: Your input is a JSON string. In Go, you can use the encoding/json package to decode it. Usually, when the structure of the JSON string is known prior, a Go struct type can be constructed which models it, and then you can unmarshal

Swap indexes using slices?

百般思念 提交于 2019-12-11 01:17:19
问题 I know that you can swap 2 single indexes in Python r = ['1', '2', '3', '4', '5', '6', '7', '8'] r[2], r[4] = r[4], r[2] output: ['1', '2', '5', '4', '3', '6', '7', '8'] But why can't you swap 2 slices of indexes in python? r = ['1', '2', '3', '4', '5', '6', '7', '8'] I want to swap the numbers 3 + 4 with 5 + 6 + 7 in r: r[2:4], r[4:7] = r[4:7], r[2:4] output: ['1', '2', '5', '6', '3', '4', '7', '8'] expected output: ['1', '2', '5', '6', '7', '3', '4', '8'] What did I wrong? output: 回答1: The