slice

Slice a string in groovy

点点圈 提交于 2019-12-01 15:35:44
I have a 18 character string I want characters 2-8 from. In python I can do this: sliceMe = "nnYYYYYYnnnnnnnnnn" print sliceMe[2:8] prints YYYYYY I am looking for a way to do this same thing in groovy, and every explanation is REALLY long. Whats the elegant accepted way to do this in groovy (or java for that matter)? groovy:000> sliceMe = "nnYYYYYYnnnnnnnnnn" ===> nnYYYYYYnnnnnnnnnn groovy:000> sliceMe[2..7] ===> YYYYYY Note the difference in the length being 1 less. BZ. You inherit all the Java methods off String so sliceMe.substring(2,7) should do the trick. For future reference, you can

Why does list[::-1] not equal list[:len(list):-1]?

橙三吉。 提交于 2019-12-01 15:30:49
问题 When slicing in python, omitting the end portion of the slice (ie the end in list[:end:] ) results in end being defined as "the size of the string being sliced." * However, this doesn't seem to hold true when using the step argument (the step in list[::step] ) in a slice, at least when the step argument is -1 . A simple example: >>> l = [1, 2, 3] >>> l[::-1] [3, 2, 1] >>> l[:len(l):-1] [] This indicates that in the case of a step argument being passed, an omitted end value is not equivalent

using .slice method on an array

佐手、 提交于 2019-12-01 14:32:29
I'm practicing the array section of JavaScript Koan and I'm not fully understanding why these answers are correct. I added my assumptions below if someone could please clarify/let me know if I'm wrong : it("should slice arrays", function () { var array = ["peanut", "butter", "and", "jelly"]; expect(array.slice(3, 0)).toEqual([]); Why wouldn't it at least slice "jelly" since the slice begins with 3? How does the cut off of 0 make it empty instead? expect(array.slice(3, 100)).toEqual(["jelly"]); If the cut off index goes beyond what currently exists in the array, does this mean that a new array

Slice a string in groovy

眉间皱痕 提交于 2019-12-01 13:40:59
问题 I have a 18 character string I want characters 2-8 from. In python I can do this: sliceMe = "nnYYYYYYnnnnnnnnnn" print sliceMe[2:8] prints YYYYYY I am looking for a way to do this same thing in groovy, and every explanation is REALLY long. Whats the elegant accepted way to do this in groovy (or java for that matter)? 回答1: groovy:000> sliceMe = "nnYYYYYYnnnnnnnnnn" ===> nnYYYYYYnnnnnnnnnn groovy:000> sliceMe[2..7] ===> YYYYYY Note the difference in the length being 1 less. 回答2: You inherit all

Slicing element from sublist - Python

我的梦境 提交于 2019-12-01 13:34:29
问题 I want to return the number 5 from this: list_1 = [[1, 2, 3], [4, 5, 6]] I thought this would work but it is not: print(list_1[1:1]) It returns an empty list. It is Index 1 (second list) and position 1 (second number in the list). Shouldn't that work? 回答1: You need two separate operations: sub_list = list_1[1] item = sub_list[1] # or shortly list_1[1][1] What you did was calling the slice interface which has an interface of [from:to:step] . So it meant: "give me all the items from index 1 to

using .slice method on an array

有些话、适合烂在心里 提交于 2019-12-01 13:26:06
问题 I'm practicing the array section of JavaScript Koan and I'm not fully understanding why these answers are correct. I added my assumptions below if someone could please clarify/let me know if I'm wrong : it("should slice arrays", function () { var array = ["peanut", "butter", "and", "jelly"]; expect(array.slice(3, 0)).toEqual([]); Why wouldn't it at least slice "jelly" since the slice begins with 3? How does the cut off of 0 make it empty instead? expect(array.slice(3, 100)).toEqual(["jelly"])

1.splice(),slice(),split()快查

不问归期 提交于 2019-12-01 12:58:43
1.splice() 对数组添加/删除项目,然后返回该数组。注意:该方法会改变原始数组。 arrayObject.splice(index,howmany,item1,.....,itemX);index:起始位置,使用负数可从数组结尾处规定位置;howmany:要删除的元素个数,可以为0;item1,.....,itemX参数可选; <script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" arr.splice(2,0,"William");//在arr【1】后面插入数据“William” arr.splice(2,1,"William");//删除arr【1】后面的一个数据“Thomas”,然后在该位置插入数据“William” arr.splice(2,3,"William");//删除arr【1】后面的三个数据“Thomas”、“James”,“Adrew”,然后在该位置插入数据“William” </script> 2.slice() 从已有的数组中返回选定的元素。注意:不会修改原始数组。 arrayObject

matching end of string

元气小坏坏 提交于 2019-12-01 12:42:58
I'm looking for the best most efficient way to match the end of a single string with a value from a predefined list of strings. Something like my_str='QWERTY' my_lst=['QWE','QQQQ','TYE','YTR','TY'] match='TY' or match=['TY'] Under the restrictions len(my_lst) is known but arbitrary thus could be very long, probably around 30 elements in my_lst may have different len so I can't just check a defined last portion of my_str every time for my_str as well as the matching elements in my_lst they can be either strings or lists, whichever is more efficient (see background) len(my_str) is mostly small,

Implementation and performance difference between Python's insert() method and inserting by slicing

跟風遠走 提交于 2019-12-01 12:11:42
问题 What is the difference between inserting an element in a python list in the following ways? myList.insert(at, myValue) myList[at:at] = [myValue] I have run some tests and the performance of the two are very similar, but the slicing insert consistently produces slightly better results. My question is regarding the difference in implementation and performance, not the behaviour. 回答1: We have the same behaviour, see bellow: The default behaviour is to insert the item at the given index; each

Python : Get many list from a list [duplicate]

萝らか妹 提交于 2019-12-01 11:32:10
Possible Duplicate: How do you split a list into evenly sized chunks in Python? Hi, I would like to split a list in many list of a length of x elements, like: a = (1, 2, 3, 4, 5) and get : b = ( (1,2), (3,4), (5,) ) if the length is set to 2 or : b = ( (1,2,3), (4,5) ) if the length is equal to 3 ... Is there a nice way to write this ? Otherwise I think the best way is to write it using an iterator ... The itertools module documentation . Read it, learn it, love it. Specifically, from the recipes section: import itertools def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --