in-place

implementing add and iadd for custom class in python?

爷,独闯天下 提交于 2019-12-25 04:22:25
问题 I am writing a Queue class that wraps list for most of its operations. But I do not sublcass from list , since I do not want to provide all the list API's . I have my code pasted below. The add method seems to work fine, but iadd seems to go wrong, it is printing none. Here is the code: import copy from iterator import Iterator class Abstractstruc(object): def __init__(self): assert False def __str__(self): return "<%s: %s>" %(self.__class__.__name__,self.container) class Queue(Abstractstruc

Syntax error when attempting to amend a string with indexing

风流意气都作罢 提交于 2019-12-25 00:34:16
问题 I'm studying APL from here. Why am I getting this syntax error? 'computer' [ 1 2 3 ] ← 'COM' SYNTAX ERROR 'computer'[1 2 3]←'COM' ^ But if I save 'computer' in a variable I don't get the error: T ← 'computer' T computer T[1 2 3] ← 'COM' T COMputer What am I doing wrong? 回答1: 'computer' is a constant, and you can't change the value of a constant itself, only the current value of a variable. Think about it: If you could assign to 'computer' , then next time you wrote 'computer' , would you

NumPy in-place operations with overlapping slices

房东的猫 提交于 2019-12-24 17:22:53
问题 Consider this out-of-place operation: >>> b = numpy.asarray([1, 2, 3]) >>> b[1:] = b[1:] - b[:-1] >>> b array([1, 1, 1]) Now consider the in-place operation: >>> a = numpy.asarray([1, 2, 3]) >>> a[1:] -= a[:-1] >>> a array([1, 1, 2]) They give different results, which I did not expect. Update: It seems this behavior changed; now they give the same result. I would have assumed NumPy would have done the subtraction in the correct order (backward) so that they would give equivalent results to

Re-arranging numpy array in place

ぐ巨炮叔叔 提交于 2019-12-24 01:47:15
问题 I am trying to modify a numpy array "in-place". I am interested in re-arranging the array in-place (instead of return:ing a re-arranged version of the array). Here is an example code: from numpy import * def modar(arr): arr=arr[[1,0]] # comment & uncomment this line to get different behaviour arr[:,:]=0 print "greetings inside modar:" print arr def test2(): arr=array([[4,5,6],[1,2,3]]) print "array before modding" print arr print modar(arr) print print "array now" print arr test2() The

Python “IN PLACE” functions

China☆狼群 提交于 2019-12-21 06:18:59
问题 What's the particular reason that some functions in Python operate "IN PLACE", like [].sort and [].reverse , while some others like [].append not? 回答1: According to my Programming Python 4th edition: By default, pop is equivalent to fetching, and then deleting, the last item at offset −1. With an argument, pop deletes and returns the item at that offset—list.pop(-1) is the same as list.pop(). For in-place change operations like append, insert, del, and pop, no new list is created in memory,

guidelines on using pandas inplace keyword argument

一世执手 提交于 2019-12-18 16:46:43
问题 What is the guideline for using inplace ? For example, df = df.reset_index() or df.reset_index(inplace=True) Same same but different? 回答1: In terms of the resulting DataFrame df , the two approaches are the same. The difference lies in the (maximum) memory usage, since the in-place version does not create a copy of the DataFrame. Consider this setup: import numpy as np import pandas as pd def make_data(): return pd.DataFrame(np.random.rand(1000000, 100)) def func_copy(): df = make_data() df =

Sorting in linear time and in place

寵の児 提交于 2019-12-18 11:28:39
问题 Suppose that n records have keys in the range from 1 to k. Write an algorithm to sort the records in place in O(n+k) time. You may use O(k) storage outside the input array. Is your algorithm stable? if we use counting sort to we can do it in O(n+k) time and is stable but its not in place. if k=2 it can be done in place but its not stable (using two variables to maintain the indexes in the array for k=0 and k=1) but for k>2 i couldnt think of any good algo 回答1: First, let's rehash how counting

Why piping to the same file doesn't work on some platforms?

风格不统一 提交于 2019-12-18 08:25:59
问题 In cygwin, the following code works fine $ cat junk bat bat bat $ cat junk | sort -k1,1 |tr 'b' 'z' > junk $ cat junk zat zat zat But in the linux shell(GNU/Linux), it seems that overwriting doesn't work [41] othershell: cat junk cat cat cat [42] othershell: cat junk |sort -k1,1 |tr 'c' 'z' zat zat zat [43] othershell: cat junk |sort -k1,1 |tr 'c' 'z' > junk [44] othershell: cat junk Both environments run BASH. I am asking this because sometimes after I do text manipulation, because of this

Separate the alphabet and digit such that their relative order remains the same in O(n) time and O(1) space

為{幸葍}努か 提交于 2019-12-17 22:24:19
问题 Given an array [a1b7c3d2] convert to [abcd1732] with O(1) space and O(n) time i.e. put the letters on the left and digits on the right such that their relative order is the same. I can think of an O(nlogn) algorithm, but not better. Can somebody please help? 回答1: AFAIK it can't be done. This is essentially a single step of the RADIX sort algorithm. And AFAIK stable RADIX sort can't be done in-place. edit Wikipedia agrees with me (for what that's worth): http://en.wikipedia.org/wiki/Radix_sort

Stable separation for two classes of elements in an array

岁酱吖の 提交于 2019-12-17 13:03:40
问题 Consider the following problem. We are given an array of elements belonging to one two classes: either red or blue. We have to rearrange the elements of the array so that all blue elements come first (and all red elements follow). The rearrangement must be done is stable fashion, meaning that the relative order of blue elements must be preserved (same for red ones). Is there a clever algorithm that would perform the above rearrangement in-place? A non-in place solution is, of course,