built-in

What's “better” the reverse method or the reversed built-in function?

我怕爱的太早我们不能终老 提交于 2019-11-30 12:19:37
What is typically regarded as more Pythonic/better/faster to use, the reverse method or the reversed built-in function? Both in action: _list = list(xrange(4)) print _list rlist = list(reversed(_list)) print rlist _list.reverse() print _list Depends on whether you want to reverse the list in-place (i.e. change the list) or not. No other real difference. Often using reversed leads to nicer code. foo.reverse() actually reverses the elements in the container. reversed() doesn't actually reverse anything, it merely returns an object that can be used to iterate over the container's elements in

Built-in binary search tree in Python? [closed]

≡放荡痞女 提交于 2019-11-30 11:15:57
Are there any self-balancing binary search tree ( RED-BLACK , AVL or others) built-in types in Python 2.7 or Python 3.x? I am looking for something equivalent to Java's TreeMap or TreeSet . If there are no such built-ins, why have they been ommited? Is there a special reason, for not including such tools? There's no special reason, to my knowledge - I'd guess that the reason is that for so many applications the highly-tuned dict and set implementations (which are hash tables) work well. They're good enough in most cases. There are definitely situations where you need the performance

Haskell replace element in list

心不动则不痛 提交于 2019-11-30 08:19:15
Is there any built-in function to replace an element at a given index in haskell? Example: replaceAtIndex(2,"foo",["bar","bar","bar"]) Should give: ["bar", "bar", "foo"] I know i could make my own function, but it just seems it should be built-in. hammar If you need to update elements at a specific index, lists aren't the most efficient data structure for that. You might want to consider using Seq from Data.Sequence instead, in which case the function you're looking for is update :: Int -> a -> Seq a -> Seq a . > import Data.Sequence > update 2 "foo" $ fromList ["bar", "bar", "bar"] fromList [

No module named builtins

孤街浪徒 提交于 2019-11-30 08:13:31
I'm trying to convert my .py script into an executable using py2exe. I've had a number of issues so far that have been largely addressed by the "options" in the setup file below. But now I have a problem that I have not been able to find a solution for, and wondering if others have had this same issue and fixed it. When I execute the setup file below using "python setup.py py2exe" it gives me an executable but when I run it, it complains "No module named builtins". The only other post I could find on this subject indicated that builtins is a python3 thing, but I'm running 2.7. Appreciate any

why __builtins__ is both module and dict

岁酱吖の 提交于 2019-11-30 08:11:35
问题 I am using the built-in module to insert a few instances, so they can be accessed globally for debugging purposes. The problem with the __builtins__ module is that it is a module in a main script and is a dict in modules, but as my script depending on cases can be a main script or a module, I have to do this: if isinstance(__builtins__, dict): __builtins__['g_frame'] = 'xxx' else: setattr(__builtins__, 'g_frame', 'xxx') Is there a workaround, shorter than this? More importantly, why does _

itertools.ifilter Vs. filter Vs. list comprehensions

陌路散爱 提交于 2019-11-30 08:03:07
I am trying to become more familiar with the itertools module and have found a function called ifilter . From what I understand, it filters and iterable based on the given function and returns an iterator over a list containing the elements of the iterable on which the function evaluates to True . Question 1 : is my understanding thus far correct? Question 2 : aside from the fact that this returns and iterator, how is it different from the built-in filter function? Question 3 Which is faster? From what I can tell, it is not. Am I missing something? (I ran the following test) >>> itertools

Don't display pushd/popd stack across several bash scripts (quiet pushd/popd)

不打扰是莪最后的温柔 提交于 2019-11-30 04:08:48
Each time I use pushd or popd, it print the stack to standard output. How not to do so? I don't want to do pushd > /dev/null each time because I have a lot of scripts calling each other. Maybe a nice override will do it, but I'll need to override these builtins only in my scripts, and then restore the correct behavior. You could add pushd () { command pushd "$@" > /dev/null } popd () { command popd "$@" > /dev/null } to the top of each script. This is probably the minimum amount of work it will take to solve your problem. In your .profile file (what ever it is called in your system) add: pushd

PHP array function that returns a subset for given keys

余生长醉 提交于 2019-11-30 00:19:04
问题 I'm looking for an array function that does something like this: $myArray = array( 'apple'=>'red', 'banana'=>'yellow', 'lettuce'=>'green', 'strawberry'=>'red', 'tomato'=>'red' ); $keys = array( 'lettuce', 'tomato' ); $ret = sub_array($myArray, $keys); where $ret is: array( 'lettuce'=>'green', 'tomato'=>'red' ); A have no problem in writing it down by myself, the thing is I would like to avoid foreach loop and adopt a built-in function or a combination of built-in functions. It seems to me

How to find a value in array and remove it by using PHP array functions

为君一笑 提交于 2019-11-29 20:24:34
How to find a value exist in an array and how to remove it. If any php builtin array functions for doing this. After removing I need the sequential index order. any body knows please help me. To search an element in an array, you can use array_search function and to remove an element from an array you can use unset function. Ex: <?php $hackers = array ('Alan Kay', 'Peter Norvig', 'Linus Trovalds', 'Larry Page'); print_r($hackers); // Search $pos = array_search('Linus Trovalds', $hackers); echo 'Linus Trovalds found at: ' . $pos; // Remove from array unset($hackers[$pos]); print_r($hackers); ?>

What's “better” the reverse method or the reversed built-in function?

风格不统一 提交于 2019-11-29 17:14:15
问题 What is typically regarded as more Pythonic/better/faster to use, the reverse method or the reversed built-in function? Both in action: _list = list(xrange(4)) print _list rlist = list(reversed(_list)) print rlist _list.reverse() print _list 回答1: Depends on whether you want to reverse the list in-place (i.e. change the list) or not. No other real difference. Often using reversed leads to nicer code. 回答2: foo.reverse() actually reverses the elements in the container. reversed() doesn't