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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 11,768,336 | <p>As other answers said, using the string method count() is probably the simplest, but if you're doing this frequently, check out collections.Counter:</p>
<pre><code>from collections import Counter
str = "Mary had a little lamb"
counter = Counter(str)
print counter['a']
</code></pre>
| 51 | 2012-08-01T22:12:08Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 27,321,071 | <p>Regular expressions are very useful if you want case-insensitivity (and of course all the power of regex).</p>
<pre><code>my_string = "Mary had a little lamb"
# simplest solution, using count, is case-sensitive
my_string.count("m") # yields 1
import re
# case-sensitive with regex
len(re.findall("m", my_string))
#... | 6 | 2014-12-05T17:04:28Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 27,889,928 | <h1>"Without using count to find you want character in string" method.</h1>
<pre><code>import re
def count(s, ch):
pass
def main():
s = raw_input ("Enter strings what you like, for example, 'welcome': ")
ch = raw_input ("Enter you want count characters, but best result to find one character: " )
pr... | 3 | 2015-01-11T17:24:27Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 33,324,089 | <pre><code>a = 'have a nice day'
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
print key, a.count(key)
</code></pre>
| 2 | 2015-10-24T22:36:58Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 33,723,716 | <pre><code>spam = 'have a nice day'
var = 'd'
def count(spam, var):
found = 0
for key in spam:
if key == var:
found += 1
return found
count(spam, var)
print 'count %s is: %s ' %(var, count(spam, var))
</code></pre>
| 0 | 2015-11-15T19:05:09Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 35,586,210 | <p>No more than this IMHO - you can add the upper or lower methods</p>
<pre><code>def count_letter_in_str(string,letter):
return string.count(letter)
</code></pre>
| 0 | 2016-02-23T19:16:31Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 36,346,557 | <pre><code>str = "count a character occurance"
List = list(str)
print (List)
Uniq = set(List)
print (Uniq)
for key in Uniq:
print (key, str.count(key))
</code></pre>
| 0 | 2016-04-01T01:29:24Z | [
"python",
"string",
"count"
] |
Count occurrence of a character in a string | 1,155,617 | <p>What's the simplest way to count the number of occurrences of a character in a string?</p>
<p>e.g. count the number of times <code>'a'</code> appears in <code>'Mary had a little lamb'</code></p>
| 433 | 2009-07-20T20:00:36Z | 39,813,320 | <p><code>str.count(a)</code> is the best solution to count a single character in a string. But if you need to count more characters than one you would have to read the string as many times as characters you want to count.</p>
<p>A better approach for this job would be:</p>
<pre><code>from collections import defaultdi... | 0 | 2016-10-02T02:33:10Z | [
"python",
"string",
"count"
] |
Python: Importing pydoc and then using it natively? | 1,155,853 | <p>I know how to use pydoc from the command line. However, because of complicated environmental setup, it would be preferable to run it within a python script as a native API call. That is, my python runner looks a bit like this:</p>
<p>import pydoc
pydoc.generate_html_docs_for(someFile)</p>
<p>However, it's not cl... | 2 | 2009-07-20T20:41:24Z | 1,155,885 | <p>Do you mean something like this?</p>
<pre><code>>>> import pydoc
>>> pydoc.writedoc('sys')
wrote sys.html
>>>
</code></pre>
| 4 | 2009-07-20T20:48:28Z | [
"python"
] |
Latin-1 and the unicode factory in Python | 1,155,903 | <p>I have a Python 2.6 script that is gagging on special characters, encoded in Latin-1, that I am retrieving from a SQL Server database. I would like to print these characters, but I'm somewhat limited because I am using a library that calls the <code>unicode</code> factory, and I don't know how to make Python use a c... | 4 | 2009-07-20T20:52:20Z | 1,155,923 | <p>Add this at the beginning of the module:</p>
<pre><code># coding: latin1
</code></pre>
<p>Or decode the string to Unicode yourself.</p>
<p>[Edit]</p>
<p>It's been a while since I played with Unicode, but hopefully this example will show how to convert from Latin1 to Unicode:</p>
<pre><code>>>> s = u'é... | 4 | 2009-07-20T20:59:20Z | [
"python",
"unicode"
] |
Latin-1 and the unicode factory in Python | 1,155,903 | <p>I have a Python 2.6 script that is gagging on special characters, encoded in Latin-1, that I am retrieving from a SQL Server database. I would like to print these characters, but I'm somewhat limited because I am using a library that calls the <code>unicode</code> factory, and I don't know how to make Python use a c... | 4 | 2009-07-20T20:52:20Z | 1,155,968 | <p>Maybe try to decode the latin1-encoded strings into unicode?</p>
<pre><code>t.add_row((value.decode('latin1') for value in rec))
</code></pre>
| 1 | 2009-07-20T21:09:36Z | [
"python",
"unicode"
] |
Latin-1 and the unicode factory in Python | 1,155,903 | <p>I have a Python 2.6 script that is gagging on special characters, encoded in Latin-1, that I am retrieving from a SQL Server database. I would like to print these characters, but I'm somewhat limited because I am using a library that calls the <code>unicode</code> factory, and I don't know how to make Python use a c... | 4 | 2009-07-20T20:52:20Z | 1,156,570 | <p>After a quick peek at the source for PrettyTable, it appears that it works on unicode objects internally (see <code>_stringify_row</code>, <code>add_row</code> and <code>add_column</code>, for example). Since it doesn't know what encoding your input strings are using, it uses the default encoding, <a href="http://di... | 0 | 2009-07-20T23:45:32Z | [
"python",
"unicode"
] |
Print current call stack from a method in Python code | 1,156,023 | <p>In Python, how can I print the current call stack from within a method (for debugging purposes).</p>
| 141 | 2009-07-20T21:24:05Z | 1,156,048 | <p>Here's an example of getting the stack via the <a href="http://docs.python.org/library/traceback.html">traceback</a> module, and printing it:</p>
<pre><code>import traceback
def f():
g()
def g():
for line in traceback.format_stack():
print(line.strip())
f()
# Prints:
# File "so-stack.py", line 1... | 161 | 2009-07-20T21:27:43Z | [
"python",
"debugging",
"stack-trace"
] |
Print current call stack from a method in Python code | 1,156,023 | <p>In Python, how can I print the current call stack from within a method (for debugging purposes).</p>
| 141 | 2009-07-20T21:24:05Z | 1,156,055 | <pre><code>import traceback
traceback.print_stack()
</code></pre>
| 46 | 2009-07-20T21:28:14Z | [
"python",
"debugging",
"stack-trace"
] |
Print current call stack from a method in Python code | 1,156,023 | <p>In Python, how can I print the current call stack from within a method (for debugging purposes).</p>
| 141 | 2009-07-20T21:24:05Z | 16,200,714 | <p><code>inspect.stack()</code> returns the current stack rather than the exception traceback:</p>
<pre><code>import inspect
print inspect.stack()
</code></pre>
<p>See <a href="https://gist.github.com/FredLoney/5454553">https://gist.github.com/FredLoney/5454553</a> for a log_stack utility function.</p>
| 11 | 2013-04-24T19:37:20Z | [
"python",
"debugging",
"stack-trace"
] |
Composable Regexp in Python | 1,156,030 | <p>Often, I would like to build up complex regexps from simpler ones. The only way I'm currently aware of of doing this is through string operations, e.g.:</p>
<pre><code>Year = r'[12]\d{3}'
Month = r'Jan|Feb|Mar'
Day = r'\d{2}'
HourMins = r'\d{2}:\d{2}'
Date = r'%s %s, %s, %s' % (Month, Day, Year, HourMins)
DateR = ... | 5 | 2009-07-20T21:24:58Z | 1,156,208 | <p>You can use Python's formatting syntax for this:</p>
<pre><code>types = {
"year": r'[12]\d{3}',
"month": r'(Jan|Feb|Mar)',
"day": r'\d{2}',
"hourmins": r'\d{2}:\d{2}',
}
import re
Date = r'%(month)s %(day)s, %(year)s, %(hourmins)s' % types
DateR = re.compile(Date)
</co... | 4 | 2009-07-20T22:02:17Z | [
"python",
"regex"
] |
Composable Regexp in Python | 1,156,030 | <p>Often, I would like to build up complex regexps from simpler ones. The only way I'm currently aware of of doing this is through string operations, e.g.:</p>
<pre><code>Year = r'[12]\d{3}'
Month = r'Jan|Feb|Mar'
Day = r'\d{2}'
HourMins = r'\d{2}:\d{2}'
Date = r'%s %s, %s, %s' % (Month, Day, Year, HourMins)
DateR = ... | 5 | 2009-07-20T21:24:58Z | 1,156,507 | <p>You could use Ping's <a href="http://zesty.ca/python/rxb.py" rel="nofollow">rxb</a>:</p>
<pre><code>year = member("1", "2") + digit*3
month = either("Jan", "Feb", "Mar")
day = digit*2
hour_mins = digit*2 + ":" + digit*2
date = month + " " + day + ", " + year + ", " + hour_mins
</code></pre>
<p>You can then match ... | 1 | 2009-07-20T23:25:04Z | [
"python",
"regex"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,114 | <p>You're always going to have a loop - someone might come along with a clever one-liner that hides the loop within a call to <code>map()</code> or similar, but it's always going to be there.</p>
<p>My preference would always be to have clean and simple code, unless performance is a major factor.</p>
<p>Here's perhap... | 20 | 2009-07-20T21:39:56Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,128 | <pre><code>>>> my_list =[ ['a', 'b'], ['a', 'c'], ['b', 'd'] ]
>>> 'd' in (x[1] for x in my_list)
True
</code></pre>
<p>Editing to add:</p>
<p>Both David's answer using <strong>any</strong> and mine using <strong>in</strong> will end when they find a match since we're using generator expressions. H... | 7 | 2009-07-20T21:43:54Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,143 | <p>Nothing against RichieHindle's and Anon's answers, but here's how I'd write it:</p>
<pre><code>data = [['a','b'], ['a','c'], ['b','d']]
search = 'c'
any(e[1] == search for e in data)
</code></pre>
<p>Like RichieHindle said, there is a hidden loop in the implementation of <code>any</code> (although I think it break... | 34 | 2009-07-20T21:47:27Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,145 | <pre><code>>>> the_list =[ ['a','b'], ['a','c'], ['b''d'] ]
>>> any('c' == x[1] for x in the_list)
True
</code></pre>
| 13 | 2009-07-20T21:47:41Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,582 | <pre><code>>>> the_list =[ ['a','b'], ['a','c'], ['b','d'] ]
>>> "b" in zip(*the_list)[1]
True
</code></pre>
<p><code>zip()</code> takes a bunch of lists and groups elements together by index, effectively transposing the list-of-lists matrix. The asterisk takes the contents of <code>the_list</code> a... | 1 | 2009-07-20T23:48:55Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,156,628 | <p>Markus has one way to avoid using the word <code>for</code> -- here's another, which should have much better performance for long <code>the_list</code>s...:</p>
<pre><code>import itertools
found = any(itertools.ifilter(lambda x:x[1]=='b', the_list)
</code></pre>
| 4 | 2009-07-21T00:01:46Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,157,153 | <p>Nothing wrong with using a gen exp, but if the goal is to inline the loop...</p>
<pre><code>>>> import itertools, operator
>>> 'b' in itertools.imap(operator.itemgetter(1), the_list)
True
</code></pre>
<p>Should be the fastest as well.</p>
| 2 | 2009-07-21T03:25:56Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 1,158,136 | <p>the above all look good</p>
<p>but do you want to keep the result?</p>
<p>if so...</p>
<p>you can use the following</p>
<pre><code>result = [element for element in data if element[1] == search]
</code></pre>
<p>then a simple</p>
<pre><code>len(result)
</code></pre>
<p>lets you know if anything was found (and ... | 9 | 2009-07-21T09:25:00Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 10,907,447 | <p>This Python demonstrates locating an element in your list of lists:</p>
<pre><code>def finder(element):
L=[ ['a', 'b'], ['a', 'c'], ['b', 'd'] ]
for x in L:
if element in x:
print "find in ", x
else:
print "not find"
finder('d')
</code></pre>
| 0 | 2012-06-06T02:27:37Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 13,588,121 | <p>What about:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
search = 'b'
filter(lambda x:x[1]==search,list)
</code></pre>
<p>This will return each list in the list of lists with the second element being equal to search. </p>
| 4 | 2012-11-27T16:00:37Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 16,757,108 | <p>Given below is a simple way to find exactly where in the list the item is.</p>
<pre><code>for i in range (0,len(a)):
sublist=a[i]
for i in range(0,len(sublist)):
if search==sublist[i]:
print "found in sublist "+ "a"+str(i)
</code></pre>
| -1 | 2013-05-26T07:33:42Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 18,412,156 | <p>k old post but no one use list expression to answer :P</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
Search = 'c'
# return if it find in either item 0 or item 1
print [x for x,y in list if x == Search or y == Search]
# return if it find in item 1
print [x for x,y in list if y == Search]
</code></pre>
| 2 | 2013-08-23T21:28:45Z | [
"python",
"list"
] |
Python search in lists of lists | 1,156,087 | <p>I have a list of two-item lists and need to search for things in it.</p>
<p>If the list is:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
</code></pre>
<p>I can search for a pair easily by doing</p>
<pre><code>['a','b'] in list
</code></pre>
<p>Now, is there a way to see if I have a pair in which a s... | 28 | 2009-07-20T21:34:26Z | 30,449,870 | <p>I think using nested list comprehensions is the most elegant way to solve this, because the intermediate result is the position where the element is. An implementation would be:</p>
<pre><code>list =[ ['a','b'], ['a','c'], ['b','d'] ]
search = 'c'
any([ (list.index(x),x.index(y)) for x in list for y in x if y == se... | 0 | 2015-05-26T05:06:08Z | [
"python",
"list"
] |
Algorithm for BFS traveral of an acylic directed graph | 1,156,175 | <p>I'm looking for an elegant Python program that does a BFS traveral of a DAG:</p>
<p>Node A is connected to B (<code>A->B</code>) if A "depends on" B (think of python package Foo "depending upon" Bar: Foo->Bar). </p>
<p>In a graph of about 7000 such nodes, I want to sort all nodes such that for all possible <cod... | 1 | 2009-07-20T21:54:22Z | 1,156,213 | <p>If I am reading the question correctly, it looks like you want a <a href="http://en.wikipedia.org/wiki/Topological%5Fsorting" rel="nofollow">topological sort</a>. The most efficient algorithm (O(V+E)) for doing this was proposed by <a href="http://en.wikipedia.org/wiki/Robert%5FTarjan" rel="nofollow">Tarjan</a>, an... | 5 | 2009-07-20T22:03:17Z | [
"python",
"algorithm",
"graph",
"dependencies",
"traversal"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 1,156,490 | <p>A "download" is simply an HTTP header change.</p>
<p>See <a href="http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment">http://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment</a> f... | 62 | 2009-07-20T23:20:15Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 1,158,244 | <p>Django recommend that you use another server to serve static media (another server running on the same machine is fine.) They recommend the use of such servers as <a href="http://www.lighttpd.net/" rel="nofollow">lighttp</a>. </p>
<p>This is very simple to set up. However. if 'somefile.txt' is generated on request ... | 1 | 2009-07-21T09:49:56Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 1,158,750 | <p>For the "best of both worlds" you could combine S.Lott's solution with the <a href="http://www.google.com.ng/search?q=mod_xsendfile&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a">xsendfile module</a>: django generates the path to the file (or the file itself), but the act... | 126 | 2009-07-21T11:57:59Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 4,565,501 | <p>S.Lott has the "good"/simple solution, and elo80ka has the "best"/efficient solution. Here is a middle "better"/middle solution - no server setup, but more efficient for large files than the naive fix.</p>
<p><a href="http://djangosnippets.org/snippets/365/">http://djangosnippets.org/snippets/365/</a></p>
<p>Basi... | 24 | 2010-12-30T19:17:20Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 5,301,921 | <p>It was mentioned above that the mod_xsendfile method does not allow for non-ASCII characters in filenames.</p>
<p>For this reason, I have a patch available for mod_xsendfile that will allow any file to be sent, as long as the name is url encoded, and the additional header:</p>
<pre><code>X-SendFile-Encoding: url
<... | 11 | 2011-03-14T17:10:18Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 5,659,486 | <p>Try: <a href="https://pypi.python.org/pypi/django-sendfile/" rel="nofollow">https://pypi.python.org/pypi/django-sendfile/</a></p>
<p>"Abstraction to offload file uploads to web-server (e.g. Apache with mod_xsendfile) once Django has checked permissions etc."</p>
| 5 | 2011-04-14T06:37:37Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 8,846,697 | <p>Another project to have a look at: <a href="http://readthedocs.org/docs/django-private-files/en/latest/usage.html" rel="nofollow">http://readthedocs.org/docs/django-private-files/en/latest/usage.html</a>
Looks promissing, haven't tested it myself yet tho.</p>
<p>Basically the project abstracts the mod_xsendfile con... | 0 | 2012-01-13T06:18:31Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 10,405,065 | <p>I have faced the same problem more then once and so implemented using xsendfile module and auth view decorators the <a href="https://github.com/danielsokolowski/django-filelibrary" rel="nofollow">django-filelibrary</a>. Feel free to use it as inspiration for your own solution. </p>
<p><a href="https://github.com/da... | 0 | 2012-05-01T22:07:51Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 18,985,653 | <p>Tried @Rocketmonkeys solution but downloaded files were being stored as *.bin and given random names. That's not fine of course. Adding another line from @elo80ka solved the problem.<br>
Here is the code I'm using now:</p>
<pre><code>filename = "/home/stackoverflow-addict/private-folder(not-porn)/image.jpg"
wrapper... | 8 | 2013-09-24T15:22:06Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 20,609,767 | <pre><code>def qrcodesave(request):
import urllib2;
url ="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=s&chld=H|0";
opener = urllib2.urlopen(url);
content_type = "application/octet-stream"
response = HttpResponse(opener.read(), content_type=content_type)
response[... | 1 | 2013-12-16T11:26:06Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 21,210,929 | <p>For a very simple <strong>but not efficient or scalable</strong> solution, you can just use the built in django <code>serve</code> view. This is excellent for quick prototypes or one-off work, but as has been mentioned throughout this question, you should use something like apache or nginx in production.</p>
<pre><... | 14 | 2014-01-18T22:54:28Z | [
"python",
"django",
"download"
] |
Having Django serve downloadable files | 1,156,246 | <p>I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded.</p>
<p>For instance, I'd like the URL to be something like this, "http://example.com/download/?f=somefile.txt</p>
<p>And on the server, I know that all downloadable files reside in a folder "/home/... | 155 | 2009-07-20T22:10:30Z | 22,776,886 | <p>Providing protected access to static html folder using <a href="https://github.com/johnsensible/django-sendfile" rel="nofollow">https://github.com/johnsensible/django-sendfile</a>: <a href="https://gist.github.com/iutinvg/9907731" rel="nofollow">https://gist.github.com/iutinvg/9907731</a></p>
| 0 | 2014-04-01T04:35:39Z | [
"python",
"django",
"download"
] |
Import Dynamically Created Python Files | 1,156,356 | <p>I am creating python files through the course of running a python program. I then want to import these files and run functions that were defined within them. The files I am creating are not stored within my path variables and I'd prefer to keep it that way.</p>
<p>Originally I was calling the <code>execFile(<scr... | 2 | 2009-07-20T22:37:22Z | 1,156,382 | <p>Use</p>
<pre><code>m = __import__("File")
</code></pre>
<p>This is essentially the same as doing</p>
<pre><code>import File
m = File
</code></pre>
| 5 | 2009-07-20T22:43:55Z | [
"python",
"import"
] |
Import Dynamically Created Python Files | 1,156,356 | <p>I am creating python files through the course of running a python program. I then want to import these files and run functions that were defined within them. The files I am creating are not stored within my path variables and I'd prefer to keep it that way.</p>
<p>Originally I was calling the <code>execFile(<scr... | 2 | 2009-07-20T22:37:22Z | 1,156,594 | <p>If I understand correctly your remarks to man that the file isn't in <code>sys.path</code> and you'd rather keep it that way, this would still work:</p>
<pre><code>import imp
fileobj, pathname, description = imp.find_module('thefile', 'c:/')
moduleobj = imp.load_module('thefile', fileobj, pathname, description)
fi... | 3 | 2009-07-20T23:51:44Z | [
"python",
"import"
] |
Seeding random in django | 1,156,511 | <p>In a view in django I use <code>random.random()</code>. How often do I have to call <code>random.seed()</code>?
One time for every request?
One time for every season?
One time while the webserver is running?</p>
| 3 | 2009-07-20T23:26:16Z | 1,156,536 | <p>It really depends on what you need the random number for. Use some experimentation to find out if it makes any difference. You should also consider that there is actually a pattern to pseudo-random numbers. Does it make a difference to you if someone can possible guess the next random number? If not, seed it onc... | 0 | 2009-07-20T23:33:25Z | [
"python",
"django",
"random",
"django-views"
] |
Seeding random in django | 1,156,511 | <p>In a view in django I use <code>random.random()</code>. How often do I have to call <code>random.seed()</code>?
One time for every request?
One time for every season?
One time while the webserver is running?</p>
| 3 | 2009-07-20T23:26:16Z | 1,156,541 | <p>Call <code>random.seed()</code> rarely if at all.</p>
<p>To be random, you must allow the random number generator to run without touching the seed. The sequence of numbers is what's random. If you change the seed, you start a new sequence. The seed values may not be very random, leading to problems. </p>
<p>De... | 2 | 2009-07-20T23:34:30Z | [
"python",
"django",
"random",
"django-views"
] |
Seeding random in django | 1,156,511 | <p>In a view in django I use <code>random.random()</code>. How often do I have to call <code>random.seed()</code>?
One time for every request?
One time for every season?
One time while the webserver is running?</p>
| 3 | 2009-07-20T23:26:16Z | 1,157,735 | <p>Don't set the seed.</p>
<p>The only time you want to set the seed is if you want to make sure that the same events keep happening. For example, if you don't want to let players cheat in your game you can save the seed, and then set it when they load their game. Then no matter how many times they save + reload, it s... | 3 | 2009-07-21T07:32:08Z | [
"python",
"django",
"random",
"django-views"
] |
Forced to use inconsistent file import paths in Python (/Django) | 1,156,515 | <p>I've recently been having some problems with my imports in Django (Python)... It's better to explain using a file diagram:</p>
<pre><code>- project/
- application/
- file.py
- application2/
- file2.py
</code></pre>
<p>In <code>project/application/file.py</code> I have the following:</p>
<p... | 4 | 2009-07-20T23:26:43Z | 1,156,579 | <p>For import to find a module, it needs to either be in sys.path. Usually, this includes "", so it searches the current directory. If you load "application" from project, it'll find it, since it's in the current directory.</p>
<p>Okay, that's the obvious stuff. A confusing bit is that Python remembers which module... | 4 | 2009-07-20T23:48:06Z | [
"python",
"django",
"import",
"path"
] |
Forced to use inconsistent file import paths in Python (/Django) | 1,156,515 | <p>I've recently been having some problems with my imports in Django (Python)... It's better to explain using a file diagram:</p>
<pre><code>- project/
- application/
- file.py
- application2/
- file2.py
</code></pre>
<p>In <code>project/application/file.py</code> I have the following:</p>
<p... | 4 | 2009-07-20T23:26:43Z | 1,157,719 | <p>It is better to always import using the same way - say, using <code>project.app.models</code> - because otherwise, you may find your module is imported twice, which sometimes leads to obscure errors as discussed in <a href="http://stackoverflow.com/questions/1149317/signals-registered-more-than-once-in-django1-1-tes... | 0 | 2009-07-21T07:28:29Z | [
"python",
"django",
"import",
"path"
] |
Forced to use inconsistent file import paths in Python (/Django) | 1,156,515 | <p>I've recently been having some problems with my imports in Django (Python)... It's better to explain using a file diagram:</p>
<pre><code>- project/
- application/
- file.py
- application2/
- file2.py
</code></pre>
<p>In <code>project/application/file.py</code> I have the following:</p>
<p... | 4 | 2009-07-20T23:26:43Z | 1,157,777 | <p>If you dig into the django philosophy, you will find, that a project is a collection of apps. Some of these apps could depend on other apps, which is just fine. However, what you always want is to make your apps plug-able so you can move them to a different project and use them there as well. To do this, you need to... | 2 | 2009-07-21T07:44:19Z | [
"python",
"django",
"import",
"path"
] |
How to a query a set of objects and return a set of object specific attribute in SQLachemy/Elixir? | 1,156,962 | <p>Suppose that I have a table like:</p>
<pre><code>class Ticker(Entity):
ticker = Field(String(7))
tsdata = OneToMany('TimeSeriesData')
staticdata = OneToMany('StaticData')
</code></pre>
<p>How would I query it so that it returns a set of <code>Ticker.ticker</code>?</p>
<p>I dig into the doc and seems l... | 0 | 2009-07-21T02:04:40Z | 1,157,012 | <p>Not sure what you're after exactly but to get an array with all 'Ticker.ticker' values you would do this:</p>
<pre><code>[instance.ticker for instance in Ticker.query.all()]
</code></pre>
<p>What you really want is probably the <a href="http://elixir.ematia.de/trac/wiki/TutorialDivingIn" rel="nofollow">Elixir gett... | 0 | 2009-07-21T02:27:32Z | [
"python",
"sql",
"sqlalchemy"
] |
Fast, searchable dict storage for Python | 1,156,993 | <p>Current I use <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> (w/ <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>) to store about 5000 dict objects. Each dict object corresponds to an entry in PyPI with keys - (name, version, summary .. sometimes 'description' can be as big as the proje... | 2 | 2009-07-21T02:16:49Z | 1,157,019 | <p>Did you put indices on name and description? Searching on 5000 indexed entries should be essentially instantaneous (of course ORMs will make your life much harder, as they usually do [even relatively good ones such as SQLAlchemy, but try "raw sqlite" and it absolutely should fly).</p>
<p>Writing just the updated e... | 2 | 2009-07-21T02:29:57Z | [
"python",
"sqlite",
"data-storage"
] |
Fast, searchable dict storage for Python | 1,156,993 | <p>Current I use <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> (w/ <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>) to store about 5000 dict objects. Each dict object corresponds to an entry in PyPI with keys - (name, version, summary .. sometimes 'description' can be as big as the proje... | 2 | 2009-07-21T02:16:49Z | 1,157,087 | <p>It might be overkill for your application, but you ought to check out schema-free/document-oriented databases. Personally I'm a fan of <a href="http://couchdb.apache.org/" rel="nofollow">couchdb</a>. Basically, rather than store records as rows in a table, something like couchdb stores key-value pairs, and then (in ... | 1 | 2009-07-21T03:06:08Z | [
"python",
"sqlite",
"data-storage"
] |
Fast, searchable dict storage for Python | 1,156,993 | <p>Current I use <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> (w/ <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>) to store about 5000 dict objects. Each dict object corresponds to an entry in PyPI with keys - (name, version, summary .. sometimes 'description' can be as big as the proje... | 2 | 2009-07-21T02:16:49Z | 2,373,002 | <p>Given the approximate number of objects stated (around 5,000), SQLite is probably not the problem behind speed. It's the intermediary measures; for example JSON or possibly non-optimal use of SQLAlChemy.</p>
<p>Try this out (fairly fast even for million objects):
y_serial.py module :: warehouse Python objects with ... | 0 | 2010-03-03T16:27:50Z | [
"python",
"sqlite",
"data-storage"
] |
Fast, searchable dict storage for Python | 1,156,993 | <p>Current I use <a href="http://www.sqlite.org/" rel="nofollow">SQLite</a> (w/ <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>) to store about 5000 dict objects. Each dict object corresponds to an entry in PyPI with keys - (name, version, summary .. sometimes 'description' can be as big as the proje... | 2 | 2009-07-21T02:16:49Z | 4,468,860 | <p>I'm solving a very similar problem for myself right now, using <a href="http://nucular.sourceforge.net/index.html" rel="nofollow">Nucular</a>, which might suit your needs. It's a file-system based storage and seems very fast indeed. (It comes with an example app that <a href="http://aaron.oirt.rutgers.edu/cgi-bin/nu... | 0 | 2010-12-17T08:41:18Z | [
"python",
"sqlite",
"data-storage"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 1,157,132 | <p>You can use a list comprehension:</p>
<pre><code>def remove_values_from_list(the_list, val):
return [value for value in the_list if value != val]
x = [1, 2, 3, 4, 2, 2, 3]
x = remove_values_from_list(x, 2)
print x
# [1, 3, 4, 3]
</code></pre>
| 114 | 2009-07-21T03:17:58Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 1,157,160 | <p>Functional approach:</p>
<p><strong>2.x</strong></p>
<pre><code>>>> x = [1,2,3,2,2,2,3,4]
>>> filter(lambda a: a != 2, x)
[1, 3, 3, 4]
</code></pre>
<p><strong>3.x</strong></p>
<pre><code>>>> list(filter((2).__ne__, x))
[1, 3, 3, 4]
</code></pre>
| 223 | 2009-07-21T03:28:39Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 1,157,174 | <p>You can use slice assignment if the original list must be modified, while still using an efficient list comprehension (or generator expression).</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> x[:] = (value for value in x if value != 2)
>>> x
[1, 3, 4, 3]
</code></pre>
| 64 | 2009-07-21T03:33:13Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 4,618,920 | <p>I believe this is probably faster than any other way if you don't care about the lists order, if you do take care about the final order store the indexes from the original and resort by that.</p>
<pre><code>category_ids.sort()
ones_last_index = category_ids.count('1')
del category_ids[0:ones_last_index]
</code></pr... | 1 | 2011-01-06T19:27:17Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 10,978,459 | <p>At the cost of readability, I think this version is slightly faster as it doesn't force the while to reexamine the list, thus doing exactly the same work remove has to do anyway:</p>
<pre><code>x = [1, 2, 3, 4, 2, 2, 3]
def remove_values_from_list(the_list, val):
for i in range(the_list.count(val)):
the... | 3 | 2012-06-11T10:32:47Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 14,658,399 | <p>To remove all duplicate occurrences and leave one in the list:</p>
<pre><code>test = [1, 1, 2, 3]
newlist = list(set(test))
print newlist
[1, 2, 3]
</code></pre>
<p>Here is the function I've used for Project Euler:</p>
<pre><code>def removeOccurrences(e):
return list(set(e))
</code></pre>
| 3 | 2013-02-02T03:52:04Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 24,577,941 | <p>All of the answers above (apart from Martin Andersson's) create a new list without the desired items, rather than removing the items from the original list. </p>
<pre><code>>>> import random, timeit
>>> a = list(range(5)) * 1000
>>> random.shuffle(a)
>>> b = a
>>> print... | 4 | 2014-07-04T16:04:21Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 26,419,313 | <p>Repeating the solution of the first post in a more abstract way:</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> while 2 in x: x.remove(2)
>>> x
[1, 3, 4, 3]
</code></pre>
| 9 | 2014-10-17T06:42:03Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 31,132,147 | <pre><code>p=[2,3,4,4,4]
p.clear()
print(p)
[]
</code></pre>
<p>Only with Python 3</p>
| -3 | 2015-06-30T07:29:32Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 36,342,652 | <h1>Remove all occurrences of a value from a Python list</h1>
<pre><code>lists = [6.9,7,8.9,3,5,4.9,1,2.9,7,9,12.9,10.9,11,7]
def remove_values_from_list():
for list in lists:
if(list!=7):
print(list)
remove_values_from_list()
</code></pre>
<p>"""
Result:
6.9
8.9
3
5
4.9
1
2.9
9
12.9
10.9
11
"""</p... | 0 | 2016-03-31T20:01:01Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 37,628,392 | <p>Numpy approach and timings against a list/array with 1.000.000 elements:</p>
<p>Timings:</p>
<pre><code>In [10]: a.shape
Out[10]: (1000000,)
In [13]: len(lst)
Out[13]: 1000000
In [18]: %timeit a[a != 2]
100 loops, best of 3: 2.94 ms per loop
In [19]: %timeit [x for x in lst if x != 2]
10 loops, best of 3: 79.7 ... | 1 | 2016-06-04T09:01:52Z | [
"python",
"list"
] |
Remove all occurrences of a value from a Python list | 1,157,106 | <p>In Python <code>remove()</code> will remove the first occurrence of value in a list.</p>
<p>How to remove all occurrences of a value from a list, without sorting the list?</p>
<p>This is what I have in mind.</p>
<pre><code>>>> x = [1, 2, 3, 4, 2, 2, 3]
>>> def remove_values_from_list(the_list, v... | 167 | 2009-07-21T03:12:10Z | 38,259,872 | <p>you can do this</p>
<pre><code>while 2 in x:
x.remove(2)
</code></pre>
| -1 | 2016-07-08T06:02:21Z | [
"python",
"list"
] |
How to update an older C extension for Python 2.x to Python 3.x | 1,157,134 | <p>I'm wanting to use an extension for Python that I found <a href="http://effbot.org/zone/console-index.htm" rel="nofollow">here</a>, but I'm using Python 3.1 and when I attempt to compile the C extension included in the package (_wincon), it does not compile due to all the syntax errors. Unfortunately, it was written... | 2 | 2009-07-21T03:18:46Z | 1,157,170 | <p>I don't believe there is any magic bullet to make the C sources for a Python extension coded for some old-ish version of Python 2, into valid C sources for one coded for Python 3 -- it takes understanding of C and of how the C API has changed, and of what exactly the extension is doing in each part of its code. Beli... | 7 | 2009-07-21T03:31:25Z | [
"python",
"c",
"windows",
"console"
] |
Creating a board game simulator (Python?) (Pygame?) | 1,157,245 | <p>I've decided to start working on programming an old favorite of mine. I've never done a game before and also never done a large project in Python.</p>
<p>The game is the old Avalon Hill game <a href="http://www.boardgamegeek.com/boardgame/2808" rel="nofollow">Russian Campaign</a></p>
<p>I've been playing with PyGa... | 14 | 2009-07-21T04:03:33Z | 1,157,289 | <p>Separate the "back-end" engine (which keeps track of board state, receives move orders from front-ends, generates random numbers to resolve battles, sends updates to front-ends, deals with saving and restoring specific games, ...) from "front-end" ones, which basically supply user interfaces for all of this.</p>
<p... | 25 | 2009-07-21T04:18:01Z | [
"python",
"pygame"
] |
Creating a board game simulator (Python?) (Pygame?) | 1,157,245 | <p>I've decided to start working on programming an old favorite of mine. I've never done a game before and also never done a large project in Python.</p>
<p>The game is the old Avalon Hill game <a href="http://www.boardgamegeek.com/boardgame/2808" rel="nofollow">Russian Campaign</a></p>
<p>I've been playing with PyGa... | 14 | 2009-07-21T04:03:33Z | 1,160,754 | <p>I don't think you should care about multiple-plateforms support, separation of front-ends and back-ends, multiple processes with communication using XML-RPC and JSON, server, etc.</p>
<p>Drop your bonuses and concentrate on your main idea : a turn-based, two players game. It's your first game so you'll have a lot t... | 2 | 2009-07-21T18:04:42Z | [
"python",
"pygame"
] |
Pygame and blitting: white on white = gray? | 1,157,385 | <p>I'm using pygame (1.9.0rc3, though this also happens in 1.8.1) to create a heatmap. To build the heatmap, I use a small, 24-bit 11x11px dot PNG image with a white background and a very low-opacity grey dot that stops exactly at the edges:</p>
<p><img src="http://img442.imageshack.us/img442/465/dot.png" alt="Dot ima... | 5 | 2009-07-21T05:02:52Z | 1,157,753 | <p>After trying around, the only thing I could see was that you're 100% right. Multiplication by 255 results in a subtraction of 1 -- every time. In the end, I downloaded the pygame source code, and the answer is right there, in <code>surface.h</code>:</p>
<pre><code>#define BLEND_MULT(sR, sG, sB, sA, dR, dG, dB, dA) ... | 6 | 2009-07-21T07:37:34Z | [
"python",
"image",
"pygame",
"imaging"
] |
Pygame and blitting: white on white = gray? | 1,157,385 | <p>I'm using pygame (1.9.0rc3, though this also happens in 1.8.1) to create a heatmap. To build the heatmap, I use a small, 24-bit 11x11px dot PNG image with a white background and a very low-opacity grey dot that stops exactly at the edges:</p>
<p><img src="http://img442.imageshack.us/img442/465/dot.png" alt="Dot ima... | 5 | 2009-07-21T05:02:52Z | 1,710,620 | <p>Also encountered this problem doing heatmaps and after reading balpha's answer, chose to fix it the "right" (if slower) way. Change the various</p>
<pre><code>(s * d) >> 8
</code></pre>
<p>to</p>
<pre><code>(s * d) / 255
</code></pre>
<p>This required patching multiply functions in <code>alphablit.c</code... | 1 | 2009-11-10T19:38:30Z | [
"python",
"image",
"pygame",
"imaging"
] |
python function for retrieving key and encryption | 1,157,442 | <p>M2Crypto package is not showing the 'recipient_public_key.pem' file at linux terminal.</p>
<p>How do I get/connect with recipient public key.</p>
<p>Exactly, I need to check how can I open this file through linux commands.</p>
<pre><code>import M2Crypto
def encrypt():
recip = M2Crypto.RSA.load_pub_key(open('r... | 0 | 2009-07-21T05:27:51Z | 1,157,537 | <p>I have never used M2Crypto, but according to the <a href="http://www.heikkitoivonen.net/m2crypto/api/M2Crypto.RSA-module.html#load_pub_key" rel="nofollow">API documentation</a>, <code>load_pub_key</code> expects the file name as the argument, not the key itself. Try</p>
<pre><code>recip = M2Crypto.RSA.load_pub_key(... | 1 | 2009-07-21T06:19:04Z | [
"python"
] |
Django Double Escaping Quotes etc | 1,157,725 | <p>I'm experiencing what I would consider somewhat strange behavior. Specifically if I have a string like this:</p>
<blockquote>
<p><b>1984: Curriculum Unit</b><br />
by Donald R. Hogue, Center for Learning, George Orwell<br />
<br />
"A center for learning publication"--Cover.
</p>
</blockqu... | 1 | 2009-07-21T07:30:20Z | 1,157,755 | <p>You shouldn't have to think about escaping in 1.0 . If you have a template</p>
<pre><code><html>
<body>
& == &amp; in HTML
</body>
</html>
</code></pre>
<p>It should encode the <code>&</code> to <code>&amp;</code> before printing. </p>
<p>If you have a variable</p>
<pr... | 1 | 2009-07-21T07:37:57Z | [
"python",
"django"
] |
Django Double Escaping Quotes etc | 1,157,725 | <p>I'm experiencing what I would consider somewhat strange behavior. Specifically if I have a string like this:</p>
<blockquote>
<p><b>1984: Curriculum Unit</b><br />
by Donald R. Hogue, Center for Learning, George Orwell<br />
<br />
"A center for learning publication"--Cover.
</p>
</blockqu... | 1 | 2009-07-21T07:30:20Z | 1,157,891 | <p>Oh hardy har har,</p>
<p>Silly me, Google is so smart that they already escaped those chars in the XML I was parsing. Wouldn't you know it, an hour of fiddling only to realize Google outsmarted me again!</p>
<p>P.S. In case anyone else ever comes across a similar problem, I'm specifically referring to the XML retu... | 1 | 2009-07-21T08:12:03Z | [
"python",
"django"
] |
Problem with recursive search of xml document using python | 1,157,768 | <p>I am trying to write a function that will take an xml object, an arbitrary number of tags, defined by tuples containing a tag name, attribute and attribute value (e.g ('tag1', 'id', '1')) and return the most specific node possible. My code is below:</p>
<pre><code>from xml.dom import minidom
def _search(object, *... | 0 | 2009-07-21T07:41:22Z | 1,157,787 | <p>Shouldn't you be inserting a <strong>return</strong> in front of your recursive calls to <code>_search</code>? The way you have it now, some exit paths from <code>_search</code> don't have a <code>return</code> statement, so they will return <code>None</code> - which leads to the exception you're seeing.</p>
| 2 | 2009-07-21T07:46:15Z | [
"python",
"xml"
] |
Problem with recursive search of xml document using python | 1,157,768 | <p>I am trying to write a function that will take an xml object, an arbitrary number of tags, defined by tuples containing a tag name, attribute and attribute value (e.g ('tag1', 'id', '1')) and return the most specific node possible. My code is below:</p>
<pre><code>from xml.dom import minidom
def _search(object, *... | 0 | 2009-07-21T07:41:22Z | 1,157,881 | <p>I assume you're using <a href="http://www.eggheadcafe.com/community/aspnet/17/10084853/xml-viewer.aspx" rel="nofollow">http://www.eggheadcafe.com/community/aspnet/17/10084853/xml-viewer.aspx</a> as your sample data... </p>
<p>As <a href="http://stackoverflow.com/questions/1157768/problem-with-recursive-search-of-xm... | 2 | 2009-07-21T08:09:54Z | [
"python",
"xml"
] |
Qt GraphicsScene constantly redrawing | 1,157,773 | <p>I've written my own implementation of <code>QGraphicsView.drawItems()</code>, to fit the needs of my application. The method as such works fine, however, it is called repeatedly, even if it does not need to be redrawn. This causes the application to max out processor utilization.<br />
Do I need to somehow signal th... | 0 | 2009-07-21T07:42:49Z | 1,158,538 | <p>I think this causes the problem:</p>
<pre><code>item.setPen(markupColors[drawable.source])
</code></pre>
<p>If you take a look at the source code:</p>
<pre><code>void QAbstractGraphicsShapeItem::setPen(const QPen &pen)
{
Q_D(QAbstractGraphicsShapeItem);
prepareGeometryChange();
d->pen = pen;
... | 0 | 2009-07-21T11:06:25Z | [
"python",
"qt"
] |
Creating DateTime from user inputted date | 1,157,794 | <p>I'm pretty new to Python and App Engine, but what I'm trying to do is store a model which contains a DateProperty, and that DateProperty is populated with a Date entered by the user in a web form.</p>
<p>I've got the model of:</p>
<pre><code>class Memory(db.Model):
author = db.UserProperty()
content = db.S... | 0 | 2009-07-21T07:47:48Z | 1,157,838 | <p>You were right with strptime:</p>
<pre><code>>>> dt = time.strptime('2009-07-21', '%Y-%m-%d')
>>> dt
time.struct_time(tm_year=2009, tm_mon=7, tm_mday=21, tm_hour=0, tm_min=0, tm_sec
=0, tm_wday=1, tm_yday=202, tm_isdst=-1)
>>>
</code></pre>
<p>You got struct that can be used by o... | 0 | 2009-07-21T07:59:57Z | [
"python",
"datetime"
] |
Creating DateTime from user inputted date | 1,157,794 | <p>I'm pretty new to Python and App Engine, but what I'm trying to do is store a model which contains a DateProperty, and that DateProperty is populated with a Date entered by the user in a web form.</p>
<p>I've got the model of:</p>
<pre><code>class Memory(db.Model):
author = db.UserProperty()
content = db.S... | 0 | 2009-07-21T07:47:48Z | 1,157,860 | <p>Yeah, PHP's is much nicer. I'm using this library </p>
<p><a href="http://labix.org/python-dateutil" rel="nofollow">http://labix.org/python-dateutil</a></p>
<pre><code>>>> import dateutil.parser
>>> dateutil.parser.parse("may 2 1984")
datetime.datetime(1984, 5, 2, 0, 0)
</code></pre>
| 0 | 2009-07-21T08:04:07Z | [
"python",
"datetime"
] |
Creating DateTime from user inputted date | 1,157,794 | <p>I'm pretty new to Python and App Engine, but what I'm trying to do is store a model which contains a DateProperty, and that DateProperty is populated with a Date entered by the user in a web form.</p>
<p>I've got the model of:</p>
<pre><code>class Memory(db.Model):
author = db.UserProperty()
content = db.S... | 0 | 2009-07-21T07:47:48Z | 1,158,002 | <p>perhaps you are using the wrong class? I'm not sure what type your model should take</p>
<p>but try:</p>
<pre><code>from datetime import datetime
myValue = datetime.strptime(self.request.get('date'), '%Y-%m-%d')
</code></pre>
<p>I use a datetime object in MySQL with MySQLdb (and a datetime field)</p>
<p>...like... | 0 | 2009-07-21T08:45:38Z | [
"python",
"datetime"
] |
get group id by group name (Python, Unix) | 1,157,864 | <p>I want to use Python to get the group id to a corresponding group name.
The routine must work for Unix-like OS (Linux and Mac OS X).</p>
<p>This is what I found so far</p>
<pre><code>>>> import grp
>>> for g in grp.getgrall():
... if g[0] == 'wurzel':
... print g[2]
</code></pre>
| 7 | 2009-07-21T08:05:05Z | 1,157,897 | <p>See <a href="http://docs.python.org/library/grp.html#grp.getgrnam" rel="nofollow"><code>grp.getgrnam(name)</code></a>:</p>
<blockquote>
<p><code>grp.getgrnam(name)</code></p>
<p>Return the group database entry for the given group name. KeyError is raised if the entry asked for cannot be found.</p>
<p>Gr... | 5 | 2009-07-21T08:13:08Z | [
"python",
"linux",
"unix"
] |
get group id by group name (Python, Unix) | 1,157,864 | <p>I want to use Python to get the group id to a corresponding group name.
The routine must work for Unix-like OS (Linux and Mac OS X).</p>
<p>This is what I found so far</p>
<pre><code>>>> import grp
>>> for g in grp.getgrall():
... if g[0] == 'wurzel':
... print g[2]
</code></pre>
| 7 | 2009-07-21T08:05:05Z | 1,157,958 | <p>If you read the <a href="http://docs.python.org/library/grp.html">grp module documentation</a> you'll see that grp.getgrnam(groupname) will return one entry from the group database, which is a tuple-like object. You can either access the information by index or by attribute:</p>
<pre><code>>>> import grp
&... | 14 | 2009-07-21T08:34:41Z | [
"python",
"linux",
"unix"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 1,158,096 | <pre><code>def touch(fname):
if os.path.exists(fname):
os.utime(fname, None)
else:
open(fname, 'a').close()
</code></pre>
| 23 | 2009-07-21T09:11:18Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 1,158,388 | <p>Simplistic:</p>
<pre><code>def touch(fname):
open(fname, 'a').close()
os.utime(fname, None)
</code></pre>
<ul>
<li>The <code>open</code> ensures there is a file there</li>
<li>the <code>utime</code> ensures that the timestamps are updated</li>
</ul>
<p>Theoretically, it's possible someone will delete the ... | 2 | 2009-07-21T10:30:54Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 1,158,471 | <p>Complex (possibly buggy):</p>
<pre><code>def utime(fname, atime=None, mtime=None)
if type(atime) is tuple:
atime, mtime = atime
if atime is None or mtime is None:
statinfo = os.stat(fname)
if atime is None:
atime = statinfo.st_atime
if mtime is None:
... | 0 | 2009-07-21T10:49:18Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 1,160,227 | <p>This tries to be a little more race-free than the other solutions. (The <code>with</code> keyword is new in Python 2.5.)</p>
<pre><code>import os
def touch(fname, times=None):
with open(fname, 'a'):
os.utime(fname, times)
</code></pre>
<p>Roughly equivalent to this.</p>
<pre><code>import os
def touch... | 159 | 2009-07-21T16:17:59Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 6,222,692 | <p>Why not try this?:</p>
<pre><code>def touch(fname):
try:
os.utime(fname, None)
except:
open(fname, 'a').close()
</code></pre>
<p>I believe this eliminates any race condition that matters. If the file does not exist then an exception will be thrown.</p>
<p>The only possible race condition ... | 14 | 2011-06-03T03:31:28Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 8,860,875 | <p>Here's some code that uses ctypes (only tested on Linux):</p>
<pre><code>from ctypes import *
libc = CDLL("libc.so.6")
# struct timespec {
# time_t tv_sec; /* seconds */
# long tv_nsec; /* nanoseconds */
# };
# int futimens(int fd, const struct timespec times[2]);
c... | 6 | 2012-01-14T07:44:18Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 11,249,346 | <p>It might seem logical to create a string with the desired variables, and pass it to os.system:</p>
<pre><code>touch = 'touch ' + dir + '/' + fileName
os.system(touch)
</code></pre>
<p>This is inadequate in a number of ways (e.g.,it doesn't handle whitespace), so don't do it. </p>
<p>A more robust method is to us... | 0 | 2012-06-28T16:49:38Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 33,577,626 | <pre><code>with open(file_name,'a') as f:
pass
</code></pre>
| 0 | 2015-11-07T00:20:57Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 34,603,829 | <p>Looks like this is new as of Python 3.4 - <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path.touch"><code>pathlib</code></a>.</p>
<pre><code>from pathlib import Path
Path('path/to/file.txt').touch()
</code></pre>
<p>This will create a <code>file.txt</code> at the path.</p>
<p>--</p>
<blockquot... | 20 | 2016-01-05T03:45:06Z | [
"python",
"utility"
] |
Implement touch using Python? | 1,158,076 | <p><code>touch</code> is a Unix utility that sets the modification and access times of files to the current time of day. If the file doesn't exist, it is created with default permissions.</p>
<p>How would you implement it as a Python function? Try to be cross platform and complete.</p>
<p>(Current Google results for... | 160 | 2009-07-21T09:05:11Z | 37,978,434 | <p>"open(file_name, 'a').close()" did not work for me in Python 2.7 on Windows. "os.utime(file_name, None)" worked just fine.</p>
<p>Also, I had a need to recursively touch all files in a directory with a date older than some date. I created hte following based on ephemient's very helpful response. </p>
<pre><cod... | 0 | 2016-06-22T21:19:35Z | [
"python",
"utility"
] |
python - Importing a file that is a symbolic link | 1,158,108 | <p>If I have files x.py and y.py . And y.py is the link(symbolic or hard) of x.py .</p>
<p>If I import both the modules in my script. Will it import it once or it assumes both are different files and import it twice.</p>
<p>What it does exactly?</p>
| 7 | 2009-07-21T09:15:35Z | 1,158,116 | <p>Python will import it twice.</p>
<p>A link is a file system concept. To the Python interpreter, <code>x.py</code> and <code>y.py</code> are two different modules.</p>
<pre>
$ echo print \"importing \" + __file__ > x.py
$ ln -s x.py y.py
$ python -c "import x; import y"
importing x.py
importing y.py
$ python -c "im... | 9 | 2009-07-21T09:18:05Z | [
"python",
"testing",
"import"
] |
python - Importing a file that is a symbolic link | 1,158,108 | <p>If I have files x.py and y.py . And y.py is the link(symbolic or hard) of x.py .</p>
<p>If I import both the modules in my script. Will it import it once or it assumes both are different files and import it twice.</p>
<p>What it does exactly?</p>
| 7 | 2009-07-21T09:15:35Z | 1,948,735 | <p>You only have to be careful in the case where your script itself is a symbolic link, in which case the first entry of sys.path will be the directory containing the target of the link.</p>
| 10 | 2009-12-22T19:54:49Z | [
"python",
"testing",
"import"
] |
Merge sorted lists in python | 1,158,128 | <p>I have a bunch of sorted lists of objects, and a comparison function</p>
<pre><code>class Obj :
def __init__(p) :
self.points = p
def cmp(a, b) :
return a.points < b.points
a = [Obj(1), Obj(3), Obj(8), ...]
b = [Obj(1), Obj(2), Obj(3), ...]
c = [Obj(100), Obj(300), Obj(800), ...]
result = magic... | 10 | 2009-07-21T09:21:00Z | 1,158,150 | <p>I don't know whether it would be any quicker, but you could simplify it with:</p>
<pre><code>def GetObjKey(a):
return a.points
return sorted(a + b + c, key=GetObjKey)
</code></pre>
<p>You could also, of course, use <code>cmp</code> rather than <code>key</code> if you prefer.</p>
| 0 | 2009-07-21T09:27:55Z | [
"python",
"arrays",
"merge",
"sorting"
] |
Merge sorted lists in python | 1,158,128 | <p>I have a bunch of sorted lists of objects, and a comparison function</p>
<pre><code>class Obj :
def __init__(p) :
self.points = p
def cmp(a, b) :
return a.points < b.points
a = [Obj(1), Obj(3), Obj(8), ...]
b = [Obj(1), Obj(2), Obj(3), ...]
c = [Obj(100), Obj(300), Obj(800), ...]
result = magic... | 10 | 2009-07-21T09:21:00Z | 1,158,186 | <p>Use the <a href="http://docs.python.org/library/bisect.html" rel="nofollow"><code>bisect</code></a> module. From the documentation: "This module provides support for maintaining a list in sorted order without having to sort the list after each insertion."</p>
<pre><code>import bisect
def magic(*args):
r = []
... | 1 | 2009-07-21T09:37:02Z | [
"python",
"arrays",
"merge",
"sorting"
] |
Merge sorted lists in python | 1,158,128 | <p>I have a bunch of sorted lists of objects, and a comparison function</p>
<pre><code>class Obj :
def __init__(p) :
self.points = p
def cmp(a, b) :
return a.points < b.points
a = [Obj(1), Obj(3), Obj(8), ...]
b = [Obj(1), Obj(2), Obj(3), ...]
c = [Obj(100), Obj(300), Obj(800), ...]
result = magic... | 10 | 2009-07-21T09:21:00Z | 1,158,196 | <p>Python standard library offers a method for it: <a href="http://docs.python.org/library/heapq.html#heapq.merge"><code>heapq.merge</code></a>.<br />
As the documentation says, it is very similar to using <a href="http://docs.python.org/library/itertools.html#itertools.chain">itertools</a> (but with more limitations);... | 12 | 2009-07-21T09:38:03Z | [
"python",
"arrays",
"merge",
"sorting"
] |
Merge sorted lists in python | 1,158,128 | <p>I have a bunch of sorted lists of objects, and a comparison function</p>
<pre><code>class Obj :
def __init__(p) :
self.points = p
def cmp(a, b) :
return a.points < b.points
a = [Obj(1), Obj(3), Obj(8), ...]
b = [Obj(1), Obj(2), Obj(3), ...]
c = [Obj(100), Obj(300), Obj(800), ...]
result = magic... | 10 | 2009-07-21T09:21:00Z | 1,158,209 | <p>Instead of using a list, you can use a <a href="http://en.wikipedia.org/wiki/Heap%5F%28data%5Fstructure" rel="nofollow">heap</a>.</p>
<p>The insertion is O(log(n)), so merging a, b and c will be O(n log(n))</p>
<p>In Python, you can use the <a href="http://docs.python.org/library/heapq.html" rel="nofollow"><code>h... | 2 | 2009-07-21T09:40:30Z | [
"python",
"arrays",
"merge",
"sorting"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.