slice

Swift Array instance method drop(at: Int)

可紊 提交于 2019-12-13 03:47:49
问题 Array in Swift has several instance methods for excluding elements, such as dropFirst(), dropLast(), drop(while:), etc. What about drop(at:) ? Note: I'd use remove(at:), but the array I'm working with is a let constant. 回答1: Note: I'd use remove(at:) , but the array I'm working with is a constant. I wouldn't let that stop you: extension Array { func drop(at:Int) -> [Element] { var temp = self temp.remove(at: at) return temp } } let numbers = [1, 2, 3, 4, 5] print(numbers.drop(at: 2)) // [1, 2

Using string as array indices in NumPy

此生再无相见时 提交于 2019-12-13 03:38:11
问题 I'm handling large numerical arrays in python through a GUI. I'd like to expose the slicing capabilities to a textbox in a GUI, so I can easily choose part of the array that should be used for the calculation at hand. Simple example of what I'd like to do: arr = array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) a = "2:4" # example string from GUI Textbox b = "[3, 4, 5]" # example string from GUI Textbox print arr[a] # not valid code -> what should be written here to make it work? print arr[b] #

filter tensorflow array with specific condition over numpy array

筅森魡賤 提交于 2019-12-13 03:33:02
问题 I have a tensorflow array names tf-array and a numpy array names np_array . I want to find specific rows in tf_array with regards to np-array . tf-array = tf.constant( [[9.968594, 8.655439, 0., 0. ], [0., 8.3356, 0., 8.8974 ], [0., 0., 6.103182, 7.330564 ], [6.609862, 0., 3.0614321, 0. ], [9.497023, 0., 3.8914037, 0. ], [0., 8.457685, 8.602337, 0. ], [0., 0., 5.826657, 8.283971 ]]) I also have an np-array: np_array = np.matrix( [[2, 5, 1], [1, 6, 4], [0, 0, 0], [2, 3, 6], [4, 2, 4]] Now I

how to do slice assignment while the slice itself is a tensor in tensorflow

时间秒杀一切 提交于 2019-12-12 22:47:42
问题 I want to do slice assignment in tensorflow. I got to know that I can use: my_var = my_var[4:8].assign(tf.zeros(4)) base on this link. as you see in my_var[4:8] we have specific indices 4, 8 here for slicing and then assignment. My case is different I want to do slicing based on a tensor and then do the assignment. out = tf.Variable(tf.zeros(shape=[8,4], dtype=tf.float32)) rows_tf = tf.constant ( [[1, 2, 5], [1, 2, 5], [1, 2, 5], [1, 4, 6], [1, 4, 6], [2, 3, 6], [2, 3, 6], [2, 4, 7]]) columns

MongoDB slice query into golang

◇◆丶佛笑我妖孽 提交于 2019-12-12 20:04:00
问题 How can i write this below slice query into golang? db.con.find({"repoid":1356485},{"contr":{$slice:[0,10]}}).pretty() Tried with this but not working DB.C("con").Find(bson.M{"id": ID, "contr": bson.M{"$slice": []interface{}{"$contr", offset, limit}}}) does not find anything. Any ideas? Thank you in advance 回答1: With Collection.Find() you can only specify the filter. But what you have is a projection: {"contr":{$slice:[0,10]} Projections can be specified using Query.Select(), so this is how

Write list variable to file

六月ゝ 毕业季﹏ 提交于 2019-12-12 17:51:07
问题 I have a .txt file of words I want to 'clean' of swear words, so I have written a program which checks each position, one-by-one, of the word list, and if the word appears anywhere within the list of censorable words, it removes it with var.remove(arg) . It's worked fine, and the list is clean, but it can't be written to any file. wordlist is the clean list. newlist = open("lists.txt", "w") newlist.write(wordlist) newlist.close() This returns this error: newlist.write(wordlist) TypeError:

How to perform operations along a certain dimension of an array?

假装没事ソ 提交于 2019-12-12 16:14:55
问题 I have a 3D array containing five 3-by-4 slices, defined as follows: rng(3372061); M = randi(100,3,4,5); I'd like to collect some statistics about the array: The maximum value in every column. The mean value in every row. The standard deviation within each slice. This is quite straightforward using loops, sz = size(M); colMax = zeros(1,4,5); rowMean = zeros(3,1,5); sliceSTD = zeros(1,1,5); for indS = 1:sz(3) sl = M(:,:,indS); sliceSTD(indS) = std(sl(1:sz(1)*sz(2))); for indC = 1:sz(1) rowMean

Keras tensors - Get values with indices coming from another tensor

别说谁变了你拦得住时间么 提交于 2019-12-12 13:12:15
问题 Suppose I have these two tensors: valueMatrix , shaped as (?, 3) , where ? is the batch size indexMatrix , shaped as (?, 1) I want to retrieve values from valueMatrix at the indices contained in indexMatrix . Example (pseudocode): valueMatrix = [[7,15,5],[4,6,8]] -- shape=(2,3) -- type=float indexMatrix = [[1],[0]] -- shape = (2,1) -- type=int I want from this example to do something like: valueMatrix[indexMatrix] --> returns --> [[15],[4]] I prefer Tensorflow over other backends, but the

Indexing numpy multidimensional arrays depends on a slicing method

雨燕双飞 提交于 2019-12-12 13:01:42
问题 I have a 3-D array. When I take a 2-D slice of it the result depends on whether it is indexed with a list or with a slice. In the first case the result is transposed. Didn't find this behaviour in the manual. >>> import numpy as np >>> x = np.array([[[1,1,1],[2,2,2]], [[3,3,3],[4,4,4]]]) >>> x array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]) >>> x[0,:,[0,1]] array([[1, 2], [1, 2]]) >>> x[0,:,slice(2)] array([[1, 1], [2, 2]]) >>> Could anyone point a rationale for this? 回答1: As I

What are the consequences of “$scalar = @array[n]”?

試著忘記壹切 提交于 2019-12-12 12:20:05
问题 use warnings; my @array = (0, 1); my $scalar1 = $array[0]; my $scalar2 = @array[0]; if($scalar1 == $scalar2) { print "scalars are equal\n"; } Here's the output when I run /usr/bin/perl5.10.1 test.pl : Scalar value @array[0] better written as $array[0] at test.pl line 4. scalars are equal I'm concerned about that warning. 回答1: You can look up all warning messages in perldoc perldiag, which explains the consequences: (W syntax) You've used an array slice (indicated by @) to select a single