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 |
|---|---|---|---|---|---|---|---|---|---|
Can I write Python web application for Windows and Linux platforms at the same time? | 662,762 | <p>Can I write web application that I can host on Windows(IIS web server) and Linux (Apache or lighttpd) without any changes?</p>
<p>CGI? Maybe something new? WSGI | FastCGI ? </p>
| 3 | 2009-03-19T15:52:10Z | 663,372 | <p>Writing python web apps is a topic on itself, but I would say that by default, it will be portable on multiple servers / platforms. </p>
<p>When developping python web applications, you will often use frameworks that provide their own web server. For performance reasons, you might want to place it behind apache, bu... | 0 | 2009-03-19T18:09:19Z | [
"python",
"cgi",
"fastcgi",
"wsgi"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,175 | <pre><code>>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'
</code></pre>
<p>Python calls this concept "slicing" and it works on more than just strings. Take a look <a href="http://stackover... | 1,557 | 2009-03-19T17:30:44Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,177 | <p>Yes there is. Your example is very close:</p>
<pre><code>myString[2:]
</code></pre>
| 13 | 2009-03-19T17:30:50Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,178 | <p><code>myString[2:]</code> .. leave off the second index to go to the end</p>
| 5 | 2009-03-19T17:30:56Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,182 | <p><code>mystring[2:]</code> </p>
| 4 | 2009-03-19T17:31:15Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,185 | <p>You've got it right there except for "end". Its called slice notation. Your example should read.</p>
<pre><code>new_sub_string = myString[2:]
</code></pre>
<p>If you leave out the second param it is implicitly the end of the string.</p>
| 5 | 2009-03-19T17:31:34Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 663,333 | <p>One example seems to be missing here: full (shallow) copy.</p>
<pre><code>>>> x = "Hello World!"
>>> x
'Hello World!'
>>> x[:]
'Hello World!'
>>> x==x[:]
True
>>>
</code></pre>
<p>This is a common idiom for creating a copy of sequence types (not of interned strings). <c... | 20 | 2009-03-19T18:02:38Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 9,528,361 | <p>A common way to achieve this is by String slicing. <code>MyString[a:b]</code> gives you a substring from index a to (b - 1)</p>
| 34 | 2012-03-02T05:19:02Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 9,780,082 | <p>Just for completeness as nobody else has mentioned it. The third parameter to an array slice is a step. So reversing a string is as simple as:</p>
<pre><code>some_string[::-1]
</code></pre>
<p>Or selecting alternate characters would be:</p>
<pre><code>"H-e-l-l-o- -W-o-r-l-d"[::2] # outputs "Hello World"
</code>... | 196 | 2012-03-20T00:58:50Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 11,698,785 | <p>here is some method to do sub string.using slicing and dicing.</p>
<pre><code>>>> a = "Hello World"
>>> a[1:]
'ello World'
>>> a[2:]
'llo World'
>>> a[:1]
'H'
>>> a[:2]
'He'
>>> a[-1:]
'd'
>>> a[-2:]
'ld'
>>> a[:-1]
'Hello Worl'
>>> a[... | 4 | 2012-07-28T06:15:51Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 11,808,384 | <p>Substr() normally (i.e. PHP, Perl) works this way: </p>
<pre><code>s = Substr(s, beginning, LENGTH)
</code></pre>
<p>So the parameters are beginning and LENGTH</p>
<p>But Python's behaviour is different, it expects beginning and one after END (!). <strong>This is difficult to spot by beginners.</strong> So the co... | 49 | 2012-08-04T11:43:03Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 29,121,528 | <p>Maybe I missed it, but I couldn't find a complete answer on this page to the original question(s) because variables are not further discussed here. So I had to go on searching.</p>
<p>Since I'm not yet allowed to comment, let me add my conclusion here. I'm sure I was not the only one interested in it when accessing... | 4 | 2015-03-18T12:01:43Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 39,240,743 | <p>I would like to add two points to the discussion:</p>
<ol>
<li><p>You can use <code>None</code> instead on an empty space to specify "from the start" or "to the end":</p>
<pre><code>'abcde'[2:None] == 'abcde'[2:] == 'cde'
</code></pre>
<p>This is particularly helpful in functions:</p>
<pre><code>def remove_from_... | 0 | 2016-08-31T04:28:09Z | [
"python",
"string"
] |
Is there a way to substring a string in Python? | 663,171 | <p>Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?</p>
<p>Maybe like <code>myString[2:end]</code>?</p>
<p>If leaving the second part means 'till the end', if you leave the first part, does it start from the start?</p>
| 990 | 2009-03-19T17:29:41Z | 39,240,974 | <p>No one mention about using hardcode indexes itself can be a mess. </p>
<p>In order to avoid that, python offers a built-in object <code>slice()</code>. </p>
<pre><code>string = "my company has 1000$ on profit, but I lost 500$ gambling."
</code></pre>
<p>If we want to know how many money I got left. </p>
<p>Norma... | 0 | 2016-08-31T04:50:52Z | [
"python",
"string"
] |
Python: How do you login to a page and view the resulting page in a browser? | 663,490 | <p>I've been googling around for quite some time now and can't seem to get this to work. A lot of my searches have pointed me to finding similar problems but they all seem to be related to cookie grabbing/storing. I think I've set that up properly, but when I try to open the 'hidden' page, it keeps bringing me back to ... | 4 | 2009-03-19T18:40:44Z | 664,126 | <p>First off, when doing cookie-based authentication, you need to have a <a href="http://docs.python.org/library/cookielib.html#cookielib.CookieJar" rel="nofollow"><code>CookieJar</code></a> to store your cookies in, much in the same way that your browser stores its cookies a place where it can find them again.</p>
<p... | 4 | 2009-03-19T21:21:16Z | [
"python",
"login"
] |
Python: How do you login to a page and view the resulting page in a browser? | 663,490 | <p>I've been googling around for quite some time now and can't seem to get this to work. A lot of my searches have pointed me to finding similar problems but they all seem to be related to cookie grabbing/storing. I think I've set that up properly, but when I try to open the 'hidden' page, it keeps bringing me back to ... | 4 | 2009-03-19T18:40:44Z | 683,596 | <p>I've generally used the <a href="http://wwwsearch.sourceforge.net/mechanize/" rel="nofollow">mechanize library</a> to handle stuff like this. That doesn't answer your question about why your existing code isn't working, but it's something else to play with.</p>
| 1 | 2009-03-25T21:41:43Z | [
"python",
"login"
] |
Python: How do you login to a page and view the resulting page in a browser? | 663,490 | <p>I've been googling around for quite some time now and can't seem to get this to work. A lot of my searches have pointed me to finding similar problems but they all seem to be related to cookie grabbing/storing. I think I've set that up properly, but when I try to open the 'hidden' page, it keeps bringing me back to ... | 4 | 2009-03-19T18:40:44Z | 692,163 | <p>The provided code calls:</p>
<pre><code>opener.open('http://example.com', login_data)
</code></pre>
<p>but throws away the response. I would look at this response to see if it says "Bad password" or "I only accept IE" or similar.</p>
| 0 | 2009-03-28T04:02:01Z | [
"python",
"login"
] |
Looking for knowledge base integrated with bug tracker in python | 663,603 | <p>Ok, I am not sure I want to use Request Tracker and RTFM, which is a possible solution.</p>
<p>I'd like to have a knowledge base with my bug tracker/todo list , so that when
I solve a problem, I would have a record of its resolution for myself or others later.</p>
<p>What python based solutions are available?</p>
| 4 | 2009-03-19T19:12:19Z | 663,618 | <p>Try <a href="http://trac.edgewall.org/" rel="nofollow">Trac</a></p>
| 4 | 2009-03-19T19:13:48Z | [
"python",
"knowledge-management"
] |
Looking for knowledge base integrated with bug tracker in python | 663,603 | <p>Ok, I am not sure I want to use Request Tracker and RTFM, which is a possible solution.</p>
<p>I'd like to have a knowledge base with my bug tracker/todo list , so that when
I solve a problem, I would have a record of its resolution for myself or others later.</p>
<p>What python based solutions are available?</p>
| 4 | 2009-03-19T19:12:19Z | 671,744 | <p>A highly flexible issue tracker in Python I would recommend is "Roundup":
<a href="http://roundup.sourceforge.net/" rel="nofollow">http://roundup.sourceforge.net/</a>.</p>
<p>An example of its use can be seen online at <a href="http://bugs.python.org/" rel="nofollow">http://bugs.python.org/</a>.</p>
| 4 | 2009-03-22T22:50:50Z | [
"python",
"knowledge-management"
] |
Looking for knowledge base integrated with bug tracker in python | 663,603 | <p>Ok, I am not sure I want to use Request Tracker and RTFM, which is a possible solution.</p>
<p>I'd like to have a knowledge base with my bug tracker/todo list , so that when
I solve a problem, I would have a record of its resolution for myself or others later.</p>
<p>What python based solutions are available?</p>
| 4 | 2009-03-19T19:12:19Z | 22,592,567 | <p>I do have experience using probably 20-30 different bug trackers, installed or hosted and so far if you are up to deal with a big number of bugs and you want to spend less time coding-the-issues-tracker, to get Atlassian Jira, which is free for open-source projects.</p>
<p>Yes, it's not Python, it is Java, starts s... | 0 | 2014-03-23T15:03:07Z | [
"python",
"knowledge-management"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 664,143 | <pre><code>>>> def f():
... return {'a' : 1, 'b' : 2}
...
>>> def g():
... return dict(a=1, b=2)
...
>>> g()
{'a': 1, 'b': 2}
>>> f()
{'a': 1, 'b': 2}
>>> import dis
>>> dis.dis(f)
2 0 BUILD_MAP 0
3 DUP_TOP ... | 55 | 2009-03-19T21:25:14Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 664,184 | <p>As far as performance goes:</p>
<pre><code>>>> from timeit import timeit
>>> timeit("a = {'a': 1, 'b': 2}")
0.424...
>>> timeit("a = dict(a = 1, b = 2)")
0.889...
</code></pre>
| 26 | 2009-03-19T21:41:00Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 664,416 | <p>Basically, {} is syntax and is handled on a language and bytecode level. dict() is just another builtin with a more flexible initialization syntax. Note that dict() was only added in the middle of 2.x series.</p>
| 6 | 2009-03-19T23:15:01Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 667,437 | <p><em>Update</em>: thanks for the responses. Removed speculation about copy-on-write.</p>
<p>One other difference between <code>{}</code> and <code>dict</code> is that <code>dict</code> always allocates a new dictionary (even if the contents are static) whereas <code>{}</code> doesn't <em>always</em> do so (see <a h... | 3 | 2009-03-20T18:44:30Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 681,358 | <p>@Jacob: There is a difference in how the objects are allocated, but they are not copy-on-write. Python allocates a fixed-size "free list" where it can quickly allocate dictionary objects (until it fills). Dictionaries allocated via the <code>{}</code> syntax (or a C call to <code>PyDict_New</code>) can come from t... | 20 | 2009-03-25T12:28:03Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 5,837,762 | <p>dict() is used when you want to create a dictionary from an iterable, like :</p>
<pre><code>dict( generator which yields (key,value) pairs )
dict( list of (key,value) pairs )
</code></pre>
| 0 | 2011-04-29T21:50:46Z | [
"python",
"initialization",
"instantiation"
] |
What's the difference between dict() and {}? | 664,118 | <p>So let's say I wanna make a dictionary. We'll call it <code>d</code>. But there are multiple ways to initialize a dictionary in Python! For example, I could do this:</p>
<pre><code>d = {'hash': 'bang', 'slash': 'dot'}
</code></pre>
<p>Or I could do this:</p>
<pre><code>d = dict(hash='bang', slash='dot')
</code></... | 39 | 2009-03-19T21:19:58Z | 37,548,506 | <p>In order to create an empty set we should use the keyword set before it
i.e <code>set()</code> this creates an empty set where as in dicts only the flower brackets can create an empty dict</p>
<p>Lets go with an example </p>
<pre><code>print isinstance({},dict)
True
print isinstance({},set)
False
print isinst... | 0 | 2016-05-31T14:30:01Z | [
"python",
"initialization",
"instantiation"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,222 | <p>Will throw a <code>NameError</code> exception:</p>
<pre><code>>>> val
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'val' is not defined
</code></pre>
<p>You can either catch that or use <code>'val' in dir()</code>, i.e.:</p>
<pre><code>try:
val
... | 8 | 2009-03-19T21:54:54Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,228 | <pre><code>try:
print val
except NameError:
print "val wasn't set."
</code></pre>
| 0 | 2009-03-19T21:56:54Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,230 | <p>In python, variables either refer to an object, or they don't exist. If they don't exist, you will get a NameError. Of course, one of the objects they might refer to is <code>None</code>.</p>
<pre><code>try:
val
except NameError:
print "val is not set"
if val is None:
print "val is None"
</code></pre>
| 5 | 2009-03-19T21:57:05Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,234 | <p>To add to <a href="http://stackoverflow.com/questions/664219/uninitialized-value-of-python/664222#664222">phihag's answer</a>: you can use <a href="http://docs.python.org/library/functions.html#dir" rel="nofollow"><code>dir()</code></a> to get a list of all of the variables in the current scope, so if you want to te... | 0 | 2009-03-19T21:59:16Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,256 | <p>A name does not exist unless a value is assigned to it. There is None, which generally represents no usable value, but it is a value in its own right.</p>
| 5 | 2009-03-19T22:06:40Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,257 | <p>In Python, for a variable to exist, something must have been assigned to it. You can think of your variable name as a dictionary key that must have some value associated with it (even if that value is None).</p>
| 1 | 2009-03-19T22:06:50Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,430 | <p><strong>Q: How do I discover if a variable is defined at a point in my code?</strong></p>
<p><strong>A: Read up in the source file until you see a line where that variable is defined.</strong></p>
| 0 | 2009-03-19T23:19:25Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 664,491 | <p>This question leads on to some fun diversions concerning the nature of python objects and it's garbage collector:</p>
<p>It's probably helpful to understand that all variables in python are really pointers, that is they are names in a namespace (implemented as a hash-table) whch point to an address in memory where ... | 1 | 2009-03-19T23:54:06Z | [
"python"
] |
Uninitialized value in Python? | 664,219 | <p>What's the uninitialized value in Python, so I can compare if something is initialized, like:</p>
<pre><code>val
if val == undefined ?
</code></pre>
<p>EDIT: added a pseudo keyword.</p>
<p>EDIT2: I think I didn't make it clear, but say val is already there, but nothing is assigned to it.</p>
<h3>Duplicate: <a h... | 2 | 2009-03-19T21:53:47Z | 665,319 | <p>Usually a value of <code>None</code> is used to mark something as "declared but not yet initialized; I would consider an uninitialized variable a defekt in the code</p>
| 0 | 2009-03-20T08:19:26Z | [
"python"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,297 | <p>Why not just do this:</p>
<pre><code>var = None
</code></pre>
<p>Python is dynamic, so you don't need to declare things; they exist automatically in the first scope where they're assigned. So, all you need is a regular old assignment statement as above.</p>
<p>This is nice, because you'll never end up with an un... | 147 | 2009-03-19T22:23:05Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,299 | <p>I'm not sure what you're trying to do. Python is a very dynamic language; you don't usually need to declare variables until you're actually going to assign to or use them. I think what you want to do is just</p>
<pre><code>foo = None
</code></pre>
<p>which will assign the value <code>None</code> to the variable <c... | 10 | 2009-03-19T22:24:10Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,302 | <p>I usually initialize the variable to something that denotes the type like</p>
<pre><code>var = ""
</code></pre>
<p>or </p>
<pre><code>var = 0
</code></pre>
<p>If it is going to be an object then don't initialize it until you instantiate it:</p>
<pre><code>var = Var()
</code></pre>
| 2 | 2009-03-19T22:24:31Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,352 | <p>I'd heartily recommend that you read <a href="http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables">Other languages have "variables"</a> (I added it as a related link) â in two minutes you'll know that Python has "names", not "variables".</p>
<pre><code>val = None
#... | 37 | 2009-03-19T22:47:39Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,386 | <p>First of all, my response to the question you've originally asked</p>
<p><strong>Q: How do I discover if a variable is defined at a point in my code?</strong></p>
<p><strong>A: Read up in the source file until you see a line where that variable is defined.</strong></p>
<p>But further, you've given a code example ... | 2 | 2009-03-19T22:59:51Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,391 | <p>You look like you're trying to write C in Python. If you want to find something in a sequence, Python has builtin functions to do that, like</p>
<pre><code>value = sequence.index(blarg)
</code></pre>
| 1 | 2009-03-19T23:03:49Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 664,396 | <p>If I'm understanding your example right, you don't need to refer to 'value' in the if statement anyway. You're breaking out of the loop as soon as it could be set to anything.</p>
<pre><code>value = None
for index in sequence:
doSomethingHere
if conditionMet:
value = index
break
</code></pre>
| 2 | 2009-03-19T23:04:45Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 1,661,181 | <p>Well, if you want to check if a variable is defined or not then why not check if its in the locals() or globals() arrays? Your code rewritten:</p>
<pre><code>for index in sequence:
if 'value' not in globals() and conditionMet:
value = index
break
</code></pre>
<p>If it's a local variable you are l... | 3 | 2009-11-02T13:00:57Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 25,803,365 | <pre><code>var_str = str()
var_int = int()
</code></pre>
| 0 | 2014-09-12T07:51:37Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Is it possible only to declare a variable without assigning any value in Python? | 664,294 | <p>Is it possible to declare a variable in Python, like so?:</p>
<pre><code>var
</code></pre>
<p>so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why?</p>
<p>EDIT: I want to do this for cases like this:</p>
<pre><code>value
for in... | 113 | 2009-03-19T22:21:53Z | 39,994,840 | <p>If <code>None</code> is a valid data value then you can use <code>var = object()</code> as a sentinel like <a href="http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html" rel="nofollow">Nick Coghlan suggests</a>.</p>
| 0 | 2016-10-12T09:13:59Z | [
"python",
"variable-assignment",
"variable-declaration"
] |
Using norwegian letters æøå in python | 664,372 | <p>Hello
I'm learning python and PyGTK now, and have created a simple Music Organizer.
<a href="http://pastebin.com/m2b596852" rel="nofollow">http://pastebin.com/m2b596852</a>
But when it edits songs with the Norwegian letters æ, ø, and å it's just changing them to a weird character.</p>
<p>So is there any good way... | 2 | 2009-03-19T22:54:03Z | 664,404 | <p>You'd need to convert the bytestrings you read from the file into Unicode character strings. Looking at your code, I would do this in the parsing function, i.e. replace <code>stripnulls</code> with something like this</p>
<pre><code>def stripnulls_and_decode(data):
return codecs.utf_8_decode(data.replace("\00",... | 1 | 2009-03-19T23:06:49Z | [
"python",
"utf-8"
] |
Using norwegian letters æøå in python | 664,372 | <p>Hello
I'm learning python and PyGTK now, and have created a simple Music Organizer.
<a href="http://pastebin.com/m2b596852" rel="nofollow">http://pastebin.com/m2b596852</a>
But when it edits songs with the Norwegian letters æ, ø, and å it's just changing them to a weird character.</p>
<p>So is there any good way... | 2 | 2009-03-19T22:54:03Z | 664,456 | <p>I don't know what encodings are used for mp3 tags but if you are sure that it is UTF-8 then:</p>
<pre><code> tagdata[start:end].decode("utf-8")
</code></pre>
<p>The line <code># -*- coding: utf-8 -*-</code> defines your source code encoding and doesn't define encoding used to read from or write to files.</p>
| 1 | 2009-03-19T23:33:04Z | [
"python",
"utf-8"
] |
Using norwegian letters æøå in python | 664,372 | <p>Hello
I'm learning python and PyGTK now, and have created a simple Music Organizer.
<a href="http://pastebin.com/m2b596852" rel="nofollow">http://pastebin.com/m2b596852</a>
But when it edits songs with the Norwegian letters æ, ø, and å it's just changing them to a weird character.</p>
<p>So is there any good way... | 2 | 2009-03-19T22:54:03Z | 664,499 | <p>You want to start by decoding the input FROM the charset it is in TO utf-8 (in Python, encode means "take it from unicode/utf-8 to some other charset"). </p>
<p>Some googling suggests the Norwegian charset is plain-ole 'iso-8859-1'... I hope someone can correct me if I'm wrong on this detail. Regardless, whatever t... | 4 | 2009-03-19T23:57:53Z | [
"python",
"utf-8"
] |
How to recover a broken python "cPickle" dump? | 664,444 | <p>I am using <code>rss2email</code> for converting a number of RSS feeds into mail for easier consumption. That is, I <em>was</em> using it because it broke in a horrible way today: On every run, it only gives me this backtrace:</p>
<pre><code>Traceback (most recent call last):
File "/usr/share/rss2email/rss2email.... | 2 | 2009-03-19T23:25:03Z | 664,577 | <p>Have you tried manually loading the feeds.dat file using both cPickle and pickle? If the output differs it might hint at the error.</p>
<p>Something like (from your home directory):</p>
<pre><code>import cPickle, pickle
f = open('.rss2email/feeds.dat', 'r')
obj1 = cPickle.load(f)
obj2 = pickle.load(f)
</code></pre... | 3 | 2009-03-20T00:40:19Z | [
"python",
"rss",
"pickle"
] |
How to recover a broken python "cPickle" dump? | 664,444 | <p>I am using <code>rss2email</code> for converting a number of RSS feeds into mail for easier consumption. That is, I <em>was</em> using it because it broke in a horrible way today: On every run, it only gives me this backtrace:</p>
<pre><code>Traceback (most recent call last):
File "/usr/share/rss2email/rss2email.... | 2 | 2009-03-19T23:25:03Z | 664,591 | <p>Sounds like the internals of cPickle are getting tangled up. This thread (<a href="http://bytes.com/groups/python/565085-cpickle-problems" rel="nofollow">http://bytes.com/groups/python/565085-cpickle-problems</a>) looks like it might have a clue..</p>
| 2 | 2009-03-20T00:46:26Z | [
"python",
"rss",
"pickle"
] |
How to recover a broken python "cPickle" dump? | 664,444 | <p>I am using <code>rss2email</code> for converting a number of RSS feeds into mail for easier consumption. That is, I <em>was</em> using it because it broke in a horrible way today: On every run, it only gives me this backtrace:</p>
<pre><code>Traceback (most recent call last):
File "/usr/share/rss2email/rss2email.... | 2 | 2009-03-19T23:25:03Z | 664,624 | <ol>
<li><code>'sxOYAAuyzSx0WqN3BVPjE+6pgPU'</code> is most probably unrelated to the pickle's problem</li>
<li><p>Post an error traceback for (to determine what class defines the attribute that can't be called (the one that leads to the TypeError):</p>
<pre><code>python -c "import pickle; pickle.load(open('feeds.dat'... | 2 | 2009-03-20T01:01:15Z | [
"python",
"rss",
"pickle"
] |
How to recover a broken python "cPickle" dump? | 664,444 | <p>I am using <code>rss2email</code> for converting a number of RSS feeds into mail for easier consumption. That is, I <em>was</em> using it because it broke in a horrible way today: On every run, it only gives me this backtrace:</p>
<pre><code>Traceback (most recent call last):
File "/usr/share/rss2email/rss2email.... | 2 | 2009-03-19T23:25:03Z | 719,464 | <h1>How I solved my problem</h1>
<h2>A Perl port of <code>pickle.py</code></h2>
<p>Following J.F. Sebastian's comment about how simple the the <code>pickle</code>
format is, I went out to port parts of <code>pickle.py</code> to Perl. A couple
of quick regular expressions would have been a faster way to access my
data... | 5 | 2009-04-05T18:49:16Z | [
"python",
"rss",
"pickle"
] |
How to Make an Image Uniform Brightness (using Python/PIL) | 664,760 | <p>I want to take an image of a document that was photographed and make it look like it was scanned. Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document. The desired effect would be to remove any shadows or areas of low light (or at least ma... | 3 | 2009-03-20T02:05:35Z | 664,804 | <p>As a first attempt, try thresholding the image. Dark areas become black, light areas become white. I haven't used PIL, but I imagine there's any easy way to do it.</p>
| 2 | 2009-03-20T02:33:23Z | [
"python",
"image",
"image-processing",
"python-imaging-library",
"brightness"
] |
How to Make an Image Uniform Brightness (using Python/PIL) | 664,760 | <p>I want to take an image of a document that was photographed and make it look like it was scanned. Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document. The desired effect would be to remove any shadows or areas of low light (or at least ma... | 3 | 2009-03-20T02:05:35Z | 664,834 | <p>Try ImageChops.screen(image1, image2) with 2 copies of the image. If that's not satisfactory, try some of the other functions in the ImageChops module.</p>
<p>Also, you may want to convert it to grayscale first: ImageOps.grayscale(image). </p>
| 2 | 2009-03-20T02:54:27Z | [
"python",
"image",
"image-processing",
"python-imaging-library",
"brightness"
] |
How to Make an Image Uniform Brightness (using Python/PIL) | 664,760 | <p>I want to take an image of a document that was photographed and make it look like it was scanned. Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document. The desired effect would be to remove any shadows or areas of low light (or at least ma... | 3 | 2009-03-20T02:05:35Z | 676,764 | <p>First try it manually in a image editing program, like GIMP. I think what you're looking for is adjusting brightness and contrast.</p>
| 0 | 2009-03-24T10:12:01Z | [
"python",
"image",
"image-processing",
"python-imaging-library",
"brightness"
] |
How to Make an Image Uniform Brightness (using Python/PIL) | 664,760 | <p>I want to take an image of a document that was photographed and make it look like it was scanned. Since a scanner will put a constant light source over the whole document, I want to achieve that effect on a photo of a document. The desired effect would be to remove any shadows or areas of low light (or at least ma... | 3 | 2009-03-20T02:05:35Z | 850,972 | <p>What type of image? If it's supposed to be ideally pure black and white, as with text pages, then your raw data probably is something like a grayscale gradient with varying levels of not-quite-black letters. Thresholding against a constant may give good results, but not if the illumination is too uneven or lens gla... | 0 | 2009-05-12T02:55:33Z | [
"python",
"image",
"image-processing",
"python-imaging-library",
"brightness"
] |
Hierarchy traversal and comparison modules for Python? | 664,898 | <p>I deal with a lot of hierarchies in my day to day development. File systems, nested DAG nodes in Autodesk Maya, etc.</p>
<p>I'm wondering, are there any good modules for Python specifically designed to traverse and compare hierarchies of objects?</p>
<p>Of particular interest would be ways to do 'fuzzy' compariso... | 5 | 2009-03-20T03:37:34Z | 664,925 | <p><a href="http://code.google.com/p/pytree/" rel="nofollow">http://code.google.com/p/pytree/</a></p>
<p>these maybe overkill or not suited at all for what you need:</p>
<p><a href="http://networkx.lanl.gov/" rel="nofollow">http://networkx.lanl.gov/</a></p>
<p><a href="http://www.osl.iu.edu/~dgregor/bgl-python/" rel... | 1 | 2009-03-20T03:53:27Z | [
"python",
"tree",
"module",
"hierarchy",
"traversal"
] |
Hierarchy traversal and comparison modules for Python? | 664,898 | <p>I deal with a lot of hierarchies in my day to day development. File systems, nested DAG nodes in Autodesk Maya, etc.</p>
<p>I'm wondering, are there any good modules for Python specifically designed to traverse and compare hierarchies of objects?</p>
<p>Of particular interest would be ways to do 'fuzzy' compariso... | 5 | 2009-03-20T03:37:34Z | 665,548 | <p>I'm not sure I see the need for a complete module -- hierarchies are a design pattern, and each hierarchy has enough unique features that it's hard to generalize.</p>
<pre><code>class Node( object ):
def __init__( self, myData, children=None )
self.myData= myData
self.children= children if child... | 4 | 2009-03-20T10:00:05Z | [
"python",
"tree",
"module",
"hierarchy",
"traversal"
] |
Hierarchy traversal and comparison modules for Python? | 664,898 | <p>I deal with a lot of hierarchies in my day to day development. File systems, nested DAG nodes in Autodesk Maya, etc.</p>
<p>I'm wondering, are there any good modules for Python specifically designed to traverse and compare hierarchies of objects?</p>
<p>Of particular interest would be ways to do 'fuzzy' compariso... | 5 | 2009-03-20T03:37:34Z | 670,492 | <p>I recommend digging around xmldifff <a href="http://www.logilab.org/859" rel="nofollow">http://www.logilab.org/859</a> and seeing how they compare nodes and handle parallel trees. Or, try writing a [recursive] generator that yields each [significant] node in a tree, say <code>f(t)</code>, then use <code>itertools.i... | 2 | 2009-03-22T03:06:05Z | [
"python",
"tree",
"module",
"hierarchy",
"traversal"
] |
How can I improve this "register" view in Django? | 664,937 | <p>I've got a Django-based site that allows users to register (but requires an admin to approve the account before they can view certain parts of the site). I'm basing it off of <code>django.contrib.auth</code>. I require users to register with an email address from a certain domain name, so I've overridden the <code>U... | 3 | 2009-03-20T03:59:12Z | 665,070 | <p>First response: It looks a heck of a lot better than 95% of the "improve my code" questions.</p>
<p>Is there anything in particular you are dissatisfied with?</p>
| 0 | 2009-03-20T05:17:32Z | [
"python",
"django",
"user-registration"
] |
How can I improve this "register" view in Django? | 664,937 | <p>I've got a Django-based site that allows users to register (but requires an admin to approve the account before they can view certain parts of the site). I'm basing it off of <code>django.contrib.auth</code>. I require users to register with an email address from a certain domain name, so I've overridden the <code>U... | 3 | 2009-03-20T03:59:12Z | 665,153 | <p>You don't even need this code, but I think the style:</p>
<pre><code>pk = None
try: pk = User.objects.filter(username=username)[0].pk
except: pass
</code></pre>
<p>is more naturally written like:</p>
<pre><code>try:
user = User.objects.get(username=username)
except User.DoesNotExist:
user = None
</code></... | 4 | 2009-03-20T06:16:29Z | [
"python",
"django",
"user-registration"
] |
How can I improve this "register" view in Django? | 664,937 | <p>I've got a Django-based site that allows users to register (but requires an admin to approve the account before they can view certain parts of the site). I'm basing it off of <code>django.contrib.auth</code>. I require users to register with an email address from a certain domain name, so I've overridden the <code>U... | 3 | 2009-03-20T03:59:12Z | 665,546 | <p>I personally try to put the short branch of an if-else statement first. Especially if it returns. This to avoid getting large branches where its difficult to see what ends where. Many others do like you have done and put a comment at the else statement. But python doesnt always have an end of block statement - like ... | 3 | 2009-03-20T09:59:39Z | [
"python",
"django",
"user-registration"
] |
box drawing in python | 664,991 | <p>Platform: WinXP SP2, python 2.5.4.3. (activestate distribution)</p>
<p>Has anyone succeded in writing out <a href="http://en.wikipedia.org/wiki/Box%5Fdrawing%5Fcharacters" rel="nofollow">box drawing characters</a> in python?
When I try to run this:</p>
<pre><code>print u'\u2500'
print u'\u2501'
print u'\u2502'
pri... | 4 | 2009-03-20T04:31:03Z | 665,011 | <p>Printing them will print in the default character encoding, which perhaps is not the right encoding for your terminal.</p>
<p>Have you tried transcoding them to utf-8 first?</p>
<pre><code>print u'\u2500'.encode('utf-8')
print u'\u2501'.encode('utf-8')
print u'\u2502'.encode('utf-8')
print u'\u2503'.encode('utf-8'... | 1 | 2009-03-20T04:44:41Z | [
"python",
"windows",
"unicode"
] |
box drawing in python | 664,991 | <p>Platform: WinXP SP2, python 2.5.4.3. (activestate distribution)</p>
<p>Has anyone succeded in writing out <a href="http://en.wikipedia.org/wiki/Box%5Fdrawing%5Fcharacters" rel="nofollow">box drawing characters</a> in python?
When I try to run this:</p>
<pre><code>print u'\u2500'
print u'\u2501'
print u'\u2502'
pri... | 4 | 2009-03-20T04:31:03Z | 665,053 | <p>This varies greatly based on what your terminal supports. If it uses UTF-8, and if Python can detect it, then it works just fine.</p>
<pre><code>>>> print u'\u2500'
â
>>> print u'\u2501'
â
>>> print u'\u2502'
â
>>> print u'\u2503'
â
>>> print u'\u2504'
â
</code... | 2 | 2009-03-20T05:08:09Z | [
"python",
"windows",
"unicode"
] |
box drawing in python | 664,991 | <p>Platform: WinXP SP2, python 2.5.4.3. (activestate distribution)</p>
<p>Has anyone succeded in writing out <a href="http://en.wikipedia.org/wiki/Box%5Fdrawing%5Fcharacters" rel="nofollow">box drawing characters</a> in python?
When I try to run this:</p>
<pre><code>print u'\u2500'
print u'\u2501'
print u'\u2502'
pri... | 4 | 2009-03-20T04:31:03Z | 665,377 | <p>Your problem is not in Python but in cmd.exe. It has to be set to support UTF-8. Unfortunately, it is not very easy to switch windows console (cmd.exe) to UTF-8 "Python-compatible" way.</p>
<p>You can use command (in cmd.exe) to switch to UTF8:</p>
<pre><code>chcp 65001
</code></pre>
<p>but Python (2.5) does not ... | 4 | 2009-03-20T08:51:40Z | [
"python",
"windows",
"unicode"
] |
box drawing in python | 664,991 | <p>Platform: WinXP SP2, python 2.5.4.3. (activestate distribution)</p>
<p>Has anyone succeded in writing out <a href="http://en.wikipedia.org/wiki/Box%5Fdrawing%5Fcharacters" rel="nofollow">box drawing characters</a> in python?
When I try to run this:</p>
<pre><code>print u'\u2500'
print u'\u2501'
print u'\u2502'
pri... | 4 | 2009-03-20T04:31:03Z | 665,407 | <p>Python supports Unicode. It is possible to print these characters. </p>
<p>For example, see <a href="http://stackoverflow.com/questions/637396/default-encoding-for-python-for-stderr/638823#638823">my answer</a> to <a href="http://stackoverflow.com/questions/637396/default-encoding-for-python-for-stderr">"Default en... | 0 | 2009-03-20T09:04:22Z | [
"python",
"windows",
"unicode"
] |
Python: defaultdict became unmarshallable object in 2.6? | 665,061 | <p>Did defaultdict's become not marshal'able as of Python 2.6? The following works under 2.5, fails under 2.6 with "ValueError: unmarshallable object" on OS X 1.5.6, python-2.6.1-macosx2008-12-06.dmg from python.org:</p>
<pre><code>from collections import defaultdict
import marshal
dd = defaultdict(list)
marshal.dump(... | 9 | 2009-03-20T05:12:27Z | 665,283 | <p><a href="http://svn.python.org/view?view=rev&revision=58893">Marshal was deliberately changed to not support subclasses of built-in types</a>. Marshal was never supposed to handle defaultdicts, but happened to since they are a subclass of dict. <a href="http://docs.python.org/library/marshal.html">Marshal is <s... | 11 | 2009-03-20T07:50:49Z | [
"python"
] |
Python: defaultdict became unmarshallable object in 2.6? | 665,061 | <p>Did defaultdict's become not marshal'able as of Python 2.6? The following works under 2.5, fails under 2.6 with "ValueError: unmarshallable object" on OS X 1.5.6, python-2.6.1-macosx2008-12-06.dmg from python.org:</p>
<pre><code>from collections import defaultdict
import marshal
dd = defaultdict(list)
marshal.dump(... | 9 | 2009-03-20T05:12:27Z | 944,344 | <p>wrt performance issues.. encoding a list of ~600000 dicts, each with 4 key/values, one of the values has a list (around 1-3 length) of 2 key/val dicts:</p>
<pre><code>In [27]: timeit(cjson.encode, data)
4.93589496613
In [28]: timeit(cPickle.dumps, data, -1)
141.412974119
In [30]: timeit(marshal.dumps, data, marsh... | 7 | 2009-06-03T12:01:32Z | [
"python"
] |
Python: Running all unit tests inside a package | 665,093 | <p>I'm trying to hack my through an open source python project (namely: jinja2), </p>
<p>When I say "I'm hacking my way through", I mean I don't really know what I'm doing, so I want to run unittests whenever I change something to make sure I'm not breaking something major!</p>
<p>There's a package full of unit tests... | 1 | 2009-03-20T05:39:41Z | 665,095 | <p>It looks like Jinja uses the <a href="http://codespeak.net/py/dist/test.html" rel="nofollow">py.test testing tool</a>. If so you can run all tests by just running <em>py.test</em> from within the tests subdirectory.</p>
| 1 | 2009-03-20T05:43:48Z | [
"python",
"unit-testing",
"jinja2"
] |
Python: Running all unit tests inside a package | 665,093 | <p>I'm trying to hack my through an open source python project (namely: jinja2), </p>
<p>When I say "I'm hacking my way through", I mean I don't really know what I'm doing, so I want to run unittests whenever I change something to make sure I'm not breaking something major!</p>
<p>There's a package full of unit tests... | 1 | 2009-03-20T05:39:41Z | 665,376 | <p>Try to 'walk' through the directories and import all from files like "test_xxxxxx.py", then call unittest.main()</p>
| 0 | 2009-03-20T08:51:11Z | [
"python",
"unit-testing",
"jinja2"
] |
Python: Running all unit tests inside a package | 665,093 | <p>I'm trying to hack my through an open source python project (namely: jinja2), </p>
<p>When I say "I'm hacking my way through", I mean I don't really know what I'm doing, so I want to run unittests whenever I change something to make sure I'm not breaking something major!</p>
<p>There's a package full of unit tests... | 1 | 2009-03-20T05:39:41Z | 666,327 | <p>You could also take a look at <a href="http://somethingaboutorange.com/mrl/projects/nose/" rel="nofollow">nose</a> too. It's supposed to be a py.test evolution.</p>
| 0 | 2009-03-20T14:17:34Z | [
"python",
"unit-testing",
"jinja2"
] |
Python: Running all unit tests inside a package | 665,093 | <p>I'm trying to hack my through an open source python project (namely: jinja2), </p>
<p>When I say "I'm hacking my way through", I mean I don't really know what I'm doing, so I want to run unittests whenever I change something to make sure I'm not breaking something major!</p>
<p>There's a package full of unit tests... | 1 | 2009-03-20T05:39:41Z | 1,936,845 | <p>Watch out for "test.py" in the Jinja2 package! -- Those are not unit tests! That is a set of utility functions for checking attributes, etc. My testing package is assuming that they are unit tests because of the name "test" -- and returning strange messages.</p>
| 0 | 2009-12-20T20:09:33Z | [
"python",
"unit-testing",
"jinja2"
] |
Status of shift and caps lock in Python | 665,502 | <p>I'm writing a TkInter application using Python 2.5 and I need to find out the status of the caps lock and shift keys (either true or false). I've searched all over the net but cant find a solution.</p>
| 1 | 2009-03-20T09:43:04Z | 665,559 | <p><code>Lock</code> and <code>Shift</code> event modifiers:</p>
<p><a href="http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html" rel="nofollow">http://infohost.nmt.edu/tcc/help/pubs/tkinter/event-modifiers.html</a></p>
| 0 | 2009-03-20T10:08:20Z | [
"python",
"tkinter"
] |
Status of shift and caps lock in Python | 665,502 | <p>I'm writing a TkInter application using Python 2.5 and I need to find out the status of the caps lock and shift keys (either true or false). I've searched all over the net but cant find a solution.</p>
| 1 | 2009-03-20T09:43:04Z | 665,571 | <p>I googled and got one..
I am not sure whether it works for you for all keys...</p>
<p><a href="http://www.java2s.com/Code/Python/Event/KeyactionFunctionKeyALtControlShift.htm" rel="nofollow">http://www.java2s.com/Code/Python/Event/KeyactionFunctionKeyALtControlShift.htm</a></p>
| 0 | 2009-03-20T10:11:50Z | [
"python",
"tkinter"
] |
Status of shift and caps lock in Python | 665,502 | <p>I'm writing a TkInter application using Python 2.5 and I need to find out the status of the caps lock and shift keys (either true or false). I've searched all over the net but cant find a solution.</p>
| 1 | 2009-03-20T09:43:04Z | 665,583 | <p>Keyboard events in Tkinter can be tricky.</p>
<p>I suggest you have a look at the following, in order:</p>
<ul>
<li><a href="http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm" rel="nofollow">http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm</a></li>
<li><a href="http://infohost.nmt.edu/tcc/... | 2 | 2009-03-20T10:16:34Z | [
"python",
"tkinter"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 665,561 | <p>Have you looked at <a href="http://code.google.com/p/py-sms/" rel="nofollow">py-sms</a>?</p>
| 2 | 2009-03-20T10:09:01Z | [
"python",
"sms",
"gsm",
"at-command"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 665,579 | <p><a href="http://www.gammu.org/wiki/index.php?title=Gammu:Python-gammu" rel="nofollow">Python-binding</a> for <a href="http://en.wikipedia.org/wiki/Gammu%5F%28software%29" rel="nofollow">Gammu</a> perhaps? </p>
<p>BTW. It's GUI version (<a href="http://en.wikipedia.org/wiki/Wammu" rel="nofollow">Wammu</a>) is writte... | 2 | 2009-03-20T10:15:01Z | [
"python",
"sms",
"gsm",
"at-command"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 665,881 | <p>I have recently written some code for interacting with Huawei 3G USB modems in python.</p>
<p>My initial prototype used pyserial directly, and then my production code used Twisted's Serial support so I can access the modem asynchronously.</p>
<p>I found that by accessing the modem programatically using the serial ... | 2 | 2009-03-20T12:10:32Z | [
"python",
"sms",
"gsm",
"at-command"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 665,994 | <p>Also check <a href="http://mobilehacking.org/index.php/PyKannel" rel="nofollow">PyKannel</a>.</p>
<p>There's also a web service for sending and receiving SMS (non-free, of course), but I don't have URL at hand. :(</p>
| 0 | 2009-03-20T12:48:32Z | [
"python",
"sms",
"gsm",
"at-command"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 1,071,517 | <p>I provided a detailed answer to a similar question <a href="http://stackoverflow.com/questions/716946/how-do-i-enable-sms-notifications-in-my-web-app/1071496#1071496">here</a>. I mention a Python interface to an SMS gateway provided by TextMagic.</p>
| 1 | 2009-07-01T21:38:08Z | [
"python",
"sms",
"gsm",
"at-command"
] |
How do I go about writing a program to send and receive sms using python? | 665,536 | <p>I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!</p>
<p>Are there GSM libraries for python out there?</p>
| 4 | 2009-03-20T09:55:15Z | 12,443,201 | <p>I was at a hackday where we had free access to a few APIs, including <a href="http://www.twilio.com/" rel="nofollow">twillio</a>. It was very easy to use their python library to send and receive SMS, and we built an app around it in about 3 hours (twillio integration was about 15 minutes).</p>
| 0 | 2012-09-15T23:18:47Z | [
"python",
"sms",
"gsm",
"at-command"
] |
Redirect command line results to a tkinter GUI | 665,566 | <p>I have created a program that prints results on command line.
(It is server and it prints log on command line.)</p>
<p>Now, I want to see the same result to GUI .</p>
<p><strong>How can I redirect command line results to GUI?</strong></p>
<p>Please, suggest a trick to easily transform console application to simpl... | 11 | 2009-03-20T10:10:19Z | 665,598 | <p>You could create a script wrapper that runs your command line program as a sub process, then add the output to something like a text widget.</p>
<pre><code>from tkinter import *
import subprocess as sub
p = sub.Popen('./script',stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
root = Tk()
text = Te... | 9 | 2009-03-20T10:23:53Z | [
"python",
"user-interface",
"tkinter"
] |
Redirect command line results to a tkinter GUI | 665,566 | <p>I have created a program that prints results on command line.
(It is server and it prints log on command line.)</p>
<p>Now, I want to see the same result to GUI .</p>
<p><strong>How can I redirect command line results to GUI?</strong></p>
<p>Please, suggest a trick to easily transform console application to simpl... | 11 | 2009-03-20T10:10:19Z | 665,847 | <p>Redirecting stdout to a write() method that updates your gui is one way to go, and probably the quickest - although running a subprocess is probably a more elegant solution. </p>
<p>Only redirect stderr once you're really confident it's up and working, though!</p>
<p>Example implimentation (gui file and test scrip... | 2 | 2009-03-20T12:01:36Z | [
"python",
"user-interface",
"tkinter"
] |
Redirect command line results to a tkinter GUI | 665,566 | <p>I have created a program that prints results on command line.
(It is server and it prints log on command line.)</p>
<p>Now, I want to see the same result to GUI .</p>
<p><strong>How can I redirect command line results to GUI?</strong></p>
<p>Please, suggest a trick to easily transform console application to simpl... | 11 | 2009-03-20T10:10:19Z | 32,682,520 | <p>To display subprocess' output in a GUI <em>while it is still running</em>, a portable stdlib-only solution that works on both Python 2 and 3 has to use a background thread:</p>
<pre><code>#!/usr/bin/python
"""
- read output from a subprocess in a background thread
- show the output in the GUI
"""
import sys
from it... | 2 | 2015-09-20T17:55:36Z | [
"python",
"user-interface",
"tkinter"
] |
Extract array from list in python | 665,652 | <p>If I have a list like this:</p>
<p><code>>>> data = [(1,2),(40,2),(9,80)]</code></p>
<p>how can I extract the the two lists [1,40,9] and [2,2,80] ? Of course I can iterate and extract the numbers myself but I guess there is a better way ?</p>
| 4 | 2009-03-20T10:48:18Z | 665,662 | <p>List comprehensions save the day:</p>
<pre><code>first = [x for (x,y) in data]
second = [y for (x,y) in data]
</code></pre>
| 14 | 2009-03-20T10:50:37Z | [
"python",
"arrays",
"list"
] |
Extract array from list in python | 665,652 | <p>If I have a list like this:</p>
<p><code>>>> data = [(1,2),(40,2),(9,80)]</code></p>
<p>how can I extract the the two lists [1,40,9] and [2,2,80] ? Of course I can iterate and extract the numbers myself but I guess there is a better way ?</p>
| 4 | 2009-03-20T10:48:18Z | 665,684 | <p>The unzip operation is:</p>
<pre><code>In [1]: data = [(1,2),(40,2),(9,80)]
In [2]: zip(*data)
Out[2]: [(1, 40, 9), (2, 2, 80)]
</code></pre>
<p>Edit: You can decompose the resulting list on assignment:</p>
<pre><code>In [3]: first_elements, second_elements = zip(*data)
</code></pre>
<p>And if you really need li... | 26 | 2009-03-20T10:58:18Z | [
"python",
"arrays",
"list"
] |
Extract array from list in python | 665,652 | <p>If I have a list like this:</p>
<p><code>>>> data = [(1,2),(40,2),(9,80)]</code></p>
<p>how can I extract the the two lists [1,40,9] and [2,2,80] ? Of course I can iterate and extract the numbers myself but I guess there is a better way ?</p>
| 4 | 2009-03-20T10:48:18Z | 665,927 | <p>There is also </p>
<pre><code>In [1]: data = [(1,2),(40,2),(9,80)]
In [2]: x=map(None, *data)
Out[2]: [(1, 40, 9), (2, 2, 80)]
In [3]: map(None,*x)
Out[3]: [(1, 2), (40, 2), (9, 80)]
</code></pre>
| 5 | 2009-03-20T12:27:08Z | [
"python",
"arrays",
"list"
] |
Best continuously updated resource about python web "plumbing" | 665,848 | <p>I'm a programmer in Python who works on web-applications. I know a fair bit about the application level. But not so much about the underlying "plumbing" which I find myself having to configure or debug.</p>
<p>I'm thinking of everything from using memcached to flup, fcgi, WSGI etc.</p>
<p>When looking for informat... | 3 | 2009-03-20T12:02:10Z | 665,869 | <p>Buy this. <a href="http://rads.stackoverflow.com/amzn/click/067232699X" rel="nofollow">http://www.amazon.com/Scalable-Internet-Architectures-Developers-Library/dp/067232699X</a></p>
| 1 | 2009-03-20T12:05:30Z | [
"python",
"wsgi",
"flup"
] |
Best continuously updated resource about python web "plumbing" | 665,848 | <p>I'm a programmer in Python who works on web-applications. I know a fair bit about the application level. But not so much about the underlying "plumbing" which I find myself having to configure or debug.</p>
<p>I'm thinking of everything from using memcached to flup, fcgi, WSGI etc.</p>
<p>When looking for informat... | 3 | 2009-03-20T12:02:10Z | 665,951 | <p><em>Zope</em> is a still evolving framework, written in <em>Python</em> and is documented online. For a start, see <a href="http://docs.zope.org/zope2/zope2book/source/ZopeArchitecture.html" rel="nofollow">Zope Concepts and Architecture</a>. Like other <em>Python</em> based web frameworks, the <a href="http://www.zo... | 0 | 2009-03-20T12:33:53Z | [
"python",
"wsgi",
"flup"
] |
Best continuously updated resource about python web "plumbing" | 665,848 | <p>I'm a programmer in Python who works on web-applications. I know a fair bit about the application level. But not so much about the underlying "plumbing" which I find myself having to configure or debug.</p>
<p>I'm thinking of everything from using memcached to flup, fcgi, WSGI etc.</p>
<p>When looking for informat... | 3 | 2009-03-20T12:02:10Z | 665,953 | <ul>
<li>General info about highly efficient web architecture: <a href="http://highscalability.com/" rel="nofollow">http://highscalability.com/</a></li>
<li>Interesting Python related articles: <a href="http://www.onlamp.com/python/" rel="nofollow">http://www.onlamp.com/python/</a> </li>
<li>Printed magazine: <a href="... | 1 | 2009-03-20T12:35:27Z | [
"python",
"wsgi",
"flup"
] |
Best continuously updated resource about python web "plumbing" | 665,848 | <p>I'm a programmer in Python who works on web-applications. I know a fair bit about the application level. But not so much about the underlying "plumbing" which I find myself having to configure or debug.</p>
<p>I'm thinking of everything from using memcached to flup, fcgi, WSGI etc.</p>
<p>When looking for informat... | 3 | 2009-03-20T12:02:10Z | 667,316 | <p>You can use this terminology to limit search results to the past year:</p>
<p><a href="http://www.tech-recipes.com/rx/2860/google_how_to_access_filter_by_date_dropdown_box/" rel="nofollow">http://www.tech-recipes.com/rx/2860/google_how_to_access_filter_by_date_dropdown_box/</a></p>
| 0 | 2009-03-20T18:10:45Z | [
"python",
"wsgi",
"flup"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 665,980 | <p>This should do it nicely.</p>
<pre><code>def frequency_analysis(string):
d = dict()
for key in string:
d[key] = d.get(key, 0) + 1
return d
def letters_in_order_of_frequency(string):
frequencies = frequency_analysis(string)
# frequencies is of bounded size because number of letters is bo... | 4 | 2009-03-20T12:44:10Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 665,991 | <p>Here's a one line answer</p>
<pre><code>sortedLetters = sorted(d.iteritems(), key=lambda (k,v): (v,k))
</code></pre>
| 12 | 2009-03-20T12:47:42Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 665,998 | <p>Here is something that returns a list of tuples rather than a dictionary:</p>
<pre><code>import operator
if __name__ == '__main__':
test_string = 'cnaa'
string_dict = dict()
for letter in test_string:
if letter not in string_dict:
string_dict[letter] = test_string.count(letter)
... | 3 | 2009-03-20T12:49:48Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 666,045 | <p>chills42 lambda function wins, I think but as an alternative, how about generating the dictionary with the counts as the keys instead?</p>
<pre><code>def count_chars(string):
distinct = set(string)
dictionary = {}
for s in distinct:
num = len(string.split(s)) - 1
dictionary[num] = s
... | 0 | 2009-03-20T13:04:36Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 666,048 | <p><strong>EDIT</strong> This will do what you want. I'm stealing <strong>chills42</strong> line and adding another:</p>
<pre><code>sortedLetters = sorted(d.iteritems(), key=lambda (k,v): (v,k))
sortedString = ''.join([c[0] for c in reversed(sortedLetters)])
</code></pre>
<p>------------<em>original answer</em>------... | 2 | 2009-03-20T13:05:26Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 666,123 | <pre><code>from collections import defaultdict
def most_frequent(s):
d = defaultdict(int)
for c in s:
d[c] += 1
return "".join([
k for k, v in sorted(
d.iteritems(), reverse=True, key=lambda (k, v): v)
])
</code></pre>
<p><strong>EDIT:</strong></p>
<p>here is my one liner:</p... | 2 | 2009-03-20T13:25:24Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 666,155 | <pre><code>def reversedSortedFrequency(string)
from collections import defaultdict
d = defaultdict(int)
for c in string:
d[c]+=1
return sorted([(v,k) for k,v in d.items()], key=lambda (k,v): -k)
</code></pre>
| 0 | 2009-03-20T13:31:16Z | [
"python"
] |
How to write a function that takes a string and prints the letters in decreasing order of frequency? | 665,942 | <p>I got this far:</p>
<pre><code>def most_frequent(string):
d = dict()
for key in string:
if key not in d:
d[key] = 1
else:
d[key] += 1
return d
print most_frequent('aabbbc')
</code></pre>
<p>Returning:</p>
<pre><code>{'a': 2, 'c': 1, 'b': 3}
</code></pre>
<p>No... | 4 | 2009-03-20T12:32:19Z | 666,349 | <p>Here is the <strong>fixed version</strong> (thank you for pointing out bugs)</p>
<pre><code>def frequency(s):
return ''.join(
[k for k, v in
sorted(
reduce(
lambda d, c: d.update([[c, d.get(c, 0) + 1]]) or d,
list(s),
dict()).items(),... | 0 | 2009-03-20T14:23:09Z | [
"python"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.