title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
492,728
<p>Unless your regex is extremely simple (i.e. no stars or pluses), there will be infinitely many strings which match it. If your regex only involves concatenation and alternation, then you can expand each alternation into all of its possibilities, e.g. <code>(foo|bar)(baz|quux)</code> can be expanded into the list <c...
5
2009-01-29T18:08:43Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
492,733
<p>I haven't seen a Python module to do this, but I did see a (partial) implementation in Perl: <a href="http://search.cpan.org/~bowmanbs/Regexp-Genex-0.07/lib/Regexp/Genex.pm" rel="nofollow"><code>Regexp::Genex</code></a>. From the module description, it sounds like the implementation relies on internal details of Per...
2
2009-01-29T18:09:52Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
496,603
<p>I don't know of any module to do this. If you don't find anything like this in the Cookbook or PyPI, you could try rolling your own, using the (undocumented) re.sre_parse module. This might help getting you started:</p> <pre><code>In [1]: import re In [2]: a = re.sre_parse.parse("[abc]+[def]*\d?z") In [3]: a Out[...
10
2009-01-30T18:27:24Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
502,074
<p>Although I don't see much sense in this, here goes:</p> <pre><code>import re import string def traverse(tree): retval = '' for node in tree: if node[0] == 'any': retval += 'x' elif node[0] == 'at': pass elif node[0] in ['min_repeat', 'max_repeat']: ...
15
2009-02-02T02:59:15Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
509,879
<p>While the other answers use the re engine to parse out the elements I have whipped up my own that parses the re and returns a minimal pattern that would match. (Note it doesn't handle [^ads], fancy grouping constructs, start/end of line special characters). I can supply the unit tests if you really like :)</p> <pr...
4
2009-02-04T02:36:11Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
5,006,339
<p>Check out the <a href="http://utilitymill.com/utility/Regex_inverter" rel="nofollow">regex inverter at UtilityMill</a>. (Source code is viewable, based on <a href="http://pyparsing.wikispaces.com/file/view/invRegex.py" rel="nofollow">this example</a> from the pyparsing wiki.)</p>
4
2011-02-15T16:25:00Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
9,118,086
<p>Somebody else had a similar (duplicate?) question <a href="http://stackoverflow.com/questions/4627464/generate-a-string-that-matches-a-regex-in-python">here</a>, and I'd like to offer a little helper library for <a href="https://bitbucket.org/leapfrogdevelopment/rstr/">generating random strings with Python</a> that ...
16
2012-02-02T18:45:41Z
[ "python", "regex" ]
Reversing a regular expression in Python
492,716
<p>I want to reverse a regular expression. I.e. given a regular expression, I want to produce <em>any</em> string that will match that regex.</p> <p>I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do ...
33
2009-01-29T18:05:22Z
17,595,038
<p>I have recently done automatic regex reversal in C++: <a href="http://www.benhanson.net/lexertl/blog.html#rev_regex1" rel="nofollow">http://www.benhanson.net/lexertl/blog.html#rev_regex1</a></p> <p>To get a Python version it would be quite easy to write a code generator to output Python code for lexertl. I'm not th...
1
2013-07-11T13:43:06Z
[ "python", "regex" ]
python: restarting a loop
492,860
<p>i have:</p> <pre><code>for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop </code></pre> <p>But that doesn't seem to work. Is there a way to restart that loop? </p> <p>Thanks</p>
9
2009-01-29T18:37:51Z
492,864
<p>Changing the index variable <code>i</code> from within the loop is unlikely to do what you expect. You may need to use a <code>while</code> loop instead, and control the incrementing of the loop variable yourself. Each time around the <code>for</code> loop, <code>i</code> is reassigned with the next value from <code...
9
2009-01-29T18:38:57Z
[ "python", "loops" ]
python: restarting a loop
492,860
<p>i have:</p> <pre><code>for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop </code></pre> <p>But that doesn't seem to work. Is there a way to restart that loop? </p> <p>Thanks</p>
9
2009-01-29T18:37:51Z
492,877
<p>You may want to consider using a different type of loop where that logic is applicable, because it is the most obvious answer.</p> <p>perhaps a:</p> <pre><code>i=2 while i &lt; n: if something: do something i += 1 else: do something else i = 2 #restart the loop </code></pre>
17
2009-01-29T18:42:22Z
[ "python", "loops" ]
python: restarting a loop
492,860
<p>i have:</p> <pre><code>for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop </code></pre> <p>But that doesn't seem to work. Is there a way to restart that loop? </p> <p>Thanks</p>
9
2009-01-29T18:37:51Z
21,047,506
<p>Just wanted to post an alternative which might be more genearally usable. Most of the existing solutions use a loop index to avoid this. But you don't have to use an index - the key here is that unlike a for loop, where the loop variable is hidden, the loop variable is exposed. </p> <p>You can do very similar thing...
0
2014-01-10T15:00:48Z
[ "python", "loops" ]
python: restarting a loop
492,860
<p>i have:</p> <pre><code>for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop </code></pre> <p>But that doesn't seem to work. Is there a way to restart that loop? </p> <p>Thanks</p>
9
2009-01-29T18:37:51Z
21,047,978
<p>Here is an example using a generator's <a href="http://docs.python.org/2/reference/expressions.html#generator.send" rel="nofollow"><code>send()</code> method</a>:</p> <pre><code>def restartable(seq): while True: for item in seq: restart = yield item if restart: br...
2
2014-01-10T15:22:05Z
[ "python", "loops" ]
python: restarting a loop
492,860
<p>i have:</p> <pre><code>for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop </code></pre> <p>But that doesn't seem to work. Is there a way to restart that loop? </p> <p>Thanks</p>
9
2009-01-29T18:37:51Z
35,438,847
<pre><code>a = ['1', '2', '3'] ls = [] count = False while ls != a : print(a[count]) if a[count] != a[-1] : count = count + 1 else : count = False </code></pre> <p>Restart while loop.</p>
0
2016-02-16T17:12:35Z
[ "python", "loops" ]
I don't understand slicing with negative bounds in Python. How is this supposed to work?
493,046
<p>I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:</p> <pre><code>&gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa' </code></pre> <p>Why does slicing with no beginning bound and a <code>'-1'</code> return every e...
6
2009-01-29T19:31:48Z
493,057
<p>Yes, calling <code>s[0:-1]</code> is exactly the same as calling <code>s[:-1]</code>.</p> <p>Using a negative number as an index in python returns the nth element from the right-hand side of the list (as opposed to the usual left-hand side).</p> <p>so if you have a list as so:</p> <pre><code>myList = ['a', 'b', '...
18
2009-01-29T19:34:38Z
[ "python", "slice" ]
I don't understand slicing with negative bounds in Python. How is this supposed to work?
493,046
<p>I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:</p> <pre><code>&gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa' </code></pre> <p>Why does slicing with no beginning bound and a <code>'-1'</code> return every e...
6
2009-01-29T19:31:48Z
493,061
<p>Yes, calling <code>s[0:-1]</code> is logically the same thing as <code>s[:-1]</code> since slicing is best defined as:</p> <pre><code>[beginning_index:ending_index] </code></pre> <p>Python allows you to omit 0 as this allows your code to more terse.</p>
1
2009-01-29T19:35:38Z
[ "python", "slice" ]
I don't understand slicing with negative bounds in Python. How is this supposed to work?
493,046
<p>I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:</p> <pre><code>&gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa' </code></pre> <p>Why does slicing with no beginning bound and a <code>'-1'</code> return every e...
6
2009-01-29T19:31:48Z
493,079
<p>Negative indices are counted from the end, so s[:-1] is equivalent to s[:len(s)-1] and s[-1] is the last element, for example.</p>
2
2009-01-29T19:40:49Z
[ "python", "slice" ]
I don't understand slicing with negative bounds in Python. How is this supposed to work?
493,046
<p>I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:</p> <pre><code>&gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa' </code></pre> <p>Why does slicing with no beginning bound and a <code>'-1'</code> return every e...
6
2009-01-29T19:31:48Z
493,083
<p>The crucial point is that python indices should be thought of as pointers to the spaces between the entries in a list, rather than to the elements themselves. Hence, 0 points to the beginning, 1 to between the first and second, ... and n to between the nth and (n+1)st.</p> <p>Thus l[1:2] gives you a list containing...
4
2009-01-29T19:42:35Z
[ "python", "slice" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
493,250
<p>Here's the trivial case approach:</p> <pre><code>&gt;&gt;&gt; number = {'one':1, ... 'two':2, ... 'three':3,} &gt;&gt;&gt; &gt;&gt;&gt; number['two'] 2 </code></pre> <p>Or are you looking for something that can handle <em>"twelve thousand, one hundred seventy-two"</em>?</p>
4
2009-01-29T20:25:24Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
493,264
<p>This could be easily be hardcoded into a dictionary if there's a limited amount of numbers you'd like to parse. </p> <p>For slightly more complex cases, you'll probably want to generate this dictionary automatically, based on the relatively simple numbers grammar. Something along the lines of this (of course, gener...
3
2009-01-29T20:28:09Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
493,652
<p>Some earlier posts might be helpful, even though they deal more with turning text to numbers. <a href="http://stackoverflow.com/questions/468241/python-convert-alphabetically-spelled-out-numbers-to-numerics">http://stackoverflow.com/questions/468241/python-convert-alphabetically-spelled-out-numbers-to-numerics</a><...
5
2009-01-29T22:02:35Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
493,788
<p>The majority of this code is to set up the numwords dict, which is only done on the first call.</p> <pre><code>def text2int(textnum, numwords={}): if not numwords: units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen...
56
2009-01-29T22:32:54Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
598,322
<p>Thanks for the code snippet... saved me a lot of time! </p> <p>I needed to handle a couple extra parsing cases, such as ordinal words ("first", "second"), hyphenated words ("one-hundred"), and hyphenated ordinal words like ("fifty-seventh"), so I added a couple lines:</p> <pre><code>def text2int(textnum, numwords=...
6
2009-02-28T17:10:08Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
2,685,607
<p>Made change so that text2int(scale) will return correct conversion. Eg, text2int("hundred") => 100. </p> <pre><code>import re numwords = {} def text2int(textnum): if not numwords: units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleve...
1
2010-04-21T18:37:04Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
16,171,816
<p>This is the c# implementation of the code in 1st answer:</p> <pre><code>public static double ConvertTextToNumber(string text) { string[] units = new string[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fift...
3
2013-04-23T14:21:10Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
21,669,075
<p>A quick solution is to use the <a href="https://pypi.python.org/pypi/inflect" rel="nofollow">inflect.py</a> to generate a dictionary for translation. </p> <p>inflect.py has a <code>number_to_words()</code> function, that will turn a number (e.g. <code>2</code>) to it's word form (e.g. <code>'two'</code>). Unfortun...
0
2014-02-10T04:27:40Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
28,887,097
<p>There's a <a href="https://github.com/dimidd/numbers_in_words" rel="nofollow">ruby gem</a> by Marc Burns that does it. I recently forked it to add support for years. You can call <a href="http://www.decalage.info/python/ruby_bridge" rel="nofollow">ruby code from python</a>.</p> <pre><code> require 'numbers_in_word...
1
2015-03-05T20:27:39Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
34,569,447
<p>I have just released a python module to PyPI called <a href="https://pypi.python.org/pypi/word2number" rel="nofollow">word2number</a> for the exact purpose. <a href="https://github.com/akshaynagpal/w2n" rel="nofollow">https://github.com/akshaynagpal/w2n</a></p> <p>Install it using: </p> <pre><code>pip install word...
-1
2016-01-02T18:48:01Z
[ "python", "string", "text", "integer", "numbers" ]
Is there a way to convert number words to Integers?
493,174
<p>I need to convert <code>one</code> into <code>1</code>, <code>two</code> into <code>2</code> and so on.</p> <p>Is there a way to do this with a library or a class or anything?</p>
24
2009-01-29T20:07:43Z
38,760,564
<p>If anyone is interested, I hacked up a version that maintains the rest of the string (though it may have bugs, haven't tested it too much).</p> <pre><code>def text2int (textnum, numwords={}): if not numwords: units = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", ...
0
2016-08-04T07:04:13Z
[ "python", "string", "text", "integer", "numbers" ]
In Django, how do you retrieve data from extra fields on many-to-many relationships without an explicit query for it?
493,304
<p>Given a situation in Django 1.0 where you have <a href="http://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany">extra data on a Many-to-Many relationship</a>:</p> <pre><code>class Player(models.Model): name = models.CharField(max_length=80) class Team(models.Model): name = models.CharFi...
15
2009-01-29T20:38:21Z
493,454
<p>So, 15 minutes after asking the question, and I found my own answer. </p> <p>Using <code>dir(Team)</code>, I can see another generated attribute named <code>teamplayer_set</code> (it also exists on Player). </p> <pre><code>t = Team.objects.get(pk=168) for x in t.teamplayer_set.all(): if x.captain: print "%s...
9
2009-01-29T21:13:53Z
[ "python", "django", "manytomanyfield" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
493,423
<p>You can do this using <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehensions</a> and <a href="http://docs.python.org/library/functions.html">min()</a> (Python 3.0 code):</p> <pre><code>&gt;&gt;&gt; nums = [1,2,3,4,5] &gt;&gt;&gt; [(x,y) for x in nums for y in nums] [(1...
40
2009-01-29T21:07:47Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
493,429
<p>Some readable python:</p> <pre><code>def JoeCalimar(l): masterList = [] for i in l: for j in l: masterList.append(1.*i/j) pos = masterList.index(min(masterList)) a = pos/len(masterList) b = pos%len(masterList) return (l[a],l[b]) </code></pre> <p>Let me know if something ...
3
2009-01-29T21:08:51Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
493,473
<p>If I'm correct in thinking that you want to find the minimum value of a function for all possible pairs of 2 elements from a list...</p> <pre><code>l = [1,2,3,4,5] def f(i,j): return i+j # Prints min value of f(i,j) along with i and j print min( (f(i,j),i,j) for i in l for j in l) </code></pre>
9
2009-01-29T21:17:20Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
493,898
<p>Doing it the mathy way...</p> <pre><code>nums = [1, 2, 3, 4, 5] min_combo = (min(nums), max(nums)) </code></pre> <p>Unless, of course, you have negatives in there. In that case, this won't work because you actually want the min and max absolute values - the numerator should be close to zero, and the denominator f...
2
2009-01-29T23:07:14Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
494,247
<p>If you don't mind importing the numpy package, it has a lot of convenient functionality built in. It's likely to be much more efficient to use their data structures than lists of lists, etc.</p> <pre><code>from __future__ import division import numpy data = numpy.asarray([1,2,3,4,5]) dists = data.reshape((1,5)) ...
3
2009-01-30T01:59:58Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
496,563
<p>If working with Python ≥2.6 (including 3.x), you can:</p> <pre><code>from __future__ import division import operator, itertools def getmin(alist): return min( (operator.div(*pair), pair) for pair in itertools.product(alist, repeat=2) )[1] getmin([1, 2, 3, 4, 5]) </code></pre> <p>EDIT: N...
1
2009-01-30T18:14:40Z
[ "python", "algorithm", "list", "list-comprehension" ]
Python: For each list element apply a function across the list
493,367
<p>Given <code>[1,2,3,4,5]</code>, how can I do something like </p> <pre><code>1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5 </code></pre> <p>I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I ...
23
2009-01-29T20:53:15Z
1,784,441
<pre><code>&gt;&gt;&gt; nums = [1, 2, 3, 4, 5] &gt;&gt;&gt; min(map((lambda t: ((float(t[0])/t[1]), t)), ((x, y) for x in nums for y in nums)))[1] (1, 5) </code></pre>
0
2009-11-23T16:50:45Z
[ "python", "algorithm", "list", "list-comprehension" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
493,399
<h2>General way</h2> <pre><code>import sys sys.stdout.write('.') </code></pre> <p>You may also need to call</p> <pre><code>sys.stdout.flush() </code></pre> <p>to ensure <code>stdout</code> is flushed immediately.</p> <h2>Python 2.6+</h2> <p>From Python 2.6 you can import the <code>print</code> function from Pytho...
1,118
2009-01-29T21:01:43Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
493,422
<p>The new (as of Python 3.0) print function has an optional end parameter that let's you modify the ending character. There's also sep for separator.</p>
14
2009-01-29T21:07:36Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
493,500
<p><em>Note: The title of this question used to be something like "How to printf in python?"</em></p> <p>Since people may come here looking for it based on the title, Python also supports printf-style substitution:</p> <pre><code>&gt;&gt;&gt; strings = [ "one", "two", "three" ] &gt;&gt;&gt; &gt;&gt;&gt; for i in xran...
148
2009-01-29T21:24:58Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
4,348,063
<p>This is not the answer to the question in the title, but it's the answer on how to print on the same line:</p> <pre><code>import sys for i in xrange(0,10): sys.stdout.write(".") sys.stdout.flush() </code></pre>
31
2010-12-03T17:16:00Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
4,907,563
<p>You can do it with <code>end</code> argument of print. In python3 range() returns iterator and xrange() doesn't exist.</p> <pre><code>for i in range(10): print('.', end='') </code></pre>
4
2011-02-05T15:05:41Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
5,071,123
<p>Use the python3-style print function for python2.6+ <em>(will also break any existing keyworded print statements in the same file.)</em></p> <pre><code># for python2 to use the print() function, removing the print keyword from __future__ import print_function for x in xrange(10): print('.', end='') </code></p...
67
2011-02-21T20:50:30Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
11,076,668
<p>i recently had the same problem..</p> <p>i solved it by doing:</p> <pre><code>import sys, os # reopen stdout with "newline=None". # in this mode, # input: accepts any newline character, outputs as '\n' # output: '\n' converts to os.linesep sys.stdout = os.fdopen(sys.stdout.fileno(), "w", newline=None) for i in...
2
2012-06-18T03:39:20Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
11,685,717
<p>It should be as simple as described at this link by Guido Van Rossum:</p> <p>Re: How does one print without a c/r ?</p> <p><a href="http://www.python.org/search/hypermail/python-1992/0115.html">http://www.python.org/search/hypermail/python-1992/0115.html</a></p> <blockquote> <p>Is it possible to print something...
166
2012-07-27T10:09:57Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
17,494,240
<p>You can do the same in python3 as follows :</p> <pre><code>#!usr/bin/python i = 0 while i&lt;10 : print('.',end='') i = i+1 </code></pre> <p>and execute it with <code>python filename.py</code> or <code>python3 filename.py</code></p>
1
2013-07-05T17:37:06Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
24,685,004
<p>You can just add <code>,</code> in the end of <code>print</code> function so it won't print on new line.</p>
13
2014-07-10T19:45:40Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
26,444,106
<pre><code>for i in xrange(0,10): print '\b.' </code></pre> <p>This worked in both 2.7.8 &amp; 2.5.2 (Canopy and OSX terminal, respectively) -- no module imports or time travel required.</p>
0
2014-10-18T20:13:54Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
28,778,131
<p>@lenooh satisfied my query. I discovered this article while searching for 'python suppress newline'. I'm using IDLE3 on Raspberry Pi to develop Python 3.2 for PuTTY. I wanted to create a progress bar on the PuTTY command line. I didn't want the page scrolling away. I wanted a horizontal line to re-assure the user fr...
0
2015-02-28T04:35:09Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
29,696,909
<p>My understanding is that the comma suppressed the space The 3 dots are relics of the interpreter</p> <blockquote> <blockquote> <blockquote> <p>for i in range(0,10): print".\n", ... . . . . . . . . . .</p> </blockquote> </blockquote...
-5
2015-04-17T10:37:33Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
30,881,087
<p>Using functools.partial to create a new function called printf</p> <pre><code>&gt;&gt;&gt; import functools &gt;&gt;&gt; printf = functools.partial(print, end="") &gt;&gt;&gt; printf("Hello world\n") Hello world </code></pre> <p>Easy way to wrap a function with default parameters.</p>
7
2015-06-17T01:55:02Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
31,569,705
<p>python <strong>2.6+</strong>:</p> <pre><code>from __future__ import print_function # needs to be first statement in file print('.', end='') </code></pre> <p>python <strong>3</strong>:</p> <pre><code>print('.', end='') </code></pre> <p>python &lt;= <strong>2.5</strong>:</p> <pre><code>import sys sys.stdout.write...
2
2015-07-22T17:09:59Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
31,590,081
<p>you want to print something in for loop right;but you don't want it print in new line every time.. for example:</p> <pre><code> for i in range (0,5): print "hi" OUTPUT: hi hi hi hi hi </code></pre> <hr> <p>but you want it to print like this: hi hi hi hi hi hi right???? just add a comma af...
4
2015-07-23T14:18:14Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
33,870,664
<p>You can try:</p> <pre><code>import sys import time # Keeps the initial message in buffer. sys.stdout.write("\rfoobar bar black sheep") sys.stdout.flush() # Wait 2 seconds time.sleep(2) # Replace the message with a new one. sys.stdout.write("\r"+'hahahahaaa ') sys.stdout.flush() # Finalize the new messag...
4
2015-11-23T12:03:31Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
33,901,457
<p>Many of these answers seem a little complicated. In Python 3.X you simply do this,</p> <pre><code>print(&lt;expr&gt;, &lt;expr&gt;, ..., &lt;expr&gt;, end=" ") </code></pre> <p>The default value of end is "\n". We are simply changing it to a space or you can also use end="".</p>
1
2015-11-24T18:46:28Z
[ "python", "newline" ]
How to print in Python without newline or space?
493,386
<p>The question is in the title.</p> <p>I'd like to do in <a href="/questions/tagged/python" class="post-tag" title="show questions tagged &#39;python&#39;" rel="tag">python</a> what I do in this example in <a href="/questions/tagged/c" class="post-tag" title="show questions tagged &#39;c&#39;" rel="tag">c</a>:</p> ...
781
2009-01-29T20:58:25Z
38,286,476
<p>In Python 3, printing is a function. When you call</p> <pre><code>print ('hello world') </code></pre> <p>Python translates it to</p> <pre><code>print ('hello world', end = '\n') </code></pre> <p>You can change end to whatever you want.</p> <pre><code>print ('hello world', end = '') print ('hello world', end = '...
2
2016-07-09T21:22:30Z
[ "python", "newline" ]
What's the best way(error proof / foolproof) to parse a file using python with following format?
493,484
<pre><code>######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } </code...
3
2009-01-29T21:19:31Z
493,546
<p>Well, the data looks pretty regular. So you could do something like this (untested):</p> <pre><code>class Block(object): def __init__(self, name): self.name = name infile = open(...) # insert filename here current = None blocks = [] for line in infile: if line.lstrip().startswith('#'): c...
4
2009-01-29T21:34:23Z
[ "python", "text-parsing" ]
What's the best way(error proof / foolproof) to parse a file using python with following format?
493,484
<pre><code>######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } </code...
3
2009-01-29T21:19:31Z
493,709
<p>You might look into something like <a href="http://pyparsing.wikispaces.com/" rel="nofollow">pyparsing.</a></p>
2
2009-01-29T22:15:12Z
[ "python", "text-parsing" ]
What's the best way(error proof / foolproof) to parse a file using python with following format?
493,484
<pre><code>######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } </code...
3
2009-01-29T21:19:31Z
493,744
<p>If you do not really mean parsing, but rather text processing and the input data is really that regular, then go with John's solution. If you really need some parsing (like there are some a little more complex rules to the data that you are getting), then depending on the amount of data that you need to parse, I'd g...
3
2009-01-29T22:24:17Z
[ "python", "text-parsing" ]
What's the best way(error proof / foolproof) to parse a file using python with following format?
493,484
<pre><code>######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } </code...
3
2009-01-29T21:19:31Z
1,654,802
<p>The best way would be to use an existing format such as JSON.</p> <p>Here's an example parser for your format:</p> <pre><code>from lepl import (AnyBut, Digit, Drop, Eos, Integer, Letter, NON_GREEDY, Regexp, Space, Separator, Word) # EBNF # name = ( letter | "_" ) , { letter | "_" | digit } ; nam...
6
2009-10-31T16:05:47Z
[ "python", "text-parsing" ]
What's the best way(error proof / foolproof) to parse a file using python with following format?
493,484
<pre><code>######################################## # some comment # other comment ######################################## block1 { value=data some_value=some other kind of data othervalue=032423432 } block2 { value=data some_value=some other kind of data othervalue=032423432 } </code...
3
2009-01-29T21:19:31Z
26,204,082
<p><a href="http://pythonhosted.org/grako/" rel="nofollow">Grako (for grammar compiler)</a> allows to separate the input format specification (grammar) from its interpretation (semantics). Here's grammar for your input format in Grako's variety of <a href="http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form"...
1
2014-10-05T15:23:40Z
[ "python", "text-parsing" ]
Outputting to a text file
493,816
<p>How to print the following code to a .txt file</p> <pre><code>y = '10.1.1.' # /24 network, for x in range(255): x += 1 print y + str(x) # not happy that it's in string, but how to print it into a.txt </code></pre> <p>There's copy paste, but would rather try something more interesting. </p>
2
2009-01-29T22:44:05Z
493,822
<pre><code>f = open('myfile.txt', 'w') for x in range(255): ip = "10.1.1.%s\n" % str(x) f.write(ip) f.close() </code></pre>
6
2009-01-29T22:46:38Z
[ "python", "text" ]
Outputting to a text file
493,816
<p>How to print the following code to a .txt file</p> <pre><code>y = '10.1.1.' # /24 network, for x in range(255): x += 1 print y + str(x) # not happy that it's in string, but how to print it into a.txt </code></pre> <p>There's copy paste, but would rather try something more interesting. </p>
2
2009-01-29T22:44:05Z
493,823
<p>scriptname.py >> output.txt</p>
3
2009-01-29T22:46:56Z
[ "python", "text" ]
Outputting to a text file
493,816
<p>How to print the following code to a .txt file</p> <pre><code>y = '10.1.1.' # /24 network, for x in range(255): x += 1 print y + str(x) # not happy that it's in string, but how to print it into a.txt </code></pre> <p>There's copy paste, but would rather try something more interesting. </p>
2
2009-01-29T22:44:05Z
493,943
<p>What is the <code>x += 1</code> for? It seems to be a workaround for <code>range(255)</code> being 0 based - which gives the sequence 0,1,2...254.</p> <p><code>range(1,256)</code> will better give you what you want.</p> <p>An alternative to other answers:</p> <pre><code>NETWORK = '10.1.1' f = open('outfile.txt', ...
1
2009-01-29T23:26:55Z
[ "python", "text" ]
Outputting to a text file
493,816
<p>How to print the following code to a .txt file</p> <pre><code>y = '10.1.1.' # /24 network, for x in range(255): x += 1 print y + str(x) # not happy that it's in string, but how to print it into a.txt </code></pre> <p>There's copy paste, but would rather try something more interesting. </p>
2
2009-01-29T22:44:05Z
497,857
<p>In Python 3, you can use the print function's keyword argument called file. "a" means "append."</p> <pre><code>f = open("network.txt", "a") for i in range(1, 256): print("10.1.1." + str(i), file=f) f.close() </code></pre>
0
2009-01-31T00:38:51Z
[ "python", "text" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
493,831
<p>Because the <code>join()</code> method is in the string class, instead of the list class?</p> <p>I agree it looks funny.</p> <p>See <a href="http://www.faqs.org/docs/diveintopython/odbchelper_join.html">http://www.faqs.org/docs/diveintopython/odbchelper_join.html</a>:</p> <blockquote> <p><strong>Historical note...
170
2009-01-29T22:48:38Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
493,842
<p>It's because any iterable can be joined, not just lists, but the result and the "joiner" are always strings.</p> <p>E.G:</p> <pre><code>import urllib2 print '\n############\n'.join( urllib2.urlopen('http://data.stackexchange.com/users/7095')) </code></pre>
672
2009-01-29T22:51:18Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
493,843
<p>Primarily because the result of a <code>someString.join()</code> is a string.</p> <p>The sequence (list or tuple or whatever) doesn't appear in the result, just a string. Because the result is a string, it makes sense as a method of a string.</p>
10
2009-01-29T22:51:45Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
493,884
<p>I agree that it's counterintuitive at first, but there's a good reason. Join can't be a method of a list because:</p> <ul> <li>it must work for different iterables too (tuples, generators, etc.) </li> <li>it must have different behavior between different types of strings.</li> </ul> <p>There are actually two join ...
40
2009-01-29T23:03:45Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
494,320
<p>Think of it as the natural orthogonal operation to split.</p> <p>I understand why it is applicable to anything iterable and so can't easily be implemented <em>just</em> on list.</p> <p>For readability, I'd like to see it in the language but I don't think that is actually feasible - if iterability were an interface...
15
2009-01-30T02:43:51Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
12,662,361
<p>This was discussed in the <a href="http://mail.python.org/pipermail/python-dev/1999-June/095366.html">String methods... finally</a> thread in the Python-Dev achive, and was accepted by Guido. This thread began in Jun 1999, and <code>str.join</code> was included in Python 1.6 (which supported Unicode) was released in...
117
2012-09-30T15:21:16Z
[ "python", "string", "list", "join" ]
Python join, why is it string.join(list) instead of list.join(string)?
493,819
<p>This has always confused me. It seems like this would be nicer:</p> <pre><code>my_list = ["Hello", "world"] print my_list.join("-") # Produce: "Hello-world" </code></pre> <p>Than this:</p> <pre><code>my_list = ["Hello", "world"] print "-".join(my_list) # Produce: "Hello-world" </code></pre> <p>Is there a specifi...
919
2009-01-29T22:45:13Z
29,617,379
<blockquote> <h1>Why is it <code>string.join(list)</code> instead of <code>list.join(string)</code>?</h1> </blockquote> <p>This is because <code>join</code> is a "string" method! It creates a string from any iterable. If we stuck the method on lists, what about when we have iterables that aren't lists? </p> <p>What...
14
2015-04-14T00:45:18Z
[ "python", "string", "list", "join" ]
Which Python module is suitable for data manipulation in a list?
493,853
<p>I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}.</p> <p>I need addition, multiplication and logarithm to manipulate my data.</p> <p>I would like to study a module, which is as powerful as Awk -language.</p>
3
2009-01-29T22:54:02Z
494,034
<p>I'm not sure exactly what you're after. You can do a lot with list comprehensions. For example, if you want to turn a list:</p> <pre><code>coords = [(x1, y1, z1), (x2, y2, z2), (x3, y3, z3)] # etc </code></pre> <p>into a tuple <code>(x1+x2+x3, y1+y2+y3, z1+z2+z3)</code>, then you can do:</p> <pre><code>sums = ...
6
2009-01-30T00:10:42Z
[ "python", "module" ]
Which Python module is suitable for data manipulation in a list?
493,853
<p>I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}.</p> <p>I need addition, multiplication and logarithm to manipulate my data.</p> <p>I would like to study a module, which is as powerful as Awk -language.</p>
3
2009-01-29T22:54:02Z
494,078
<p>In Python 3 the <code>reduce</code> function is gone. You can do:</p> <pre><code>def prod(lst): return [x*y*z for x, y, z in list(zip(*lst))] coords = [(2, 4, 8), (3, 6, 5), (7, 5, 2)] print(prod(coords)) &gt;&gt;&gt; [42, 120, 80] </code></pre>
1
2009-01-30T00:38:38Z
[ "python", "module" ]
Which Python module is suitable for data manipulation in a list?
493,853
<p>I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}.</p> <p>I need addition, multiplication and logarithm to manipulate my data.</p> <p>I would like to study a module, which is as powerful as Awk -language.</p>
3
2009-01-29T22:54:02Z
495,215
<p>If you need many array manipulation, then numpy is the best choice in python</p> <pre><code>&gt;&gt;&gt; import numpy &gt;&gt;&gt; data = numpy.array([(2, 4, 8), (3, 6, 5), (7, 5, 2)]) &gt;&gt;&gt; data array([[2, 4, 8], [3, 6, 5], [7, 5, 2]]) &gt;&gt;&gt; data.sum() # product of all elements 42 &gt...
5
2009-01-30T11:28:15Z
[ "python", "module" ]
Which Python module is suitable for data manipulation in a list?
493,853
<p>I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}.</p> <p>I need addition, multiplication and logarithm to manipulate my data.</p> <p>I would like to study a module, which is as powerful as Awk -language.</p>
3
2009-01-29T22:54:02Z
495,247
<p>You don't need a separate library or module to do this. Python has list comprehensions built into the language, which lets you manipulate lists and perform caculations. You could use the numpy module to do the same thing if you want to do lots of scientific calculations, or if you want to do lots of heavy number cru...
1
2009-01-30T11:42:37Z
[ "python", "module" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
493,991
<p>I was going to say closures but found <a href="http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&amp;article_id=61016" rel="nofollow">this thread</a>... (not that I understand how it would work in a "static" language)</p> <p>Related concepts are <a href="http://en.wikipedia.org/wiki/First-cl...
0
2009-01-29T23:51:36Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
493,993
<p>With a dynamic language it's much easier to have a command line interpreter so you can test things on the command line and don't have to worry about a compile step to see if they work.</p>
0
2009-01-29T23:52:10Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,001
<p><a href="http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html" rel="nofollow">Here's Steve Yegge</a> on the subject.</p> <p>Guido van Rossum also linked to that talk in <a href="http://neopythonic.blogspot.com/2008/11/scala.html" rel="nofollow">his take of Scala</a>.</p>
3
2009-01-29T23:54:36Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,009
<p>In dynamic languages you can use values in ways that you know are correct. In a statically typed language you can only use values in ways the compiler knows are correct. You need all of the things you mentioned to regain flexibility that's taken away by the type system (I'm not bashing static type systems, the flexi...
1
2009-01-29T23:59:50Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,055
<p>In theory, there's nothing that dynamic languages can do and static languages can't. Smart people put a lot of work into making <em>very good</em> dynamic languages, leading to a perception at the moment that dynamic languages are ahead while static ones need to catch up.</p> <p>In time, this will swing the other w...
15
2009-01-30T00:23:01Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,062
<p>I actually wrote a blog post on this: <a href="http://jasonmbaker.wordpress.com/2008/12/11/duck-typing-vs-interfaces/" rel="nofollow">linky</a>. But that post basically can be summed up like this:</p> <p>You'd be surprised at how much of a load off your mind it is to not have to name at compile time what type you...
1
2009-01-30T00:27:21Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,117
<blockquote> <p>"I'm curious what the benefits are of dynamic languages even when you have those."</p> </blockquote> <p>Compared to D programming language:</p> <ul> <li><p>Python is a more compact language. It allows you to express as much as D but it uses many fewer different concepts to achieve it -- <em>less...
2
2009-01-30T01:00:21Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,118
<p>I find dynamic languages like Perl and to a lesser extent Python allow me to write quick and dirty scripts for things I need to do. The run cycle is much shorter in dynamic languages and often less code needs to be written then in a statically typed language which increases my productivity. This unfortunately comes ...
0
2009-01-30T01:01:42Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,121
<p>Compiled languages tend to be used when efficiency and type safety are the priorities. Otherwise I can't think of any reason why anyone wouldn't be using ruby :)</p>
-2
2009-01-30T01:01:51Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
494,423
<p>One big advantage of dynamic <em>typing</em> when using objects is that you don't need to use class hierarchies anymore when you want several classes to have the same interface - that's more or less what is called duck typing. Bad inheritance is very difficult to fix afterwards - this makes refactoring often harder ...
1
2009-01-30T03:49:42Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
495,293
<p>The point is that in a dynamic language you can implement the same functionality much quicker than in a statically typed one. Therefore the productivity is typically much higher.</p> <p>Things like templates or polymorphism in principle give you lots of flexibility, but you have to write a large amount of code to m...
1
2009-01-30T12:02:25Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
496,932
<p>Example in Python:</p> <pre><code>def lengths(sequence): try: return sum(len(item) for item in sequence) except TypeError: return "Wolf among the sheep!" &gt;&gt;&gt; lengths(["a", "b", "c", (1, 2, 3)]) 6 &gt;&gt;&gt; lengths( ("1", "2", 3) ) 'Wolf among the sheep!' </code></pre> <p>How lo...
2
2009-01-30T19:45:27Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
Uses for Dynamic Languages
493,973
<p>My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent co...
8
2009-01-29T23:42:47Z
667,275
<p>Take a look at this <a href="http://en.wikipedia.org/wiki/ECMAScript%5Ffor%5FXML" rel="nofollow">e4x</a> example in JavaScript:</p> <pre><code>var sales = &lt;sales vendor="John"&gt; &lt;item type="peas" price="4" quantity="6"/&gt; &lt;item type="carrot" price="3" quantity="10"/&gt; &lt;item type="chips...
0
2009-03-20T17:59:33Z
[ "python", "programming-languages", "language-design", "dynamic-languages", "duck-typing" ]
retrieving current URL from FireFox with python
493,978
<p>I want to know what is the current url of active tab in running firefox instance from python module. Does FireFox have any API for this and does python know to work with it?</p>
4
2009-01-29T23:44:47Z
494,063
<p>If on windows you can use win32com</p> <pre><code>import win32clipboard import win32com.client shell = win32com.client.Dispatch("WScript.Shell") shell.AppActivate('Some Application Title') </code></pre> <p>Then use shell.SendKeys to do a ctrl+l and a ctrl+c</p> <p>Then read the string in the clipboard.</p> <p>It...
1
2009-01-30T00:27:25Z
[ "python", "firefox", "python-extensions" ]
retrieving current URL from FireFox with python
493,978
<p>I want to know what is the current url of active tab in running firefox instance from python module. Does FireFox have any API for this and does python know to work with it?</p>
4
2009-01-29T23:44:47Z
494,070
<p>I think i found <a href="https://developer.mozilla.org/en/PyXPCOM" rel="nofollow">something</a> that can help me.</p>
-1
2009-01-30T00:32:52Z
[ "python", "firefox", "python-extensions" ]
retrieving current URL from FireFox with python
493,978
<p>I want to know what is the current url of active tab in running firefox instance from python module. Does FireFox have any API for this and does python know to work with it?</p>
4
2009-01-29T23:44:47Z
494,287
<p>There is also a <a href="http://docs.python.org/library/webbrowser.html" rel="nofollow">webbrowser</a> module in python, but it seems to only allow you to open new windows/tabs.</p>
0
2009-01-30T02:22:56Z
[ "python", "firefox", "python-extensions" ]
retrieving current URL from FireFox with python
493,978
<p>I want to know what is the current url of active tab in running firefox instance from python module. Does FireFox have any API for this and does python know to work with it?</p>
4
2009-01-29T23:44:47Z
1,042,075
<p>The most convenient way maybe insatll a firefox extension to open up a tcp service, then you can exchange info with firefox.</p> <p><a href="http://wiki.github.com/bard/mozrepl" rel="nofollow">mozrepl</a> can set up a telnet service, you can call js-like command to get info.</p> <p>With telnetscript (http: //code....
2
2009-06-25T03:46:56Z
[ "python", "firefox", "python-extensions" ]
CDN options for image resizing
493,981
<p>Background:</p> <p>I working on an application on Google App Engine. Its been going really well until I hit one of their limitations in file size -- 1MB. One of the components of my application resizes images, which have been uploaded by users. The files are directly uploaded to S3 (<a href="http://developer.amazon...
2
2009-01-29T23:45:54Z
627,748
<p>Looks like I have found a service that provides what I am indeed looking for. <a href="http://developer.nirvanix.com/files/folders/python/entry1253.aspx" rel="nofollow">Nirvanix</a> provides an image resize API and even has a nice library for Google App Engine to use with their API. Just thought I would share my fin...
3
2009-03-09T19:52:56Z
[ "python", "google-app-engine", "rest", "image-manipulation", "cdn" ]
CDN options for image resizing
493,981
<p>Background:</p> <p>I working on an application on Google App Engine. Its been going really well until I hit one of their limitations in file size -- 1MB. One of the components of my application resizes images, which have been uploaded by users. The files are directly uploaded to S3 (<a href="http://developer.amazon...
2
2009-01-29T23:45:54Z
1,807,373
<p>SteadyOffload does it as well.</p>
3
2009-11-27T08:15:48Z
[ "python", "google-app-engine", "rest", "image-manipulation", "cdn" ]
How can I disable quoting in the Python 2.4 CSV reader?
494,054
<p>I am writing a Python utility that needs to parse a large, regularly-updated CSV file I don't control. The utility must run on a server with only Python 2.4 available. The CSV file does not quote field values at all, but the <a href="http://www.python.org/doc/2.4.3/lib/csv-fmt-params.html#csv-fmt-params" rel="nofo...
10
2009-01-30T00:22:40Z
494,126
<p>I don't know if python would like/allow it but could you use a non-printable ascii code such as BEL or BS (backspace) These I would think to be extremely rare.</p>
12
2009-01-30T01:05:04Z
[ "python", "csv" ]
How can I disable quoting in the Python 2.4 CSV reader?
494,054
<p>I am writing a Python utility that needs to parse a large, regularly-updated CSV file I don't control. The utility must run on a server with only Python 2.4 available. The CSV file does not quote field values at all, but the <a href="http://www.python.org/doc/2.4.3/lib/csv-fmt-params.html#csv-fmt-params" rel="nofo...
10
2009-01-30T00:22:40Z
494,177
<p>I tried a few examples using Python 2.4.3, and it seemed to be smart enough to detect that the fields were unquoted. </p> <p>I know you've already accepted a (slightly hacky) answer, but have you tried just leaving the <code>reader.dialect.quotechar</code> value alone? What happens if you do?</p> <p>Any chance w...
3
2009-01-30T01:29:25Z
[ "python", "csv" ]
How can I disable quoting in the Python 2.4 CSV reader?
494,054
<p>I am writing a Python utility that needs to parse a large, regularly-updated CSV file I don't control. The utility must run on a server with only Python 2.4 available. The CSV file does not quote field values at all, but the <a href="http://www.python.org/doc/2.4.3/lib/csv-fmt-params.html#csv-fmt-params" rel="nofo...
10
2009-01-30T00:22:40Z
496,952
<p>+1 for Triptych</p> <p>Confirmation that csv.reader automatically handles csv files with out quotes: </p> <pre><code>&gt;&gt;&gt; import StringIO &gt;&gt;&gt; import csv &gt;&gt;&gt; data=""" ... 1,2,3,4,5 ... 1,2,3,4,5 ... 1,2,3,4,5 ... """ &gt;&gt;&gt; reader=csv.reader(StringIO.StringIO(data)) &gt;&gt;&gt; for ...
0
2009-01-30T19:48:51Z
[ "python", "csv" ]
How to write the Fibonacci Sequence in Python
494,594
<p>I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1 &amp; 20), I have written for the program to display all Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 displays = First 20...
68
2009-01-30T05:49:13Z
494,601
<p>Calculate the Fibonacci sequence from the beginning (presumably using a while loop), but don't output anything until the values are >= startNumber, and exit as soon as the values are greater than endNumber.</p> <p>Actual code isn't provided because this smells like homework, but it sounds from your wording that bou...
0
2009-01-30T05:52:54Z
[ "python", "fibonacci", "sequences" ]
How to write the Fibonacci Sequence in Python
494,594
<p>I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1 &amp; 20), I have written for the program to display all Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 displays = First 20...
68
2009-01-30T05:49:13Z
494,612
<p>Start from reading good Python book. One of such book is "How to Think Like a Computer Scientist", especially chapter 5 in your case.</p>
0
2009-01-30T05:59:36Z
[ "python", "fibonacci", "sequences" ]
How to write the Fibonacci Sequence in Python
494,594
<p>I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1 &amp; 20), I have written for the program to display all Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 displays = First 20...
68
2009-01-30T05:49:13Z
494,636
<p>The idea behind the Fibonacci sequence is shown in the following Python code:</p> <pre><code>def fib(n): if n == 1: return 1 elif n == 0: return 0 else: return fib(n-1) + fib(n-2) </code></pre> <p>This means that fib is a function that can do one of t...
16
2009-01-30T06:15:45Z
[ "python", "fibonacci", "sequences" ]