slice

Why can't I assign an arbitrary iterable to an extended slice whose step is -1?

蹲街弑〆低调 提交于 2019-12-01 04:46:50
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> u = [4, 5, 6, 7, 8, 9] >>> u[1::1] = [3, 2, 1, 0] >>> u [4, 3, 2, 1, 0] >>> u[9:0:-1] = [8, 7, 6, 5] >>> u [4, 5, 6, 7, 8] >>> u[9:0:-1] = [16, 12, 8] Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: attempt to assign sequence of size 3 to extended slice of size 4 >>> u [4, 5, 6, 7, 8] >>> Expected behaviour: no exception thrown on final assignment statement; u should print on final line as [4, 8, 12, 16

How do I create two new mutable slices from one slice?

久未见 提交于 2019-12-01 04:34:01
I would like to take a mutable slice and copy the contents into two new mutable slices. Each slice being one half of the original. My attempt #1: let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5]; let list_a: &mut [u8] = my_list[0..3].clone(); let list_b: &mut [u8] = my_list[3..6].clone(); println!("{:?}", my_list); println!("{:?}", list_a); println!("{:?}", list_b); Output: error: no method named `clone` found for type `[u8]` in the current scope --> src/main.rs:3:43 | 3 | let list_a: &mut [u8] = my_list[0..3].clone(); | ^^^^^ error: no method named `clone` found for type `[u8]` in the current

Customize Python Slicing, please advise

醉酒当歌 提交于 2019-12-01 04:04:52
I have a class that subclasses the list object. Now I need to handle slicing. From everything I read on the intertubes this has to be done using the __getitem__ method. At least in Python 2.7+ which is what I'm using. I have done this (see below), but the __getitem__ method isn't called when I pass in a slice. Instead, a slice is done and a list is returned. I would like a new instance of myList returned. Please help me discover what is wrong. Thanks! class myList(list): def __init__(self, items): super(myList, self).__init__(items) self.name = 'myList' def __getitem__(self, index): print("_

Python index out of range in list slicing [duplicate]

二次信任 提交于 2019-12-01 04:01:13
This question already has an answer here: Why does substring slicing with index out of range work? 3 answers While this code will raise indexError: In [1]: lst = [1, 2, 3] In [2]: lst[3] IndexError: list index out of range Slicing the list with "out of range index" will not produce any error. In [3]: lst[3:] Out[3]: [] What is the rationale of this design? When you are accessing an element in a list whose index is beyond its length, we cannot return anything. (There is no way we can represent an element which is not there). That's why the error is thrown. But when you are slicing, you are

How to slice a generator object or iterator in Python

廉价感情. 提交于 2019-12-01 04:01:00
I would like to loop over a "slice" of an iterator. I'm not sure if this is possible as I understand that it is not possible to slice an iterator. What I would like to do is this: def f(): for i in range(100): yield(i) x = f() for i in x[95:]: print(i) This of course fails with: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-37-15f166d16ed2> in <module>() 4 x = f() 5 ----> 6 for i in x[95:]: 7 print(i) TypeError: 'generator' object is not subscriptable Is there a pythonic way to loop through a "slice" of a

How to slice a Pandas Dataframe based on datetime index

痞子三分冷 提交于 2019-12-01 03:44:53
This has been bothering me for ages now: Given a simple pandas DataFrame >>> df Timestamp Col1 2008-08-01 0.001373 2008-09-01 0.040192 2008-10-01 0.027794 2008-11-01 0.012590 2008-12-01 0.026394 2009-01-01 0.008564 2009-02-01 0.007714 2009-03-01 -0.019727 2009-04-01 0.008888 2009-05-01 0.039801 2009-06-01 0.010042 2009-07-01 0.020971 2009-08-01 0.011926 2009-09-01 0.024998 2009-10-01 0.005213 2009-11-01 0.016804 2009-12-01 0.020724 2010-01-01 0.006322 2010-02-01 0.008971 2010-03-01 0.003911 2010-04-01 0.013928 2010-05-01 0.004640 2010-06-01 0.000744 2010-07-01 0.004697 2010-08-01 0.002553 2010

In Python, how do I delete the Nth list item from a list of lists (column delete)?

試著忘記壹切 提交于 2019-12-01 03:11:45
In Python, how do I delete a "column" from a list of lists? Given: L = [ ["a","b","C","d"], [ 1, 2, 3, 4 ], ["w","x","y","z"] ] I would like to delete "column" 2 to get: L = [ ["a","b","d"], [ 1, 2, 4 ], ["w","x","z"] ] Is there a slice or del method that will do that? Something like: del L[:][2] You could loop. for x in L: del x[2] If you're dealing with a lot of data, you can use a library that support sophisticated slicing like that. However, a simple list of lists doesn't slice. just iterate through that list and delete the index which you want to delete. for example for sublist in list:

Golang: convert slices into map

怎甘沉沦 提交于 2019-12-01 03:05:49
Is there an easy/simple means of converting a slice into a map in Golang? Like converting an array into hash in perl is easy to do with simple assignment like %hash = @array this above will convert all the elements in the array into a hash, with keys being even-numbered index elements while the values will be odd-numbered index elements of the array. In my Go code, I have slices of string and would like to convert it into a map. I am wondering if there is a Go's library code to do this. func main() { var elements []string var elementMap map[string]string elements = []string{"abc", "def", "fgi"

Python index out of range in list slicing [duplicate]

对着背影说爱祢 提交于 2019-12-01 01:55:02
问题 This question already has answers here : Why does substring slicing with index out of range work? (3 answers) Closed 6 years ago . While this code will raise indexError: In [1]: lst = [1, 2, 3] In [2]: lst[3] IndexError: list index out of range Slicing the list with "out of range index" will not produce any error. In [3]: lst[3:] Out[3]: [] What is the rationale of this design? 回答1: When you are accessing an element in a list whose index is beyond its length, we cannot return anything. (There