> Replace te **start line with something like:
>
>
>
> object_data.append([])
>
> i += 1
>
>
>
> This assumes a few missing lines, which must have been there or you
>
> would have already had runtime errors. For example, you'll need i=0
>
> before the loop.
>
>
On 07/09/2013 09:30 AM, alex.ha...@gmail.com wrote:
Hello!
I'm new here and fairly new to Python. I am attempting to read a data file into
python and adding it to a 2D list to make it easy to use further down the line.
My data file is just 7 numbers in a row seperated by commas and each
Hello!
I'm new here and fairly new to Python. I am attempting to read a data file into
python and adding it to a 2D list to make it easy to use further down the line.
My data file is just 7 numbers in a row seperated by commas and each bulk of
data is seperated by the sign @ to indicate
, 30, 31, 32, 33, 34]]
> > >>> y[1:5:2][::3]
> > [[7, 8, 9, 10, 11, 12, 13]]
> > I expected the 2D list:[[ 7, 10, 13],
> > [21, 24, 27]]
> >
> > Any ideas?
> >
> > Thanks,
> > Rob
> > PS: I used Python 2.7.3
> >
>
> The
, 23, 24, 25, 26, 27], [28, >
29, 30, 31, 32, 33, 34]]
> >>> y[1:5:2][::3]
> [[7, 8, 9, 10, 11, 12, 13]]
> I expected the 2D list:[[ 7, 10, 13],
> [21, 24, 27]]
>
> Any ideas?
>
> Thanks,
> Rob
> PS: I used Python 2.7.3
>
The explanation is rather si
25, 26, 27],
> [28, 29, 30, 31, 32, 33, 34]]
> >>> y[1:5:2][::3]
> [[7, 8, 9, 10, 11, 12, 13]]
> I expected the 2D list:
> [[ 7, 10, 13],
> [21, 24, 27]]
> Any ideas?
y is just a list. It happens to be a list of lists, but that doesn't make
it a "2D&qu
25, 26, 27],
> [28, 29, 30, 31, 32, 33, 34]]
>
>>>> y[1:5:2][::3]
> [[7, 8, 9, 10, 11, 12, 13]]
>
> I expected the 2D list:
> [[ 7, 10, 13],
> [21, 24, 27]]
>
> Any ideas?
It is not really a 2D list; rather a list of lists. You cannot see the two
slic
, 8, 9, 10, 11, 12, 13]]
I expected the 2D list:
[[ 7, 10, 13],
[21, 24, 27]]
Any ideas?
Thanks,
Rob
PS: I used Python 2.7.3
--
http://mail.python.org/mailman/listinfo/python-list
On Mon, Oct 11, 2010 at 1:44 PM, Tom Pacheco wrote:
> your creating a 1d list
>
> this creates a 2d list
> a=[[[]]*5
>
>
> >>> [0]*5
> [0, 0, 0, 0, 0]
> >>> [[]]*5
> [[], [], [], [], []]
>
Don't do this. This actually just creat
ibuteError: 'int' object has no attribute 'append'
your creating a 1d list
this creates a 2d list
a=[[[]]*5
>>> [0]*5
[0, 0, 0, 0, 0]
>>> [[]]*5
[[], [], [], [], []]
- tom
--
http://mail.python.org/mailman/listinfo/python-list
On 10/11/2010 09:24 AM, Fasihul Kabir wrote:
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above codes show the following error. and how to overcome it.
Traceback (most recent call last):
File "", line 3, in
a[i].append(j)
AttributeError: 'int'
On Mon, Oct 11, 2010 at 9:24 AM, Fasihul Kabir wrote:
> a = [0]*5
> for i in range(0, 4):
> for j in range(0, i):
> a[i].append(j)
>
> why the above codes show the following error. and how to overcome it.
>
> Traceback (most recent call last):
> File "", line 3, in
> a[i].appen
the declaration is wrong
if you want to create a two dimensional array try to use functions like
arange and reshape
On Mon, Oct 11, 2010 at 9:54 PM, Fasihul Kabir wrote:
> a = [0]*5
> for i in range(0, 4):
> for j in range(0, i):
> a[i].append(j)
>
> why the above codes show the fo
a = [0]*5
for i in range(0, 4):
for j in range(0, i):
a[i].append(j)
why the above codes show the following error. and how to overcome it.
Traceback (most recent call last):
File "", line 3, in
a[i].append(j)
AttributeError: 'int' object has no attribute 'append'
--
On Jun 14, 4:05 pm, sturlamolden <[EMAIL PROTECTED]> wrote:
> On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> wrote:
>
> > Is this possible?
>
> def foobar(user,score):
>sums = {}
>for u,s in zip(user,score):
> try:
> sums[u] += s
> except KeyError:
> sums[u] = s
On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> wrote:
> Is this possible?
def foobar(user,score):
sums = {}
for u,s in zip(user,score):
try:
sums[u] += s
except KeyError:
sums[u] = s
return [(u, sums[u]) for u in sums].sort()
usersum = foobar(user,score)
for
Maric Michaud <[EMAIL PROTECTED]> writes:
> Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
>> Maric Michaud <[EMAIL PROTECTED]> writes:
>> > So, writing C in python, which has dictionnary as builtin type,
>> > should be considered "more elegant" ?
>>
>> IMO that's a bit harsh.
Le Friday 13 June 2008 18:55:24 Maric Michaud, vous avez écrit :
> > approximately the double amount of memory compared to the other.
>
> I don't see how you came to this conclusion. Are you sure the extra list
> take twice more memory than the extra dictionary ?
twice less, I meant, of course...
Hello,
Le Friday 13 June 2008 17:55:44 Karsten Heymann, vous avez écrit :
> Maric Michaud <[EMAIL PROTECTED]> writes:
> > So, writing C in python, which has dictionnary as builtin type,
> > should be considered "more elegant" ?
>
> IMO that's a bit harsh.
>
harsh ? Sorry, I'm not sure to understa
Hi Maric,
Maric Michaud <[EMAIL PROTECTED]> writes:
> So, writing C in python, which has dictionnary as builtin type,
> should be considered "more elegant" ?
IMO that's a bit harsh.
> You are comparing apples with lemons, there is no such a difference
> between list index access and dictionnary
Paddy <[EMAIL PROTECTED]> writes:
> How does your solution fare against the defaultdict solution of:
>
> d = collections.defaultdict(int)
> for u,s in zip(users,score): d[u] += s
list: 0.931s
dict + "in": 1.495s
defaultdict : 1.991s
dict + "if": ~2s
dict + "try": ~4s
I've posted the (ve
Le Friday 13 June 2008 14:12:40 Karsten Heymann, vous avez écrit :
> Hi Mark,
>
> Mark <[EMAIL PROTECTED]> writes:
> > I have a scenario where I have a list like this:
> >
> > User Score
> > 1 0
> > 1 1
> > 1 5
> > 2 3
> > 2
On Jun 13, 1:12 pm, Karsten Heymann <[EMAIL PROTECTED]>
wrote:
> Hi Mark,
>
>
>
> Mark <[EMAIL PROTECTED]> writes:
> > I have a scenario where I have a list like this:
>
> > UserScore
> > 1 0
> > 1 1
> > 1 5
> > 2 3
> > 2
Hi Björn,
"BJörn Lindqvist" <[EMAIL PROTECTED]> writes:
> On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann
> <[EMAIL PROTECTED]> wrote:
>> summed_up={}
>> for user,vote in pairs:
>> if summed_up.has_key(user):
>>summed_up[user]+=vote
>> else:
>>summed_up[user]=vote
>
> You'll save even m
BJörn Lindqvist wrote:
[...]
Here is another solution:
from itertools import groupby
from operator import itemgetter
users = [1, 1, 1, 2, 2, 3, 4, 4, 4]
scores = [0, 1, 5, 3, 1, 2, 3, 3, 2]
for u, s in groupby(zip(users, scores), itemgetter(0)):
print u, sum(y for x, y in s)
Except that
On Thu, Jun 12, 2008 at 3:48 PM, Mark <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a scenario where I have a list like this:
>
> UserScore
> 1 0
> 1 1
> 1 5
> 2 3
> 2 1
> 3 2
> 4
On Fri, Jun 13, 2008 at 2:12 PM, Karsten Heymann
<[EMAIL PROTECTED]> wrote:
> Although your problem has already been solved, I'd like to present a
> different approach which can be quite a bit faster. The most common
> approach seems to be using a dictionary:
>
> summed_up={}
> for user,vote in pai
Hi Mark,
Mark <[EMAIL PROTECTED]> writes:
> I have a scenario where I have a list like this:
>
> UserScore
> 1 0
> 1 1
> 1 5
> 2 3
> 2 1
> 3 2
> 4 3
> 4 3
> 4
On Jun 12, 4:14 pm, Gerhard Häring <[EMAIL PROTECTED]> wrote:
> Aidan wrote:
> > does this work for you?
>
> > users = [1,1,1,2,2,3,4,4,4]
> > score = [0,1,5,3,1,2,3,3,2]
>
> > d = dict()
>
> > for u,s in zip(users,score):
> > if d.has_key(u):
> > d[u] += s
> > else:
> > d[u] = s
>
> >
On Jun 12, 3:45 pm, Aidan <[EMAIL PROTECTED]> wrote:
> Aidan wrote:
> > Mark wrote:
> >> John, it's a QuerySet coming from a database in Django. I don't know
> >> enough about the structure of this object to go into detail I'm
> >> afraid.
>
> >> Aidan, I got an error trying your suggestion: 'zip a
Aidan wrote:
does this work for you?
users = [1,1,1,2,2,3,4,4,4]
score = [0,1,5,3,1,2,3,3,2]
d = dict()
for u,s in zip(users,score):
if d.has_key(u):
d[u] += s
else:
d[u] = s
for key in d.keys():
print 'user: %d\nscore: %d\n' % (key,d[key])
I've recently had the very same prob
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid. [...]
Then let the database do the summing up. That's what it's there for :-)
select user, sum(score) from score_table
group by user
or some
Aidan wrote:
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
well, if
Mark wrote:
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
well, if we can create
John, it's a QuerySet coming from a database in Django. I don't know
enough about the structure of this object to go into detail I'm
afraid.
Aidan, I got an error trying your suggestion: 'zip argument #2 must
support iteration', I don't know what this means!
Thanks to all who have answered! Sorry
> To be honest I'm relatively new to Python, so I don't know too much
> about how all the loop constructs work and how they differ to other
> languages. I'm building an app in Django and this data is coming out
> of a database and it looks like what I put up there!
>
> This was my (failed) attempt
"Mark" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
On Jun 12, 3:02 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Mark wrote:
---
This was my (failed) attempt:
predictions = Prediction.objects.all()
scores = []
for prediction in predictions:
i = [prediction.predictor.id, 0]
Mark wrote:
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up th
On Jun 12, 3:02 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Mark wrote:
> > Hi all,
>
> > I have a scenario where I have a list like this:
>
> > User Score
> > 1 0
> > 1 1
> > 1 5
> > 2 3
> > 2 1
> >
On Jun 12, 3:48 pm, Mark <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a scenario where I have a list like this:
>
> User Score
> 1 0
> 1 1
> 1 5
> 2 3
> 2 1
> 3 2
> 4 3
> 4
On Thu, Jun 12, 2008 at 9:48 AM, Mark <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a scenario where I have a list like this:
>
> UserScore
> 1 0
> 1 1
> 1 5
> 2 3
> 2 1
> 3 2
> 4
Mark wrote:
> Hi all,
>
> I have a scenario where I have a list like this:
>
> UserScore
> 1 0
> 1 1
> 1 5
> 2 3
> 2 1
> 3 2
> 4 3
> 4 3
> 4
Hi all,
I have a scenario where I have a list like this:
UserScore
1 0
1 1
1 5
2 3
2 1
3 2
4 3
4 3
4 2
And I need to add up the score for ea
# Copyright (C) 2007 Darren Lee Weber
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This p
guages and libs like
DirectX lately.
Thanks
ØØ
-Original Message-
From: Larry Bates [mailto:[EMAIL PROTECTED]
Sent: Wednesday, May 11, 2005 3:55 PM
To: Oyvind Ostlund
Cc: python-list@python.org
Subject: Re: Reading files into a 2D list.
Few observations.
1) Don't concatenate pa
> Line 1 of the second file
.
.
.
Larry Bates
Oyvind Ostlund wrote:
> I am not sure what the right syntax is here. So please help me out (started 2
> days ago).
>
> I have a list of about 20 files that I want to read line by line into a 2D
> list. So the first d
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Klaus Alexander
Seistrup
Sent: Wednesday, May 11, 2005 12:14 PM
To: python-list@python.org
Subject: Re: Reading files into a 2D list.
Øyvind Østlund wrote:
> I have a list of about 20 files that I w
Øyvind Østlund wrote:
> I have a list of about 20 files that I want to read line by
> line into a 2D list. So the first dimension will be each file,
> and the second every line in that file.
>
> I tried to do something like this:
>
> files_and_lines = [][]
I am not sure what the right syntax is here. So please help me out (started 2
days ago).
I have a list of about 20 files that I want to read line by line into a 2D
list. So the first dimension will be each file, and the second every line in
that file.
I tried to do something like this
Then you can probably use something like this:
. def boxesArea(m, foreground=1, background=0):
. maxr = len(m)
. maxc = len(m[0])
. newCol = 2
. oldCol = foreground
. for r,row in enumerate(m):
. for c,e in enumerate(row):
. if e == oldCol:
.
def floodFill4(m, r, c, newCol, oldCol):
stack = [ (r, c) ]
while stack:
r, c = stack.pop()
if c >=0 and c < maxc and r >=0 and r< maxr and m[r][c]==oldCol:
m[r][c] = newCol
stack += [ (r+1, c), (r-1, c), (r, c+1), (r, c-1) ]
--
http://mail.python.org/mailman/listinfo/pyth
Bearophile,I have written the floodfill in python with stack. But i
think I am making something wrong I dont get what i need.Need your
opinion.the code follows
def connected(m, foreground=1, background=0):
def floodFill4(m, r,c, newCol, oldCol):
if newCol == oldCol:
print "
In my first post you can find two URLs with better flood filling
algorithms. You can also modify the easy recursive function, using a
list-based stack intead of recursive calls.
Bye,
Bearophile
--
http://mail.python.org/mailman/listinfo/python-list
Just was wondering how to integrate the floodfill with the stack.I
guess it will get rid of recursion problem. You mean read all the
elements of the array to a stack and then push and pop based on
conditions?
--
http://mail.python.org/mailman/listinfo/python-list
bearphile, Is there a way I could do the floodfill rather iteratively
than recursive. It somehow breaks although I made some optimisations to
the code.
--
http://mail.python.org/mailman/listinfo/python-list
[EMAIL PROTECTED]:
> hi Bearphile! That really gives me an idea.Thanks much for that. Yes
as
> you said the algorithm reaches a maximium recursion depth for larger
> sets i tried.
You can improve the little flood filling function, avoiding the "bad"
Python recursivity.
> Do you see where I am he
On 24 Apr 2005 09:44:49 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>Richter,yes what I am looking for is for cluster with rectangular
>bounding boxes as you explained in the first figure.
>
Sorry, not your fault, but I'm still not clear on diagonal
connection/separation.
E.g., (removin
hi Bearphile! That really gives me an idea.Thanks much for that. Yes as
you said the algorithm reaches a maximium recursion depth for larger
sets i tried.I still have a question. if
m = [[0,0,0,0],[0,1,1,0,0],[0,0,1,0,0],[0,0,0,0]]
all it does is count the number of 1's and return us the number in
Richter,yes what I am looking for is for cluster with rectangular
bounding boxes as you explained in the first figure.
--
http://mail.python.org/mailman/listinfo/python-list
On 23 Apr 2005 13:17:55 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>If I have
>
>ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0],
> [1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0],
> [1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0],
> [0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]
>what I wan
I hope this is what you need, sometimes understanding the question is
one of the hardest parts :-)
If you can use a graph data structure, you can create a graph, and then
you can find the lenght of all its connected components (indentations
reduced to 2 spaces):
. def mat2graph(g, m, good=None, w
If I have
ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0],
[1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0],
[0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]]
what I want is a boundingbox over the region where we find clusters of
1's.So for instance in th
62 matches
Mail list logo