slice

python create slice object from string

青春壹個敷衍的年華 提交于 2019-12-09 06:05:40
问题 I'd like to create a slice object from a string; right now the only way seems through a cumbersome hacky eval statement class getslice: def __getitem__(self, idx): return idx[0] eval("getslice()[%s, 1]" %(":-1")) thanks in advance. Edit : Sorry if the original prompt was not clear, the input in this case was ":-1". The point was to parse the string. Ignacio Vazquez-Abrams's response at least solved the problem (and seems to work with reverse indexing as well), but I think my solution above is

cap vs len of slice in golang

跟風遠走 提交于 2019-12-09 04:13:25
问题 What is the difference between cap and len of a slice in golang? According to definition: A slice has both a length and a capacity. The length of a slice is the number of elements it contains. The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice. x := make([]int, 0, 5) // len(b)=0, cap(b)=5 Does the len mean non null values only? 回答1: A slice is an abstraction that uses an array under the covers. cap tells you the capacity of

Efficient iteration over slice in Python

烂漫一生 提交于 2019-12-09 02:47:54
问题 How efficient are iterations over slice operations in Python? And if a copy is inevitable with slices, is there an alternative? I know that a slice operation over a list is O(k), where k is the size of the slice. x[5 : 5+k] # O(k) copy operation However, when iterating over a part of a list, I find that the cleanest (and most Pythonic?) way to do this (without having to resort to indices) is to do: for elem in x[5 : 5+k]: print elem However my intuition is that this still results in an

Saving with Java springdata a mongoDB document with capped array ($slice and $sort)

痞子三分冷 提交于 2019-12-08 21:06:37
I'm developing a log register using mongoDB and Java SpringData. Here MongoDb capped sub-collection talks about mongoDB structure, but I would do with Java. The most important thing it's that I have a document with one or more fields and a capped array. Is there some method or way in Java to do this? My object it's like: user = { name: String, latest_messages: [String] (capped to a 100 elements) } in Java: public class MessageLog { private ObjectId id; private String name; private List<Message> messages; } Where: public class Message{ private String text; private String level; private Date

Slicing numpy array with another array

邮差的信 提交于 2019-12-08 16:05:27
问题 I've got a large one-dimensional array of integers I need to take slices off. That's trivial, I'd just do a[start:end] . The problem is that I need more of these slices. a[start:end] does not work if start and end are arrays. For loop could be used for this, but I need it to be as fast as possible (it is a bottleneck), so a native numpy solution would be welcome. To further illustrate, I have this: a = numpy.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], numpy.int16) start = numpy.array([1, 5,

How to slice (in Python) “all but the last n” items when n may be zero? [duplicate]

試著忘記壹切 提交于 2019-12-08 14:54:25
问题 This question already has answers here : List slicing with dynamic index on [:index] (4 answers) Closed 4 years ago . I have a list of items in Python and I need to get "all but the last N" items. It needs to work when N is zero (in which case I want the whole list) and when N is greater than or equal to the length of the list (in which case I want an empty list). This works in most cases: mylist=[0,1,2,3,4,5,6,7,8,9] print( mylist[:-n] ) But it fails in the case where N is zero. mylist[:0]

Python Slicing 'bob' in s [duplicate]

亡梦爱人 提交于 2019-12-08 08:12:49
问题 This question already has answers here : String count with overlapping occurrences (23 answers) Closed 6 years ago . s = 'gfdhbobobyui' bob = 0 for x in range(len(s)): if x == 'bob': bob += 1 print('Number of times bob occurs is: ' + str(bob)) Attempting to write a code that will count the amount of times 'bob' appears in s, but for some reason this always outputs 0 for number of 'bob'. 回答1: Here, try this, handcrafted :) for i, _ in enumerate(s): #i here is the index, equal to "i in range

Saving with Java springdata a mongoDB document with capped array ($slice and $sort)

♀尐吖头ヾ 提交于 2019-12-08 05:40:17
问题 I'm developing a log register using mongoDB and Java SpringData. Here MongoDb capped sub-collection talks about mongoDB structure, but I would do with Java. The most important thing it's that I have a document with one or more fields and a capped array. Is there some method or way in Java to do this? My object it's like: user = { name: String, latest_messages: [String] (capped to a 100 elements) } in Java: public class MessageLog { private ObjectId id; private String name; private List

How to make the circles disappear based on keyboard input?

岁酱吖の 提交于 2019-12-08 04:09:29
问题 based on this example: http://bl.ocks.org/d3noob/10633704 i wish to make an input on my keyboard (a number) and make the circles disappear with the help of array.slice() . Unfortunally it did not worked well. In my code, i created some circles based on the values of the array days . With the HTML part i am able to create a button, where i can make a number input. With the last part days.slice(nValue) i want that the input number is the same like the number inside the brackets of the slice()

non adjacent slicing of numpy multidimensional array in python

独自空忆成欢 提交于 2019-12-08 00:23:06
问题 I have a multidimensional array a: a = np.random.uniform(1,10,(2,4,2,3,10,10)) For dimensions 4-6, I have 3 lists which contain the indexes for slicing that dimension of array 'a' dim4 = [0,2] dim5 = [3,5,9] dim6 = [1,2,7,8] How do I slice out array 'a' such that i get: b = a[0,:,0,dim4,dim5,dim6] So b should be an array with shape (4,2,3,4), and containing elements from the corresponding dimensions of a. When I try the code above, I get an error saying that different shapes can't be