slice

"Slice lists" and "the ellipsis" in Python; slicing lists and lists of lists with lists of slices.

Original question: Can someone tell me how to use "slice lists" and the "ellipsis"? When are they useful? Thanks. Here's what the language definition says about "slice_list" and "ellipsis"; Alex Martelli's answer points out their origin, which is not what I had envisioned. [http://docs.python.org/reference/expressions.html#tok-slici...

Modifying QuerySet result

Is it possible to change some specific items in a QuerySet object? In my case i'm trying to slicing "title" fields with length more than 40 characters and append "..." at the end of field. ...

A question about JavaScript's slice and splice methods

I came across the following code: var f = function () { var args = Array.prototype.slice.call(arguments).splice(1); // some more code }; Basically, the result in args is an array that is a copy of the arguments without its first element. But what I can't understand exactly is why f's arguments (which is an object that holds...

How do I get a hash slice from a hash of hashes?

I have a hash like so: my %h = ( a => { one => 1, two => 2 }, b => { three => 3, four => 4 }, c => { five => 5, six => 6 } ); print join(',', @{$h{a}{qw/one two/}}); The error I get is: Can't use an undefined value as a...

How do I take a reference to an array slice in Perl?

How would you take a reference to an array slice such that when you modify elements of the slice reference, the original array is modified? The following code works due to @_ aliasing magic, but seems like a bit of a hack to me: my @a = 1 .. 10; my $b = sub{\@_}->(@a[2..7]); @$b[0, -1] = qw/ < > /; print "@a\n"; # 1 2 < 4 5 6 7 > 9 10 ...

assigning to an associative array slice in php

In perl, I could assign a list to multiple values in a hash, like so: # define the hash... my %hash = ( foo => 1, bar => 2, baz => 3, ); # change foo, bar, and baz to 4, 5, and 6 respectively @hash{ 'foo', 'bar', 'baz' } = ( 4, 5, 6 ); Is there any way to do the same in php? In fact, is there even a way to get a slice of an ass...

Slice like functionality from a List in F#

With an array let foo = [|1;2;3;4|] I can use any of the following to return a slice from an array. foo.[..2] foo.[1..2] foo.[2..] How can I do the same thing for List let foo2 = [1;2;3;4]? When I try the same syntax as the array I get error FS00039: The field, constructor or member 'GetSlice' is not defined. What's the preferred ...

jquery add or slice

I am trying to wrap a certain number of elements in a div. The problem is that the number of elements can vary based on the user's input. So the number of elements could be 2, 3, 4, or even more. I have a variable that tells me how many elements should be wrapped. So, for instance, my page may have this: <div class="test"></div> <div cl...

What is the point of slice type in go (language)

Hi, I have read this but still not fully aware of the advantage of slice against array.So I am expecting somebody in SO explain better than it and I am sure you can :) ...

ActionScript: How to push to multidimensional arrays and later retrieve just one 'row'

I am reading a set of latitude & Longitude Coordinates that define a polygone area. They are keyed to an area ID and I retrieve them from a SQL database. So for example, Area ID 153 might have 20 coordinates and area ID 77 might have 11 coordinates. I wish to save these in a 2-D array indexed by the area ID, and where each coordinate pa...

Explanation of [].slice.call in javascript?

I stumbled onto this neat shortcut for converting a DOM NodeList into a regular array, but I must admit, I don't completely understand how it works: [].slice.call(document.querySelectorAll('a'), 0) So it starts with an empty array [], then slice is used to convert the result of call to a new array yeah? The bit I don't understand is ...

Ignoring case, punctuation, and whitespace in Strings

What is the most efficient way of ignoring case, punctuation, and whitespace in strings? These strings should be divided into words instead of characters should ignore the aforementioned details on comparisons, and slices of these word-strings should be as efficient as possible with speed in mind. I was going to use case and punctuation...

Adobe Fireworks CS4 Queries

Hi, I am new to Firewroks CS4 and keen to learn to use it right but I am having difficulty understanding the whole slicing and exporting mechanism, so that I can also use in Dreamweaver CS4. Basically, I am unsure how to do the following and if anyone can assist with the process or point me to a possible online tutorial, that would be ...

how to use timeuuid names in cassandra

Hi guys, I have a problem, I would like to build logging system which will be using timeuuid type as a column name which will allow me ask for it later. Since for range queries order by time I need to use timeuuid type I would like to ask you how can I specify range queries for timeuuid column names if timeuuid is every time unique and...

Preserving the dimensions of a slice from a Numpy 3d array

I have a 3d array, a, of shape say a.shape = (10, 10, 10) When slicing, the dimensions are squeezed automatically i.e. a[:,:,5].shape = (10, 10) I'd like to preserve the number of dimensions but also ensure that the dimension that was squeezed is the one that shows 1 i.e. a[:,:,5].shape = (10, 10, 1) I have thought of re-casting the...

Python: Slicing a list into n nearly-equal-length partitions

I'm looking for a fast, clean, pythonic way to divide a list into exactly n nearly-equal partitions. partition([1,2,3,4,5],5)->[[1],[2],[3],[4],[5]] partition([1,2,3,4,5],2)->[[1,2],[3,4,5]] (or [[1,2,3],[4,5]]) partition([1,2,3,4,5],3)->[[1,2],[3,4],[5]] (there are other ways to slice this one too) There are several answers in here h...

Problem with list slice syntax in python

The extended indexing syntax is mentioned in python's doc. slice([start], stop[, step]) Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator. a[start:stop:step] works as described. But what ...

Perl - How to get the number of elements in an anonymous array, for concisely trimming pathnames

Hi Everyone, I'm trying to get a block of code down to one line. I need a way to get the number of items in a list. My code currently looks like this: # Include the lib directory several levels up from this directory my @ary = split('/', $Bin); my @ary = @ary[0 .. $#ary-4]; my $res = join '/',@ary; lib->import($res.'/lib'); That's ...

Difference between list, sequence and slice in Python?

What are the differences between these built-in Python data types: list, sequence and slice? As I see it, all three essentially represent what C++ and Java call array. ...

Perl Hash Slice, Replication x Operator, and sub params

Ok, I understand perl hash slices, and the "x" operator in Perl, but can someone explain the following code example from here (slightly simplified)? sub test{ my %hash; @hash{@_} = (undef) x @_; } Example Call to sub: test('one', 'two', 'three'); This line is what throws me: @hash{@_} = (undef) x @_; It is creating a ha...