slice

How to slice a string in PHP?

旧城冷巷雨未停 提交于 2019-12-30 06:57:04
问题 Ok, so I've got this string: "MICROSOFT CORP CIK#: 0000789019 (see all company filings)" And I would like to cut off everything after the "CORP" bit. How would I go about doing this in PHP? I am used to Python so I am not sure how this is done. To be clear, this is the output I want: "MICROSOFT CORP" I am trying: $companyname = substr($companyname, 0, strpos($companyname, " CIK")); and I am getting nothing showing. Here is my full code: <?php include 'simple_html_dom.php'; $html = file_get

JavaScript array slice versus delete

孤人 提交于 2019-12-30 06:48:06
问题 Is there any reason why one should be used over the other? e.g. var arData=['a','b','c']; arData.slice(1,1);//removes 'b' var arData=['a','b','c']; delete arData[1];//removes 'b' 回答1: delete leaves you with [ 'a', undefined, 'c' ] splice leaves you with [ 'a', 'c' ] slice doesn't do anything to the original array :) But it returns [ 'b' ] in your code 回答2: delete only makes that certain location of the array undefined but the array still contains 3 items: ['a',undefined,'c'] the other way to

What's the best way to get the last N elements of a Perl array?

时光总嘲笑我的痴心妄想 提交于 2019-12-30 02:43:09
问题 What's the best way to get the last N elements of a Perl array? If the array has less than N, I don't want a bunch of undefs in the return value. 回答1: @last_n = @source[-$n..-1]; If you require no undef s, then: @last_n = ($n >= @source) ? @source : @source[-$n..-1]; 回答2: I think what you want is called a slice. 回答3: @a = (a .. z); @last_five = @a[ $#a - 4 .. $#a ]; say join " ", @last_five; outputs: v w x y z 回答4: simple, no math: @a = reverse @a; @a = splice(@a, 0, $elements_to_keep); @a =

How to iterate over the first n elements of a list?

为君一笑 提交于 2019-12-29 18:17:11
问题 Say I've got a list and I want to iterate over the first n of them. What's the best way to write this in Python? 回答1: The normal way would be slicing: for item in your_list[:n]: ... 回答2: I'd probably use itertools.islice (<- follow the link for the docs), which has the benefit of working with any iterable object. 回答3: You can just slice the list: >>> l = [1, 2, 3, 4, 5] >>> n = 3 >>> l[:n] [1, 2, 3] and then iterate on the slice as with any iterable. 回答4: Python lists are O(1) random access,

How to iterate over the first n elements of a list?

末鹿安然 提交于 2019-12-29 18:17:08
问题 Say I've got a list and I want to iterate over the first n of them. What's the best way to write this in Python? 回答1: The normal way would be slicing: for item in your_list[:n]: ... 回答2: I'd probably use itertools.islice (<- follow the link for the docs), which has the benefit of working with any iterable object. 回答3: You can just slice the list: >>> l = [1, 2, 3, 4, 5] >>> n = 3 >>> l[:n] [1, 2, 3] and then iterate on the slice as with any iterable. 回答4: Python lists are O(1) random access,

Memory efficient extraction of overlapping patches from matrix

拈花ヽ惹草 提交于 2019-12-29 09:20:07
问题 short story: This is a follow up question to: Fast Way to slice image into overlapping patches and merge patches to image How must I adapt the code provided in the answer to work not only on images of size x,y, where a pixel is described by a float, but described by a matrix of size 3,3? Further, how to adapt the code so that it returns a generator allowing me to iterate over all patches without having to save all of them in memory? long story: Given an image of shape (x,y), where each pixel

Problem with list slice syntax in python

旧时模样 提交于 2019-12-29 07:39:30
问题 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 about the second one? How is it used? 回答1: a[start:stop,i] calls the method a.__getitem__((slice(start,stop,None), i)) . This raises a TypeError if a is a list,

Longest substring in alphabetical order [closed]

六眼飞鱼酱① 提交于 2019-12-29 07:19:09
问题 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 5 years ago . Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabetical order is: beggh In the case of ties, print the first substring. For example, if s = 'abcbcd', then your

Skip every nth index of numpy array

此生再无相见时 提交于 2019-12-29 01:38:06
问题 In order to do K-fold validation I would like to use slice a numpy array such that a view of the original array is made but with every nth element removed. For example: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] If n = 4 then the result would be [1, 2, 4, 5, 6, 8, 9] Note: the numpy requirement is due to this being used for a machine learning assignment where the dependencies are fixed. 回答1: Approach #1 with modulus a[np.mod(np.arange(a.size),4)!=0] Sample run - In [255]: a Out[255]: array([0, 1, 2, 3, 4

Numpy slicing from variable

走远了吗. 提交于 2019-12-28 15:21:12
问题 I'm trying to slice a numpy array using a slice that is predefined in a variable. This works: b = fromfunction(lambda x,y: 10*x+y, (5,4),dtype=int) # Just some matrix b[1:3,1:3] # Output: # array([[11, 12], # [21, 22]]) But what I want to do is somthing like this: slice = "1:3,1:3" b[slice] # Output: # array([[11, 12], # [21, 22]]) It is not important to me what type the slice-variable has, I'm just using a string as an example. How do I save a slice-specifier like that? 回答1: You can use the