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 |
|---|---|---|---|---|---|---|---|---|---|
Relational/Logic Programming in Python? | 1,917,607 | <p>I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire.</p>
<p>Are there any good libraries for logic programming in Python? I've done some searching on Google but only foun... | 26 | 2009-12-16T20:50:21Z | 9,927,898 | <p>A recent Prolog implementation in Python (or rather RPython) in
<a href="http://www.cs.nmsu.edu/ALP/2011/09/an-alternative-approach-for-implementing-prolog/" rel="nofollow">Pyrolog</a>. It is still rather experimental.</p>
| 2 | 2012-03-29T14:52:19Z | [
"python",
"prolog",
"logic-programming"
] |
Relational/Logic Programming in Python? | 1,917,607 | <p>I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire.</p>
<p>Are there any good libraries for logic programming in Python? I've done some searching on Google but only foun... | 26 | 2009-12-16T20:50:21Z | 9,931,871 | <p>Another option is <a href="http://yieldprolog.sourceforge.net/" rel="nofollow">Yield Prolog</a></p>
| 2 | 2012-03-29T19:10:38Z | [
"python",
"prolog",
"logic-programming"
] |
Relational/Logic Programming in Python? | 1,917,607 | <p>I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire.</p>
<p>Are there any good libraries for logic programming in Python? I've done some searching on Google but only foun... | 26 | 2009-12-16T20:50:21Z | 11,334,962 | <p>You may want to use <a href="https://sites.google.com/site/pydatalog/" rel="nofollow">pyDatalog</a>, a logic programming library that I developed for Python implementing <a href="http://en.wikipedia.org/wiki/Datalog" rel="nofollow">Datalog</a>. It also works with SQLAlchemy to query relational databases using logic... | 9 | 2012-07-04T20:11:51Z | [
"python",
"prolog",
"logic-programming"
] |
Relational/Logic Programming in Python? | 1,917,607 | <p>I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire.</p>
<p>Are there any good libraries for logic programming in Python? I've done some searching on Google but only foun... | 26 | 2009-12-16T20:50:21Z | 13,415,090 | <p>You should also check PyLog:</p>
<p><a href="http://cdsoft.fr/pylog/" rel="nofollow">http://cdsoft.fr/pylog/</a></p>
<p>It has a very clean and simple syntax and implementation.</p>
| 1 | 2012-11-16T10:51:07Z | [
"python",
"prolog",
"logic-programming"
] |
Relational/Logic Programming in Python? | 1,917,607 | <p>I'm a longtime python developer and recently have been introduced to Prolog. I love the concept of using relationship rules for certain kinds of tasks, and would like to add this to my repertoire.</p>
<p>Are there any good libraries for logic programming in Python? I've done some searching on Google but only foun... | 26 | 2009-12-16T20:50:21Z | 16,464,408 | <p><a href="http://github.com/logpy/logpy" rel="nofollow">LogPy</a> is an implementation of <a href="http://kanren.sourceforge.net/" rel="nofollow">miniKanren</a>, a relational programming language, in Python. It follows in th tradition of <a href="https://github.com/clojure/core.logic" rel="nofollow"><code>core.logic... | 1 | 2013-05-09T14:38:10Z | [
"python",
"prolog",
"logic-programming"
] |
Perl within Python? | 1,917,656 | <p>There is a Perl library I would like to access from within Python.
How can I use it?</p>
<p>FYI, the software is <a href="http://webascorpus.sourceforge.net/PHITE.php?sitesig=FILES&page=FILES%5F10%5FSoftware" rel="nofollow">NCleaner</a>. I would like to use it from within Python to transform an HTML string into... | 6 | 2009-12-16T20:55:18Z | 1,917,758 | <p><a href="http://search.cpan.org/~gaas/pyperl-1.0/perlmodule.pod">pyperl</a> provides perl embedding for python, but honestly it's not the way I'd go. I second Roboto's suggestion -- write a script that runs NCleaner (either processing from stdin to stdout, or working on temporary files, whichever one is more appropr... | 11 | 2009-12-16T21:09:16Z | [
"python",
"perl",
"text-mining"
] |
Python joining strings | 1,917,705 | <p>I have run into a problem joining two strings in Python.
I have some code that is like this:</p>
<pre><code>for line in sites:
site = line
for line in files:
url = site+line
</code></pre>
<p>That should be easy I thougth but the strings ends up "looking wierd":</p>
<p><a href="http://exam... | -1 | 2009-12-16T21:02:09Z | 1,917,759 | <p>The simplest thing is to avoid using the same variable in the for statements:</p>
<pre><code>for site in sites:
for line in files:
url = site + line
</code></pre>
<p>Does that clear things up? It is good practice in any case.</p>
| 2 | 2009-12-16T21:09:36Z | [
"python",
"string"
] |
Python joining strings | 1,917,705 | <p>I have run into a problem joining two strings in Python.
I have some code that is like this:</p>
<pre><code>for line in sites:
site = line
for line in files:
url = site+line
</code></pre>
<p>That should be easy I thougth but the strings ends up "looking wierd":</p>
<p><a href="http://exam... | -1 | 2009-12-16T21:02:09Z | 1,917,778 | <p>Perhaps the problem is in using the identifier name 'line' twice?</p>
| 0 | 2009-12-16T21:12:21Z | [
"python",
"string"
] |
Python joining strings | 1,917,705 | <p>I have run into a problem joining two strings in Python.
I have some code that is like this:</p>
<pre><code>for line in sites:
site = line
for line in files:
url = site+line
</code></pre>
<p>That should be easy I thougth but the strings ends up "looking wierd":</p>
<p><a href="http://exam... | -1 | 2009-12-16T21:02:09Z | 1,917,784 | <p>Maybe you have extra whitespace for example a newline at the end of the site</p>
<pre><code>for site in sites:
for line in files:
url = site.strip() + line.strip()
</code></pre>
| 1 | 2009-12-16T21:13:07Z | [
"python",
"string"
] |
When using py2app, is there a way to customize the traceback dialog that gets displayed? (Or show a different Cocoa dialog?) | 1,917,769 | <p>Is there an easy way to get any more control over the py2app traceback dialogs, or just a nice way to display GUI messages?</p>
<p>If I raise an exception in my py2app script, I get a dialog that says something like this:</p>
<blockquote>
<p><strong>MyAppName Error</strong></p>
<p>MyAppName Error</p>
<... | 1 | 2009-12-16T21:11:24Z | 1,918,117 | <p>Instead of using <code>osascript</code>, you can call <code>display dialog</code> via <a href="http://appscript.sourceforge.net/py-appscript/doc/osax-manual/03%5Fexamples.html" rel="nofollow">py-appscript</a> which, if you don't already have it in your python site-library, can be installed via <code>easy_install</co... | 2 | 2009-12-16T22:04:27Z | [
"python",
"osx",
"py2app"
] |
When using py2app, is there a way to customize the traceback dialog that gets displayed? (Or show a different Cocoa dialog?) | 1,917,769 | <p>Is there an easy way to get any more control over the py2app traceback dialogs, or just a nice way to display GUI messages?</p>
<p>If I raise an exception in my py2app script, I get a dialog that says something like this:</p>
<blockquote>
<p><strong>MyAppName Error</strong></p>
<p>MyAppName Error</p>
<... | 1 | 2009-12-16T21:11:24Z | 1,918,863 | <p>Just put a standard Python try/catch block around the section of code that throws the exception, then use NSAlert to tell your users they need to put the DVD in</p>
| 0 | 2009-12-17T01:05:57Z | [
"python",
"osx",
"py2app"
] |
Python import mechanics | 1,917,958 | <p>I have two related Python 'import' questions. They are easily testable, but I want answers that are language-defined and not implementation-specific, and I'm also interested in style/convention, so I'm asking here instead.</p>
<p>1)</p>
<p>If module A imports module B, and module B imports module C, can code in mo... | 10 | 2009-12-16T21:41:59Z | 1,918,211 | <p>The first thing you should know is that the Python language is NOT an ISO standard. This is rather different from C/C++, and it means that there's no "proper" way to define a language behaviour - CPython might do something just because it was coded that way, and Jython might do the other way round.</p>
<p>about you... | 11 | 2009-12-16T22:20:10Z | [
"python",
"coding-style",
"import",
"module",
"conventions"
] |
Python import mechanics | 1,917,958 | <p>I have two related Python 'import' questions. They are easily testable, but I want answers that are language-defined and not implementation-specific, and I'm also interested in style/convention, so I'm asking here instead.</p>
<p>1)</p>
<p>If module A imports module B, and module B imports module C, can code in mo... | 10 | 2009-12-16T21:41:59Z | 1,918,234 | <p>Alan's given a great answer, but I wanted to add that for your question 1 it depends on what you mean by 'imports'.</p>
<p>If you use the <code>from C import x</code> syntax, then <code>x</code> becomes available in the namespace of <code>B</code>. If in <code>A</code> you then do <code>import B</code>, you <em>wil... | 7 | 2009-12-16T22:24:23Z | [
"python",
"coding-style",
"import",
"module",
"conventions"
] |
Making python/tkinter label widget update? | 1,918,005 | <p>I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, but simply retains its original content. As far as I can tell, decrement_widget() is ne... | 13 | 2009-12-16T21:47:52Z | 1,918,054 | <p>You'll want to set the label's <code>textvariable</code> with a <a href="http://epydoc.sourceforge.net/stdlib/Tkinter.StringVar-class.html"><code>StringVar</code></a>; when the <code>StringVar</code> changes (by you calling <code>myStringVar.set("text here")</code>), then the label's text also gets updated. And yes... | 14 | 2009-12-16T21:54:29Z | [
"python",
"tkinter"
] |
Making python/tkinter label widget update? | 1,918,005 | <p>I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, but simply retains its original content. As far as I can tell, decrement_widget() is ne... | 13 | 2009-12-16T21:47:52Z | 1,918,316 | <p>I think you're getting a "referenced before assignment" error because Python thinks <code>remaining</code> is in the local scope.</p>
<p>In Python 3, you can say <code>nonlocal remaining</code>. But in Python 2, I don't believe there's a way to refer to a non-local, non-global scope. This worked for me:</p>
<pre... | 4 | 2009-12-16T22:37:34Z | [
"python",
"tkinter"
] |
Making python/tkinter label widget update? | 1,918,005 | <p>I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, but simply retains its original content. As far as I can tell, decrement_widget() is ne... | 13 | 2009-12-16T21:47:52Z | 34,633,220 | <pre><code> import tkinter
from tkinter import *
# just init some vars
remaining = 0
secs = 0
root = tkinter.Tk()
prompt = StringVar()
def snooze (secs):
"""
Snoozes for the given number of seconds. During the snooze, a progress
dialog is launched notif... | 0 | 2016-01-06T12:30:31Z | [
"python",
"tkinter"
] |
Auth_token error at Facebook | 1,918,047 | <p>i have been on this for the last 2 days with no result.
i am running my facebook app on my localhost with port-forwarding method.
i know my server setup is working fine as i can see the logs on the django runserver and dyndns log as well.</p>
<p>django is properly responding to calls as well.</p>
<p>the problem is... | 0 | 2009-12-16T21:53:59Z | 1,918,123 | <p>When you get that error, presuming you have <code>debug=True</code> in the Django settings and that your application is in development mode in Facebook, you can do View Source and see the entire Django error page that would normally display, including traceback. Facebook comment it out in the HTML so it doesn't show... | 0 | 2009-12-16T22:05:46Z | [
"python",
"django",
"facebook"
] |
Auth_token error at Facebook | 1,918,047 | <p>i have been on this for the last 2 days with no result.
i am running my facebook app on my localhost with port-forwarding method.
i know my server setup is working fine as i can see the logs on the django runserver and dyndns log as well.</p>
<p>django is properly responding to calls as well.</p>
<p>the problem is... | 0 | 2009-12-16T21:53:59Z | 1,918,132 | <p>This often happens with a failed authentication. I'm not sure what the Python client libraries might look like, but with the PHP ones you generally make an authorization call against the library, something like <code>$facebook->require_login()</code>.</p>
<p>With the PHP library, if this call fails to verify th... | 1 | 2009-12-16T22:08:00Z | [
"python",
"django",
"facebook"
] |
Python lists append return value | 1,918,270 | <p>I wanted to create a simple binary tree as followed by this image:</p>
<p><img src="http://i.stack.imgur.com/hpG9u.png" alt="http://imgur.com/QCVSW.png"></p>
<p>basically empty , but the last values so I created the root list :</p>
<pre><code>root = [list(),list()]
</code></pre>
<p>and made a recursive function ... | 3 | 2009-12-16T22:29:35Z | 1,918,296 | <p>To solve your exact question, you can do this:</p>
<pre><code>def list_append(lst, item):
lst.append(item)
return lst
</code></pre>
<p>and then <code>list_append(lst, item)</code> will append <code>item</code> to the <code>lst</code> and then return the <code>lst</code>.</p>
| 13 | 2009-12-16T22:34:28Z | [
"python",
"list"
] |
Python lists append return value | 1,918,270 | <p>I wanted to create a simple binary tree as followed by this image:</p>
<p><img src="http://i.stack.imgur.com/hpG9u.png" alt="http://imgur.com/QCVSW.png"></p>
<p>basically empty , but the last values so I created the root list :</p>
<pre><code>root = [list(),list()]
</code></pre>
<p>and made a recursive function ... | 3 | 2009-12-16T22:29:35Z | 1,918,309 | <p>You can append first and then pass the reference:</p>
<pre><code>nodeT.append([])
TF(nodeT, nodeT, i + 1)
</code></pre>
<p>But your function makes no sense to me. Even if it did make sense, it would cause 2**35 function calls to TF, which would take rather a long time to complete.</p>
| 3 | 2009-12-16T22:36:57Z | [
"python",
"list"
] |
Python lists append return value | 1,918,270 | <p>I wanted to create a simple binary tree as followed by this image:</p>
<p><img src="http://i.stack.imgur.com/hpG9u.png" alt="http://imgur.com/QCVSW.png"></p>
<p>basically empty , but the last values so I created the root list :</p>
<pre><code>root = [list(),list()]
</code></pre>
<p>and made a recursive function ... | 3 | 2009-12-16T22:29:35Z | 1,918,453 | <p>There's a better way to create a binary tree, but I could not understand what you want to do with it.</p>
<p>Here is the code:</p>
<pre><code>>>> def tree(i):
if i == 0:
return ['T', 'F']
return [tree(i-1), tree(i-1)]
>>> tree(3)
[[[['T', 'F'], ['T', 'F']], [['T', 'F'], ['T', 'F']]]... | 1 | 2009-12-16T23:06:02Z | [
"python",
"list"
] |
Python lists append return value | 1,918,270 | <p>I wanted to create a simple binary tree as followed by this image:</p>
<p><img src="http://i.stack.imgur.com/hpG9u.png" alt="http://imgur.com/QCVSW.png"></p>
<p>basically empty , but the last values so I created the root list :</p>
<pre><code>root = [list(),list()]
</code></pre>
<p>and made a recursive function ... | 3 | 2009-12-16T22:29:35Z | 1,918,462 | <p>Dont append to the list, create them. Python has custom data structures btw :P </p>
<pre><code>class BinTree(object):
def __init__(self, left=None, right=None):
self.left = left
self.right = right
def __str__(self):
return " (%s, %s) " % (self.left, self.right)
def maketree( depth )... | 4 | 2009-12-16T23:07:02Z | [
"python",
"list"
] |
Python lists append return value | 1,918,270 | <p>I wanted to create a simple binary tree as followed by this image:</p>
<p><img src="http://i.stack.imgur.com/hpG9u.png" alt="http://imgur.com/QCVSW.png"></p>
<p>basically empty , but the last values so I created the root list :</p>
<pre><code>root = [list(),list()]
</code></pre>
<p>and made a recursive function ... | 3 | 2009-12-16T22:29:35Z | 16,938,091 | <p>In python you can use the "+" operator to contatenate two lists leaving the originals untouched. I guess that's what you want to do according to your question title. Thus </p>
<pre><code>[1, 2] + [3]
</code></pre>
<p>will return </p>
<pre><code>[1, 2, 3]
</code></pre>
<p>so you can use it more in a "functional ... | 7 | 2013-06-05T10:56:20Z | [
"python",
"list"
] |
split a pdf based on outline | 1,918,420 | <p>i would like to use pyPdf to split a pdf file based on the outline where each destination in the outline refers to a different page within the pdf. </p>
<p>example outline:</p>
<pre>
main --> points to page 1
sect1 --> points to page 1
sect2 --> points to page 15
sect3 --> points to page 22
</... | 5 | 2009-12-16T23:00:08Z | 1,924,950 | <p>i figured it out:</p>
<pre>
class Darrell(pyPdf.PdfFileReader):
def getDestinationPageNumbers(self):
def _setup_outline_page_ids(outline, _result=None):
if _result is None:
_result = {}
for obj in outline:
if isinstance... | 5 | 2009-12-17T22:10:06Z | [
"python",
"pdf",
"pypdf"
] |
split a pdf based on outline | 1,918,420 | <p>i would like to use pyPdf to split a pdf file based on the outline where each destination in the outline refers to a different page within the pdf. </p>
<p>example outline:</p>
<pre>
main --> points to page 1
sect1 --> points to page 1
sect2 --> points to page 15
sect3 --> points to page 22
</... | 5 | 2009-12-16T23:00:08Z | 6,202,708 | <p>Darrell's class can be modified slightly to produce a multi-level table of contents for a pdf (in the manner of pdftoc in the pdftk toolkit.) </p>
<p>My modification adds one more parameter to _setup_page_id_to_num, an integer "level" which defaults to 1. Each invocation increments the level. Instead of storing jus... | 1 | 2011-06-01T14:22:03Z | [
"python",
"pdf",
"pypdf"
] |
split a pdf based on outline | 1,918,420 | <p>i would like to use pyPdf to split a pdf file based on the outline where each destination in the outline refers to a different page within the pdf. </p>
<p>example outline:</p>
<pre>
main --> points to page 1
sect1 --> points to page 1
sect2 --> points to page 15
sect3 --> points to page 22
</... | 5 | 2009-12-16T23:00:08Z | 20,509,335 | <p>This is just what I was looking for. Darrell's additions to PdfFileReader should be part of PyPDF2.</p>
<p>I wrote a little recipe that uses PyPDF2 and sejda-console to split a PDF by bookmarks. In my case there are several Level 1 sections that I want to keep together. This script allows me to do that and give the... | 0 | 2013-12-11T02:18:15Z | [
"python",
"pdf",
"pypdf"
] |
split a pdf based on outline | 1,918,420 | <p>i would like to use pyPdf to split a pdf file based on the outline where each destination in the outline refers to a different page within the pdf. </p>
<p>example outline:</p>
<pre>
main --> points to page 1
sect1 --> points to page 1
sect2 --> points to page 15
sect3 --> points to page 22
</... | 5 | 2009-12-16T23:00:08Z | 24,135,933 | <p>Small update to @darrell class to be able to parse UTF-8 outlines, which I post as answer because comment would be hard to read. </p>
<p>Problem is in <code>pyPdf.pdf.Destination.title</code> which may be returned in two flavors:</p>
<ul>
<li><code>pyPdf.generic.TextStringObject</code></li>
<li><code>pyPdf.generi... | 0 | 2014-06-10T08:09:03Z | [
"python",
"pdf",
"pypdf"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,485 | <p>How about using an ordinary dictionary and instead of doing:</p>
<pre><code>d[x]=y
</code></pre>
<p>use:</p>
<pre><code>d[hash(x)]=y
</code></pre>
<p>To look up:</p>
<pre><code>d[hash(foo)]
</code></pre>
<p>Of course, if there is a hash collision, you may get the wrong value back.</p>
| 2 | 2009-12-16T23:13:10Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,510 | <p>Why not a dictionary + hashlib?</p>
<pre><code>>>> import hashlib
>>> hashtable = {}
>>> def myHash(obj):
return hashlib.sha224(obj).hexdigest()
>>> hashtable[myHash("foo")] = 'bar'
>>> hashtable
{'0808f64e60d58979fcb676c96ec938270dea42445aeefcd3a4e6f8db': 'bar'}
</... | 0 | 2009-12-16T23:19:11Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,532 | <p>Hash table has to store keys, unless you provide a hash function that gives absolutely no collisions, which is nearly impossible.</p>
<p>There is, however, if your keys are string-like, there is a very space-efficient data structure - directed acyclic word graph (DAWG). I don't know any Python implementation though... | 1 | 2009-12-16T23:26:24Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,534 | <p>Build your own b-tree in RAM.</p>
<p>Memory use:</p>
<p>(4 bytes) comparison hash value</p>
<p>(4 bytes) index of next leaf if hash <= comparison OR if negative index of value</p>
<p>(4 bytes) index of next leaf if hash > comparison OR if negative index of value</p>
<p>12 bytes per b-tree node for the b-tree... | 2 | 2009-12-16T23:26:47Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,536 | <p>Although python dictionaries are very efficient, I think that if you're going to store <em>billions</em> of items, you may want to <a href="http://starship.python.net/crew/arcege/extwriting/pyext.html" rel="nofollow">create</a> your own C extension with data structures, optimized for the way you are actually using i... | 2 | 2009-12-16T23:26:49Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,544 | <p>It's not what you asked for buy why not consider Tokyo Cabinet or BerkleyDB for this job? It won't be in memory but you are trading performance for greater storage capacity. You could still keep your list in memory and use the database only to check existence.</p>
| 1 | 2009-12-16T23:28:06Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,551 | <p>Its the good old space vs runtime tradeoff: You can have constant time with linear space usage for the keys in a hastable. Or you can store the key implicitly and use log n time by using a binary tree. The (binary) hash of a value gives you the path in the tree where it will be stored.</p>
| 3 | 2009-12-16T23:29:36Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,576 | <p>If you're actually storing millions of unique values, why not use a dictionary?<br>
Store: <code>d[hash(key)/32] |= 2**(hash(key)%32)</code><br>
Check: <code>(d[hash(key)/32] | 2**(hash(key)%32))</code></p>
<p>If you have billions of entries, use a numpy array of size (2**32)/32, instead. (Because, after all, you ... | 1 | 2009-12-16T23:37:17Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,918,660 | <h3><a href="http://en.wikipedia.org/wiki/Bloom%5Ffilter#Bloomier%5Ffilters" rel="nofollow">Bloomier filters</a> - space-efficient associative array</h3>
<p>From the Wikipedia:</p>
<blockquote>
<p>Chazelle et al. (2004) designed a
generalization of Bloom filters that
could associate a value with each
element ... | 4 | 2009-12-17T00:01:44Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
What is a hashtable/dictionary implementation for Python that doesn't store the keys? | 1,918,456 | <p>I'm storing millions, possibly billions of 4 byte values in a hashtable and I don't want to store any of the keys. I expect that only the hashes of the keys and the values will have to be stored. This has to be fast and all kept in RAM. The entries would still be looked up with the key, unlike set()'s.</p>
<p>What ... | 5 | 2009-12-16T23:06:21Z | 1,919,749 | <p>Would you please tell us more about the keys? I'm wondering if there is any regularity in the keys that we could exploit.</p>
<p>If the keys are strings in a small alphabet (example: strings of digits, like phone numbers) you could use a trie data structure:</p>
<p><a href="http://en.wikipedia.org/wiki/Trie" rel=... | 1 | 2009-12-17T05:58:20Z | [
"python",
"dictionary",
"map",
"hashtable",
"data-structures"
] |
Can anyone explain python's relative imports? | 1,918,539 | <p>I can't for the life of me get python's relative imports to work. I have created a simple example of where it does not function:</p>
<p>The directory structure is:</p>
<pre><code>/__init__.py
/start.py
/parent.py
/sub/__init__.py
/sub/relative.py
</code></pre>
<p><code>/start.py</code> contains just: <code>import... | 86 | 2009-12-16T23:27:05Z | 1,918,581 | <p>You are importing from package "sub". <code>start.py</code> is not itself in a package even if there is a <code>__init__.py</code> present.</p>
<p>You would need to start your program from one directory over <code>parent.py</code>:</p>
<pre><code>./start.py
./pkg/__init__.py
./pkg/parent.py
./pkg/sub/__init__.py
... | 84 | 2009-12-16T23:37:54Z | [
"python"
] |
Make a tkinter window appear over all other windows | 1,918,752 | <pre><code>#!/usr/bin/env python
# Display window with toDisplayText and timeOut of the window.
from Tkinter import *
def showNotification(notificationTimeout, textToDisplay):
## Create main window
root = Tk()
Button(root, text=textToDisplay, activebackground="white", bg="white", command=lambda: root.des... | 1 | 2009-12-17T00:29:16Z | 1,918,980 | <p>According to <a href="http://mail.python.org/pipermail/python-win32/2006-May/004678.html">this message</a> you should be able to add the following after <code>root.overridedirect(1)</code>. A quick test here suggests it should work for you.</p>
<pre><code>root.wm_attributes("-topmost", 1)
</code></pre>
| 5 | 2009-12-17T01:45:37Z | [
"python",
"windows",
"focus",
"stack",
"tkinter"
] |
How can I do python/ruby/javascript style generators in actionscript? | 1,918,817 | <p>I want to use coroutines in actionscript to implement a state machine.</p>
<p>I'd like to be able to do something like the following</p>
<pre><code>function stateMachine():void
{
sendBytes(0xFFFF);
var receiveBytes:ByteArray = yield()
sendBytes(receiveBytes);
}
stateMachine.send( Socket.read() )
</code><... | 2 | 2009-12-17T00:50:26Z | 1,920,570 | <p>Well, how about this?</p>
<pre><code>function stateMachine(socket:Socket, target:YourReceiverClass):void
{
target.sendBytes(0xFFFF);
var receiveByte:int = socket.readByte();
target.sendBytes(receiveByte);
}
stateMachine( mySocket )
</code></pre>
| 1 | 2009-12-17T09:44:53Z | [
"python",
"actionscript",
"generator",
"coroutine"
] |
How can I do python/ruby/javascript style generators in actionscript? | 1,918,817 | <p>I want to use coroutines in actionscript to implement a state machine.</p>
<p>I'd like to be able to do something like the following</p>
<pre><code>function stateMachine():void
{
sendBytes(0xFFFF);
var receiveBytes:ByteArray = yield()
sendBytes(receiveBytes);
}
stateMachine.send( Socket.read() )
</code><... | 2 | 2009-12-17T00:50:26Z | 1,925,533 | <p>As far as I know, Actionscript doesn't have coroutines, continuations or anything that will give you the relevant behavior (call a function without pushing a stack frame). You can fake it using static variables and a <code>switch</code>, but that defeats the purpose of using coroutines for state machines. Also, with... | 2 | 2009-12-18T00:24:24Z | [
"python",
"actionscript",
"generator",
"coroutine"
] |
What's a better way to handle this in Python | 1,918,851 | <p>Simple problem, I have it solved .. but python has a million ways to solve the same problem. I don't want the most terse solution, I just want one that makes more sense than the following:</p>
<pre><code># sql query happens above, returns multiple rows
rows = cursor.fetchall()
cursor.close()
cntdict = {}
for row in... | 0 | 2009-12-17T01:03:47Z | 1,918,870 | <p>You can do the same thing with a generator expression and the dictionary constructor:</p>
<pre><code>dict((row[0], {"b": row[1], "c": row[2]}) for row in rows)
</code></pre>
<p>And here is the code in action:</p>
<pre><code>>>> rows = [[1, 2, 3], [4, 5, 6]]
>>> dict((row[0], {"b": row[1], "c": r... | 12 | 2009-12-17T01:09:25Z | [
"python"
] |
What's a better way to handle this in Python | 1,918,851 | <p>Simple problem, I have it solved .. but python has a million ways to solve the same problem. I don't want the most terse solution, I just want one that makes more sense than the following:</p>
<pre><code># sql query happens above, returns multiple rows
rows = cursor.fetchall()
cursor.close()
cntdict = {}
for row in... | 0 | 2009-12-17T01:03:47Z | 1,919,700 | <p>You might want to look into using an ORM to abstract your database. I can't tell for certain from such a short snippet, but it looks like your data might work well with an ORM if you are just going to build a dict out of it anyway.</p>
<p>My three favorite ORMs: the one in <a href="http://www.djangoproject.com/" r... | -2 | 2009-12-17T05:44:32Z | [
"python"
] |
What's a better way to handle this in Python | 1,918,851 | <p>Simple problem, I have it solved .. but python has a million ways to solve the same problem. I don't want the most terse solution, I just want one that makes more sense than the following:</p>
<pre><code># sql query happens above, returns multiple rows
rows = cursor.fetchall()
cursor.close()
cntdict = {}
for row in... | 0 | 2009-12-17T01:03:47Z | 1,920,192 | <p>As Nadia alluded to, tuple expansion is really handy when working with query results. Her dict() expressions are really nice in this case, though for other queries when you need to do something different with the results just use tuple expansion in the for loop:</p>
<pre><code>for a, b, c in rows:
# do somethin... | 0 | 2009-12-17T08:13:20Z | [
"python"
] |
Reading a file via open().read() vs storing it in a variable | 1,918,965 | <p>I've written this small app in Python that will generate paragraphs of dummy text, kind of like <a href="http://www.lipsum.com/" rel="nofollow">this site</a>, except it'll work offline. Right now you're supposed to provide a reasonably long text file (I'm currently using books from Project Gutenberg), which it will ... | 2 | 2009-12-17T01:41:19Z | 1,918,988 | <p>There's a few problems:</p>
<ul>
<li>Your program becomes really hard to read.</li>
<li>You run the risk of """ appearing in the text and blowing everything up</li>
<li>You break the whole concept of content existing in the format in which it belongs. The book in question is a text file, it should live in a text f... | 10 | 2009-12-17T01:47:29Z | [
"python"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,055 | <p>This is as pythonic as you can get:</p>
<pre><code>for lat, long in zip(Latitudes, Longitudes):
print lat, long
</code></pre>
| 126 | 2009-12-17T02:03:39Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,056 | <p>Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented <a href="https://docs.python.org/2/library/functions.html#zip" rel="nofollow">here</a>.</p>
<pre><code>>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zi... | 4 | 2009-12-17T02:03:45Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,058 | <pre><code>for Lat,Long in zip(Latitudes, Longitudes):
</code></pre>
| 3 | 2009-12-17T02:04:16Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,075 | <p>Good to see lots of love for <code>zip</code> in the answers here. </p>
<p>However it should be noted that if you are using a python version before 3.0, the <code>itertools</code> module in the standard library contains an <code>izip</code> function which returns an iterable, which is more appropriate in this case ... | 15 | 2009-12-17T02:10:49Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,402 | <p>Another way to do this would be to by using <code>map</code>.</p>
<pre><code>>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> for i,j in map(None,a,b):
... print i,j
...
1 4
2 5
3 6
</code></pre>
<p>One difference in using map compared to zip is, with zip the length of new list is<br>
same ... | 28 | 2009-12-17T04:10:06Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 1,919,544 | <p>in case your Latitude and Longitude lists are large and lazily loaded:</p>
<pre><code>from itertools import izip
for lat, lon in izip(latitudes, longitudes):
process(lat, lon)
</code></pre>
<p>or if you want to avoid the for-loop</p>
<pre><code>from itertools import izip, imap
out = imap(process, izip(latitud... | 12 | 2009-12-17T04:54:35Z | [
"python",
"list",
"iteration"
] |
Is there a better way to iterate over two lists, getting one element from each list for each iteration? | 1,919,044 | <p>I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.</p>
<p>Do you think it's be better to:</p>
<p>A) (assume that equal lengths is already checked)</p>
<pre><code>for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
</code></pre>
<p>*** ED... | 67 | 2009-12-17T02:00:55Z | 37,171,380 | <p>I think you can just use: </p>
<p><code>for i in range(len(latitudes))</code> </p>
<p>Since the latitudes and longitudes should be paired, their length should be same. That way you don't need to create a temp variable of two array via <code>zip</code>.</p>
| -2 | 2016-05-11T19:09:10Z | [
"python",
"list",
"iteration"
] |
how to add all of these values in a python dictionary | 1,919,054 | <p>last one for the night, want to see what clever ways there are with python to add all of the 'count' values from the following type of dictionary:</p>
<pre><code>{0: {'count': 1000}, 1: {'count': 2000}}
</code></pre>
<p>so the end result should be an int value of 3000.</p>
| 0 | 2009-12-17T02:03:29Z | 1,919,070 | <pre><code>sum(i['count'] for i in d.values())
</code></pre>
| 0 | 2009-12-17T02:10:25Z | [
"python"
] |
how to add all of these values in a python dictionary | 1,919,054 | <p>last one for the night, want to see what clever ways there are with python to add all of the 'count' values from the following type of dictionary:</p>
<pre><code>{0: {'count': 1000}, 1: {'count': 2000}}
</code></pre>
<p>so the end result should be an int value of 3000.</p>
| 0 | 2009-12-17T02:03:29Z | 1,919,072 | <pre><code>>>> x = {0: {'count': 1000}, 1: {'count': 2000}}
>>> sum(v['count'] for v in x.values())
3000
</code></pre>
| 4 | 2009-12-17T02:10:33Z | [
"python"
] |
how to add all of these values in a python dictionary | 1,919,054 | <p>last one for the night, want to see what clever ways there are with python to add all of the 'count' values from the following type of dictionary:</p>
<pre><code>{0: {'count': 1000}, 1: {'count': 2000}}
</code></pre>
<p>so the end result should be an int value of 3000.</p>
| 0 | 2009-12-17T02:03:29Z | 1,919,100 | <p>A shorter one:</p>
<pre><code>sum(d[k]['count'] for k in d)
</code></pre>
| 4 | 2009-12-17T02:18:59Z | [
"python"
] |
how to add all of these values in a python dictionary | 1,919,054 | <p>last one for the night, want to see what clever ways there are with python to add all of the 'count' values from the following type of dictionary:</p>
<pre><code>{0: {'count': 1000}, 1: {'count': 2000}}
</code></pre>
<p>so the end result should be an int value of 3000.</p>
| 0 | 2009-12-17T02:03:29Z | 1,919,204 | <p>How about using reduction in python?</p>
<pre><code>reduce(lambda x,y: x+y, [v['count'] for v in a.values()])
</code></pre>
| 0 | 2009-12-17T03:03:06Z | [
"python"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,112 | <pre><code>mydict = {"&y":"\033[0;30m",
"&c":"\033[0;31m",
"&b":"\033[0;32m",
"&Y":"\033[0;33m",
"&u":"\033[0;34m"}
mystr = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
for k, v in mydict.iteritems():
mystr = mystr.replace(k... | 24 | 2009-12-17T02:24:03Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,113 | <p>Not sure about the speed of this solution either, but you could just loop through your dictionary and repeatedly call the built-in </p>
<p><code>str.replace(old, new)</code> </p>
<p>This might perform decently well if the original string isn't too long, but it would obviously suffer as the string got longer.</p>
| 1 | 2009-12-17T02:24:04Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,121 | <p>If you really want to dig into the topic take a look at this: <a href="http://en.wikipedia.org/wiki/Aho-Corasick%5Falgorithm">http://en.wikipedia.org/wiki/Aho-Corasick%5Falgorithm</a></p>
<p>The obvious solution by iterating over the dictionary and replacing each element in the string takes <code>O(n*m)</code> time... | 7 | 2009-12-17T02:27:00Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,123 | <p><a href="http://code.activestate.com/recipes/81330/" rel="nofollow">This</a> seems like it does what you want - multiple string replace at once using RegExps. Here is the relevant code:</p>
<pre><code>def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compi... | 3 | 2009-12-17T02:27:18Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,124 | <p>Try this, making use of regular expression substitution, and standard string formatting:</p>
<pre><code># using your stated values for str and dict:
>>> import re
>>> str = re.sub(r'(&[a-zA-Z])', r'%(\1)s', str)
>>> str % dict
'The \x1b[0;30mquick \x1b[0;31mbrown \x1b[0;32mfox \x1b[0;... | 13 | 2009-12-17T02:27:43Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,146 | <p>If the number of keys in the list is large, and the number of the occurences in the string is low (and mostly zero), then you could iterate over the occurences of the ampersands in the string, and use the dictionary keyed by the first character of the substrings. I don't code often in python so the style might be a ... | 5 | 2009-12-17T02:37:22Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,154 | <pre><code>>>> a=[]
>>> str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
>>> d={"&y":"\033[0;30m",
... "&c":"\033[0;31m",
...... | 0 | 2009-12-17T02:40:10Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,175 | <p>A general solution for defining replacement rules is to use regex substitution using a function to provide the map (see <a href="http://docs.python.org/library/re.html#re.sub" rel="nofollow">re.sub()</a>).</p>
<pre><code>import re
str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
d... | 3 | 2009-12-17T02:48:54Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,177 | <p>The problem with doing this mass replace in Python is immutability of the strings: every time you will replace one item in the string then entire new string will be reallocated again and again from the heap.</p>
<p>So if you want the fastest solution you either need to use mutable container (e.g. list), or write th... | 1 | 2009-12-17T02:49:06Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,221 | <p>Here is a version using split/join</p>
<pre><code>mydict = {"y":"\033[0;30m",
"c":"\033[0;31m",
"b":"\033[0;32m",
"Y":"\033[0;33m",
"u":"\033[0;34m"}
mystr = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
myparts = mystr.split("&")
myparts[1:... | 3 | 2009-12-17T03:07:50Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,494 | <p>Since someone mentioned using a simple parser, I thought I'd cook one up using pyparsing. By using pyparsing's transformString method, pyparsing internally scans through the source string, and builds a list of the matching text and intervening text. When all is done, transformString then ''.join's this list, so th... | 1 | 2009-12-17T04:36:26Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 1,919,635 | <p>Here is the C Extensions Approach for python</p>
<pre><code>const char *dvals[]={
//"0-64
"","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","","",
"","","","","","","","","","",
"","",""... | 4 | 2009-12-17T05:22:09Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
Mass string replace in python? | 1,919,096 | <p>Say I have a string that looks like this:</p>
<pre><code>str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
</code></pre>
<p>You'll notice a lot of locations in the string where there is an ampersand, followed by a character (such as "&y" and "&c"). I need to replace these c... | 37 | 2009-12-17T02:17:38Z | 29,386,712 | <p>try this</p>
<p>tr.replace("&y",dict["&y"])</p>
<p>tr.replace("&c",dict["&c"])</p>
<p>tr.replace("&b",dict["&b"])</p>
<p>tr.replace("&Y",dict["&Y"])</p>
<p>tr.replace("&u",dict["&u"])</p>
| 0 | 2015-04-01T09:00:37Z | [
"python",
"regex",
"string",
"performance",
"replace"
] |
How to make PHP output a sound (beep)? | 1,919,826 | <p>What's the PHP verson of this python code?</p>
<pre><code>import winsound
winsound.Beep(537, 2000)
</code></pre>
| 2 | 2009-12-17T06:20:00Z | 1,919,839 | <p>php is mostly used on webservers, so what the use beeping there, and you can't beep on user computer through php, as php is translated into HTML, which has no such method.</p>
<p>If you want to have Win32 calls have a look at: <a href="http://stackoverflow.com/questions/1411657/how-do-i-make-win32-api-calls-from-ph... | 5 | 2009-12-17T06:23:24Z | [
"php",
"python",
"beep",
"synthesizer"
] |
How to make PHP output a sound (beep)? | 1,919,826 | <p>What's the PHP verson of this python code?</p>
<pre><code>import winsound
winsound.Beep(537, 2000)
</code></pre>
| 2 | 2009-12-17T06:20:00Z | 1,919,936 | <p>Update: Never mind, I thought you just wanted a 'beep', not a TONE.</p>
<p>Old post, not answering the question:</p>
<p>You'd need to make a .bat file, so:
Open cmd</p>
<pre><code>copy con go.bat [Enter]
@echo off [Enter]
echo [Ctrl+G] [Enter]
[Ctrl+Z] [Enter]
</code></pre>
<p>This looks like:</p>
<pre><code>C:... | 0 | 2009-12-17T06:56:09Z | [
"php",
"python",
"beep",
"synthesizer"
] |
How to make PHP output a sound (beep)? | 1,919,826 | <p>What's the PHP verson of this python code?</p>
<pre><code>import winsound
winsound.Beep(537, 2000)
</code></pre>
| 2 | 2009-12-17T06:20:00Z | 4,970,892 | <p>I tried what Tor Valamo suggested, but I still couldn't get the sound to play. </p>
<p>I would simply get a representation of the chr(7) on my screen but no sound when I used:</p>
<pre><code>system('cmd /k go.bat')
</code></pre>
<p>And I would get nothing at all if I used: </p>
<pre><code>exec('cmd /k go.bat')... | 1 | 2011-02-11T15:34:20Z | [
"php",
"python",
"beep",
"synthesizer"
] |
How to make PHP output a sound (beep)? | 1,919,826 | <p>What's the PHP verson of this python code?</p>
<pre><code>import winsound
winsound.Beep(537, 2000)
</code></pre>
| 2 | 2009-12-17T06:20:00Z | 29,202,615 | <p>Of course people write GUI apps in PHP - that's what wxPHP is for.</p>
<p>Install mpg321 - a tiny sound app:</p>
<pre><code>exec("mpg321 --quiet --gain 10 /path/to/beep.mp3");
</code></pre>
| 0 | 2015-03-23T02:54:54Z | [
"php",
"python",
"beep",
"synthesizer"
] |
Sprox form with Turbogears, using Mako, only displays plain text | 1,919,833 | <p>I'm generating a Sprox form with Turbogears 2.1 and trying to display it in a Mako template. Here is my code:</p>
<p>To define the form:</p>
<pre><code>class NewUserForm(AddRecordForm):
__model__ = User
newuserform = NewUserForm(DBSession)
</code></pre>
<p>The controller definition that assigns the form... | 2 | 2009-12-17T06:21:38Z | 1,937,001 | <p>Figured it out. I have to pass the variable through the the 'n' mako filter to remove any automatic filters mako applies to the html generated. So:</p>
<p>${tmpl_context.register_form(value=value) | n}</p>
| 3 | 2009-12-20T20:49:08Z | [
"python",
"forms",
"mako",
"turbogears",
"sprox"
] |
Tornado Web Framework Mysql connection handling | 1,920,012 | <p>I have recently been exploring the Tornado web framework to serve a lot of consistent connections by lots of different clients.</p>
<p>I have a request handler that basically takes an RSA encrypted string and decrypts it. The decrypted text is an XML string that gets parsed by a SAX document handler that I have wr... | 5 | 2009-12-17T07:19:17Z | 1,920,107 | <p>An SQL connection should not take 5 seconds. Try to not issue a query and see if that improves your performance - which it should.</p>
<p>The Mysqldb module has a threadsafety of "1", which means the module is thread safe, but connections cannot be shared amongst threads. You can implement a connection pool as an a... | 1 | 2009-12-17T07:50:36Z | [
"python",
"mysql",
"time",
"tornado",
"persistent-connection"
] |
Tornado Web Framework Mysql connection handling | 1,920,012 | <p>I have recently been exploring the Tornado web framework to serve a lot of consistent connections by lots of different clients.</p>
<p>I have a request handler that basically takes an RSA encrypted string and decrypts it. The decrypted text is an XML string that gets parsed by a SAX document handler that I have wr... | 5 | 2009-12-17T07:19:17Z | 1,923,320 | <p>Consider <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>, which provides a nicer abstraction over DBAPI and also provides connection pooling, etc. (You can happily ignore its ORM and just use the SQL-toolkit)</p>
<p>(Also, you're not doing blocking database calls in the asynchronous request handl... | 1 | 2009-12-17T17:30:08Z | [
"python",
"mysql",
"time",
"tornado",
"persistent-connection"
] |
Tornado Web Framework Mysql connection handling | 1,920,012 | <p>I have recently been exploring the Tornado web framework to serve a lot of consistent connections by lots of different clients.</p>
<p>I have a request handler that basically takes an RSA encrypted string and decrypts it. The decrypted text is an XML string that gets parsed by a SAX document handler that I have wr... | 5 | 2009-12-17T07:19:17Z | 3,835,206 | <p>Declare it in the base handler, it will be called once per application.</p>
| 0 | 2010-09-30T22:14:10Z | [
"python",
"mysql",
"time",
"tornado",
"persistent-connection"
] |
What is so bad with threadlocals | 1,920,053 | <p>Everybody in Django world seems to hate threadlocals(<a href="http://code.djangoproject.com/ticket/4280">http://code.djangoproject.com/ticket/4280</a>, <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</a>). I read Armin's essay on... | 21 | 2009-12-17T07:29:08Z | 1,920,449 | <p>You want to use threadlocals when you're working with multiple threads and want to localize some objects to a specific thread, eg. having one database connection for each thread.
In your case, you want to use it more as a global context (if I understand you correctly), which is probably a bad idea. It will make your... | 2 | 2009-12-17T09:24:53Z | [
"python",
"django",
"thread-local"
] |
What is so bad with threadlocals | 1,920,053 | <p>Everybody in Django world seems to hate threadlocals(<a href="http://code.djangoproject.com/ticket/4280">http://code.djangoproject.com/ticket/4280</a>, <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</a>). I read Armin's essay on... | 21 | 2009-12-17T07:29:08Z | 1,921,007 | <blockquote>
<p>Also a lot of Java frameworks seem to be using threadlocals a lot, so how is their case different from Python/Django 's?</p>
</blockquote>
<p>CPython's interpreter has a Global Interpreter Lock (GIL) which means that only one Python thread can be executed by the interpreter at any given time. It isn'... | 3 | 2009-12-17T11:05:56Z | [
"python",
"django",
"thread-local"
] |
What is so bad with threadlocals | 1,920,053 | <p>Everybody in Django world seems to hate threadlocals(<a href="http://code.djangoproject.com/ticket/4280">http://code.djangoproject.com/ticket/4280</a>, <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</a>). I read Armin's essay on... | 21 | 2009-12-17T07:29:08Z | 1,921,176 | <p>I don't think there is anything wrong with threadlocals - yes, it is a global variable, but besides that it's a normal tool. We use it just for this purpose (storing subdomain model in the context global to the current request from middleware) and it works perfectly.</p>
<p>So I say, use the right tool for the job,... | 17 | 2009-12-17T11:39:39Z | [
"python",
"django",
"thread-local"
] |
What is so bad with threadlocals | 1,920,053 | <p>Everybody in Django world seems to hate threadlocals(<a href="http://code.djangoproject.com/ticket/4280">http://code.djangoproject.com/ticket/4280</a>, <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</a>). I read Armin's essay on... | 21 | 2009-12-17T07:29:08Z | 1,924,524 | <p>I avoid this sort of usage of threadlocals, because it introduces an implicit non-local coupling. I frequently use models in all kinds of non-HTTP-oriented ways (local management commands, data import/export, etc). If I access some threadlocals data in models.py, now I have to find some way to ensure that it is alwa... | 21 | 2009-12-17T20:53:51Z | [
"python",
"django",
"thread-local"
] |
What is so bad with threadlocals | 1,920,053 | <p>Everybody in Django world seems to hate threadlocals(<a href="http://code.djangoproject.com/ticket/4280">http://code.djangoproject.com/ticket/4280</a>, <a href="http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser">http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser</a>). I read Armin's essay on... | 21 | 2009-12-17T07:29:08Z | 8,960,802 | <p>I have found using ThreadLocal is an excellent way to implement Dependency Injection in a HTTP request/response environment (i.e. any webapp). You just set up a servlet filter to 'inject' the object you need into the thread on receiving the request and 'uninject' it on returning the response.</p>
<p>It's a smart ma... | 0 | 2012-01-22T11:44:12Z | [
"python",
"django",
"thread-local"
] |
Methods to create text preview from a font | 1,920,129 | <p>I'm currently using ImageMagick's convert command to create text preview (.png) from .ttf font file. Overall, it's better in auto text positioning despite it failed to read some valid .ttf file sometimes. The speed is not great but acceptable.</p>
<p>PIL's ImageFont looks like is not good at text aligning, often pr... | 1 | 2009-12-17T07:56:30Z | 1,934,015 | <p>This is sort of off-the-wall, but have you looked at <a href="http://wiki.novemberborn.net/sifr/" rel="nofollow">sIFR (Scalable Inman Flash Replacement)</a>? It is used to allow web designers to insert short text chunks (headings and such) with text rendered in any font they choose to use, even though the user does ... | 0 | 2009-12-19T20:16:41Z | [
"python",
"image",
"fonts",
"imagemagick",
"python-imaging-library"
] |
Methods to create text preview from a font | 1,920,129 | <p>I'm currently using ImageMagick's convert command to create text preview (.png) from .ttf font file. Overall, it's better in auto text positioning despite it failed to read some valid .ttf file sometimes. The speed is not great but acceptable.</p>
<p>PIL's ImageFont looks like is not good at text aligning, often pr... | 1 | 2009-12-17T07:56:30Z | 1,934,045 | <p>I imagine that such sites are driven by a little custom code using something like <a href="http://www.pango.org/" rel="nofollow">Pango</a>, or just bare-bones <a href="http://www.freetype.org/" rel="nofollow">Freetype</a> at the back end to render to image files.</p>
<p>The images may well never even hit the disc,... | 1 | 2009-12-19T20:30:06Z | [
"python",
"image",
"fonts",
"imagemagick",
"python-imaging-library"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,179 | <p>Use the <code>in</code> operator instead of calling <code>__contains__</code> directly.</p>
<p>What you have almost works (but is O(n**2)):</p>
<pre><code>for i in xrange(len(list_a)):
for j in xrange(i + 1, len(list_a)):
if list_a[i] == list_a[j]:
print "duplicate:", list_a[i]
</code></pre>
<p>But it... | 23 | 2009-12-17T08:11:01Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,187 | <p>If you're looking for one-to-one mapping between your nested loops and Python, this is what you want:</p>
<pre><code>n = len(list_a)
for i in range(n):
for j in range(i+1, n):
if list_a[i] == list_a[j]:
print list_a[i]
</code></pre>
<p>The code above is not "Pythonic". I would do it someth... | 3 | 2009-12-17T08:12:50Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,190 | <p>You could just "translate" it line by line.</p>
<p>c++ </p>
<pre><code>for (int i=0;i<=list_a.length;i++)
for (int j=i+1;j<=list_a.length;j++)
if (list_a[i]==list_a[j])
print list_a[i]
</code></pre>
<p>Python</p>
<pre><code>for i in range(0, len(list_a)):
for j in range(i + 1, ... | 0 | 2009-12-17T08:13:14Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,196 | <p>You could always use a list comprehension:</p>
<pre><code>dups = [x for x in list_a if list_a.count(x) > 1]
</code></pre>
| 14 | 2009-12-17T08:14:26Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,252 | <p>The following requires the elements of your list to be hashable (not just implementing <code>__eq__</code> ).
I find it more pythonic to use a defaultdict (and you have the number of repetitions for free):</p>
<pre>
import collections
l = [1, 2, 4, 1, 3, 3]
d = collections.defaultdict(int)
for x in l:
d[x] += 1
... | 2 | 2009-12-17T08:35:43Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,254 | <p>Just for information, In python 2.7+, we can use Counter</p>
<pre><code>import collections
x=[1, 2, 3, 5, 6, 7, 5, 2]
>>> x
[1, 2, 3, 5, 6, 7, 5, 2]
>>> y=collections.Counter(x)
>>> y
Counter({2: 2, 5: 2, 1: 1, 3: 1, 6: 1, 7: 1})
</code></pre>
<p>Unique List</p>
<pre><code>>>&g... | 50 | 2009-12-17T08:36:46Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,326 | <p>A little bit more Pythonic implementation (not the most, of course), but in the spirit of your C code could be:</p>
<pre><code>for i, elem in enumerate(seq):
if elem in seq[i+1:]:
print elem
</code></pre>
<p><strong>Edit:</strong> yes, it prints the elements more than once if there're more than 2 repet... | -2 | 2009-12-17T08:52:49Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 1,920,395 | <p>Before Python 2.3, use dict() :</p>
<pre><code>>>> lst = [1, 2, 3, 5, 6, 7, 5, 2]
>>> stats = {}
>>> for x in lst : # count occurrences of each letter:
... stats[x] = stats.get(x, 0) + 1
>>> print stats
{1: 1, 2: 2, 3: 1, 5: 2, 6: 1, 7: 1} # filter letters appearing more tha... | 7 | 2009-12-17T09:10:26Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 10,006,869 | <p>Using numpy:</p>
<pre><code>import numpy as np
count,value = np.histogram(list_a,bins=np.hstack((np.unique(list_a),np.inf)))
print 'duplicate value(s) in list_a: ' + ', '.join([str(v) for v in value[count>1]])
</code></pre>
| 0 | 2012-04-04T07:42:04Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 11,201,157 | <pre><code>def get_duplicates(arr):
dup_arr = arr[:]
for i in set(arr):
dup_arr.remove(i)
return list(set(dup_arr))
print get_duplicates([1,2,3,5,6,7,5,2])
[2, 5]
print get_duplicates([1,2,1,3,4,5,4,4,6,7,8,2])
[1, 2, 4]
</code></pre>
| 5 | 2012-06-26T05:21:01Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 14,543,977 | <p>It looks like you have a list (<code>list_a</code>) potentially including duplicates, which you would rather keep as it is, and build a de-duplicated list <code>tmp</code> based on list_a. In Python 2.7, you can accomplish this with one line: </p>
<p><code>tmp = list(set(list_a))</code></p>
<p>Comparing the length... | 1 | 2013-01-27T02:55:49Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 19,024,059 | <p>Just quick and dirty, </p>
<pre><code>list_a=[1,2,3,5,6,7,5,2]
holding_list=[]
for x in list_a:
if x in holding_list:
pass
else:
holding_list.append(x)
print holding_list
</code></pre>
<h1>Output [1, 2, 3, 5, 6, 7]</h1>
| 0 | 2013-09-26T09:15:23Z | [
"python",
"duplicates"
] |
How to find duplicate elements in array using for loop in Python? | 1,920,145 | <p>I have a list with duplicate elements:</p>
<pre><code> list_a=[1,2,3,5,6,7,5,2]
tmp=[]
for i in list_a:
if tmp.__contains__(i):
print i
else:
tmp.append(i)
</code></pre>
<p>I have used the above code to find the duplicate elements in the <code>list_a</code>. I don't want to remove t... | 17 | 2009-12-17T08:01:33Z | 35,055,490 | <p>Using only itertools, and works fine on Python 2.5</p>
<pre><code>from itertools import groupby
list_a = sorted([1, 2, 3, 5, 6, 7, 5, 2])
result = dict([(r, len(list(grp))) for r, grp in groupby(list_a)])
</code></pre>
<p>Result:</p>
<pre><code>{1: 1, 2: 2, 3: 1, 5: 2, 6: 1, 7: 1}
</code></pre>
| 0 | 2016-01-28T07:42:36Z | [
"python",
"duplicates"
] |
TinyMCE Spellchecker in Pylons | 1,920,162 | <p>I've been trying to get the TinyMCE spellchecker working with my Pylons app. My first problem is actually capturing the post data in the first place. Firebug tells me that the following is being sent:</p>
<pre><code>{"id":"c0","method":"checkWords","params":["en",["Lorem","ipsum","dolor","sit","amet","consectetur",... | 0 | 2009-12-17T08:07:15Z | 1,920,258 | <p>That's not really an answer to your question but I hope it will help:</p>
<p>You maybe interested to look at <a href="http://code.google.com/p/django-tinymce/" rel="nofollow">django-tinymce</a> as a source of inspiration. The spellchecker is based on PyEnchant</p>
| 1 | 2009-12-17T08:38:01Z | [
"python",
"tinymce",
"pylons",
"spell-checking",
"pyenchant"
] |
python write CD/DVD iso file | 1,920,246 | <p>I'm making a cross-platform (Windows and OS X) with wxPython that will be compiled to exe later.</p>
<p>Is it possible for me to create ISO files for CDs or DVDs in Python to burn a data disc with?</p>
<p>Thanks,
Chris</p>
| 4 | 2009-12-17T08:34:32Z | 1,920,536 | <p>Following 'do not reinvent the wheel' I would try using <code>mkisofs</code> (part of cdrtools) (although originating on Linux, I think there are windows builds floating around the net).</p>
| 1 | 2009-12-17T09:39:55Z | [
"python",
"file",
"system",
"iso"
] |
Fill list with objects and sort (Newbie) | 1,920,315 | <p>I'm new to Python, so please forgive me when using wrong terms :)</p>
<p>I'd like to have a list of several "objects", each of them having the same numeric attributes (A, B, C). This list should then be sorted by the value of attribute A.</p>
<p>In Java I'd define a Class with my attributes as members, implement S... | 0 | 2009-12-17T08:50:49Z | 1,920,364 | <p>Sorry, If I took your question wrong way. I don't get it very well.</p>
<p>So, I will assume like you want to sort by column</p>
<p>Lets say x is 2 dimensional array</p>
<pre><code>>>> x=[[1, 2, 3], [1, 2, 4], [2, 5, 1], [3, 1, 1]]
>>> x
[[1, 2, 3], [1, 2, 4], [2, 5, 1], [3, 1, 1]]
</code></pre>... | 4 | 2009-12-17T09:01:35Z | [
"python",
"list",
"sorting",
"object"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.