Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > list to table

Reply
Thread Tools

list to table

 
 
jay
Guest
Posts: n/a
 
      11-05-2009
Hi All,

Am new to using newgroup for help, but have exhausted my searching
online for a solution

I have a long list of data of associations between values with a value
to that association as follows..

(var) to (var) = (var) hits
A B 1
B A 1
A B 3
B A 3
A C 7
C A 2

And need to build a table as follows that accumulates the above:

row is (from)
column is (to)

A B C
A 0 4 7
B 4 0 0
C 2 0 0

Just can't seam to figure out how to manage this programatically.

Any help or guidance much appreciated !!!


Cheers,


Jay
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-05-2009
* jay:
>
> I have a long list of data of associations between values with a value
> to that association as follows..
>
> (var) to (var) = (var) hits
> A B 1
> B A 1
> A B 3
> B A 3
> A C 7
> C A 2
>
> And need to build a table as follows that accumulates the above:
>
> row is (from)
> column is (to)
>
> A B C
> A 0 4 7
> B 4 0 0
> C 2 0 0
>
> Just can't seam to figure out how to manage this programatically.


You're not very clear on what A, B and C are. Assuming that they're constants
that denote unique values you can do something like

<code>
table = dict()
for k1, k2, n in list:
position = (k1, k2)
if position not in table:
table[position] = n
else:
table[position] += n
</code>

Disclaimer: I'm a Python newbie so there may be some much easier way...


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      11-05-2009
On Nov 5, 4:09*pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * jay:
>
>
>
>
>
> > I have a long list of data of associations between values with a value
> > to that association as follows..

>
> > (var) to (var) = (var) hits
> > A B 1
> > B A 1
> > A B 3
> > B A 3
> > A C 7
> > C A 2

>
> > And need to build a table as follows that accumulates the above:

>
> > row is (from)
> > column is (to)

>
> > * *A B C
> > A 0 4 7
> > B 4 0 0
> > C 2 0 0

>
> > Just can't seam to figure out how to manage this programatically.

>
> You're not very clear on what A, B and C are. Assuming that they're constants
> that denote unique values you can do something like
>
> <code>
> table = dict()
> for k1, k2, n in list:
> * * *position = (k1, k2)
> * * *if position not in table:
> * * * * *table[position] = n
> * * *else:
> * * * * *table[position] += n
> </code>
>
> Disclaimer: I'm a Python newbie so there may be some much easier way...
>
> Cheers & hth.,
>
> - Alf


I read the OP as homework (I'm thinking Scott did as well), however,
your code would be much nicer re-written using collections.defaultdict
(int)... which I don't think is giving anything away...

However, the challenge of making it 'tabular' or whatever the
requirement of 'table' is, is still there.

Jon.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-06-2009
* Jon Clements:
>
> I read the OP as homework (I'm thinking Scott did as well),


Sorry. Need to recalibrate that neural network. Back-propagation initiated...
Done!


> however,
> your code would be much nicer re-written using collections.defaultdict
> (int)... which I don't think is giving anything away...


Thanks!

This sent me searching everywhere, because the documentation of '+=' and other
"augmented assignment statements" says

"The target is only evaluated once.",

like in C++, which implies a kind of reference to mutable object.

I couldn't immediately see how subscription could apparently return a reference
to mutable int object in Python in a way so that it worked transparently (the
idiom in C++).

However, it worked to replace collections.defaultdict with a class overriding
__getitem__ and __setitem__, so I guess that's how it works, that in this case
'+=' is simply translated like 'x += n' -> 'temp = x; x = temp + n'.

Is this a correct understanding, and if so, what exactly does the documentation
mean for the general case?

E.g.

def foo():
print( "foo" )
d = dict(); d[43] = 666
return d

def bar():
print( "bar" )
return 43;

foo()[bar()] += 1

produces

foo
bar

so here it's not translated like 'foo()[bar()] = foo()[bar()] + 1' but evidently
more like 'a = foo(); i = bar(); a.__setitem__(i, a.__getitem__(i) + 1)'?

If so, is this behavior defined anywhere?

I did find discussion (end of §6.2 of the language reference) of the case where
the target is an attibute reference, with this example:

class A:
x = 3 # class variable
a = A()
a.x += 1 # writes a.x as 4 leaving A.x as 3




Cheers, & thanks,

- Alf
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      11-06-2009
En Thu, 05 Nov 2009 21:23:27 -0300, Alf P. Steinbach <>
escribió:
> * Jon Clements:


> This sent me searching everywhere, because the documentation of '+=' and
> other "augmented assignment statements" says
>
> "The target is only evaluated once.",
>
> like in C++, which implies a kind of reference to mutable object.


Not exactly. For an augmented assignment, the target (left side) may be an
identifier, an atttribute reference, or a subscription/slicing. In the
first case, the comment simply does not apply (there is a single one
opportunity to evaluate the target).

a += some(expression) means: evaluate "a", evaluate some(expression),
perform the operation +=, bind the resulting object to the name "a".

a.attr += some(expression) means: evaluate "a", ask it for its attribute
"attr", evaluate some(expression), perform the operation +=, ask the
(already known) object "a" to store the resulting object as its attribute
"attr".

a[index] += some(expression) performs a similar sequence of steps; "a" is
evaluated only once, then it is asked to retrieve its element at [index],
the computation is performed, and finally the (already known) object "a"
is asked to store the result at [index].

"The target is only evaluated once." means that it is NOT reevaluated
before the final result is stored. Same applies to "index" above, although
this is not explicited. Perhaps it is more clear with a longer expression:
a[b+c].c[d(e[f])].h += 1 computes the a[b+c].c[d(e[f])] part only once.

> I couldn't immediately see how subscription could apparently return a
> reference to mutable int object in Python in a way so that it worked
> transparently (the idiom in C++).


It does not. It perform first a "getitem" operation followed by a
"setitem" to store the result. A normal dictionary would fail when asked
for an inexistent item; a defaultdict creates and returns a default value
in such case.

py> import collections
py> d = collections.defaultdict(int)
py> d['a'] += 1
py> d['a'] += 1
py> d['a']
2
py> d['b']
0

note: int() returns 0; defaultdict(lambda: 0) could have been used.

> However, it worked to replace collections.defaultdict with a class
> overriding __getitem__ and __setitem__, so I guess that's how it works,
> that in this case '+=' is simply translated like 'x += n' -> 'temp = x;
> x = temp + n'.
>
> Is this a correct understanding, and if so, what exactly does the
> documentation mean for the general case?


If x is a simple name, the only difference between x += n and x = x+n is
the method invoked to perform the operation (__iadd__ vs __add__ in
arbitrary objects). Both x and n are evaluated only once - and this is not
an optimization, nor a shortcut, simply there is no need to do otherwise
(please make sure you understand this).

From the docs for the operator module: "Many operations have an “in-placeâ€
version. The following functions provide a more primitive access to
in-place operators than the usual syntax does; for example, the statement
x += y is equivalent to x = operator.iadd(x, y). Another way to put it is
to say that z = operator.iadd(x, y) is equivalent to the compound
statement z = x; z += y."
http://docs.python.org/library/operator.html

> foo()[bar()] += 1
>
> so here it's not translated like 'foo()[bar()] = foo()[bar()] + 1' but
> evidently more like 'a = foo(); i = bar(); a.__setitem__(i,
> a.__getitem__(i) + 1)'?


Yes, something like that.

> If so, is this behavior defined anywhere?


Isn't the description at
http://docs.python.org/reference/sim...ent-statements
enough? It goes to some detail describing, e.g., what a.x = 1 means. The
next section explains what a.x += 1 means in terms of the former case.

> I did find discussion (end of §6.2 of the language reference) of the
> case where the target is an attibute reference, with this example:
>
> class A:
> x = 3 # class variable
> a = A()
> a.x += 1 # writes a.x as 4 leaving A.x as 3


Do you want to discuss this example?

--
Gabriel Genellina

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-06-2009
* Gabriel Genellina:
> En Thu, 05 Nov 2009 21:23:27 -0300, Alf P. Steinbach <>
> escribió:
>

[snip]
> From the docs for the operator module: "Many operations have an
> “in-place†version. The following functions provide a more primitive
> access to in-place operators than the usual syntax does; for example,
> the statement x += y is equivalent to x = operator.iadd(x, y). Another
> way to put it is to say that z = operator.iadd(x, y) is equivalent to
> the compound statement z = x; z += y."
> http://docs.python.org/library/operator.html


Thanks!


>> foo()[bar()] += 1
>>
>> so here it's not translated like 'foo()[bar()] = foo()[bar()] + 1' but
>> evidently more like 'a = foo(); i = bar(); a.__setitem__(i,
>> a.__getitem__(i) + 1)'?

>
> Yes, something like that.
>
>> If so, is this behavior defined anywhere?

>
> Isn't the description at
> http://docs.python.org/reference/sim...ent-statements
> enough? It goes to some detail describing, e.g., what a.x = 1 means. The
> next section explains what a.x += 1 means in terms of the former case.


No, it wasn't exactly enough for me, coming most recently from a C++ background.

One reason was as mentioned that the C++ standard has essentially the /same
wording/ about "only evaluated once" but with a more strict meaning; in C++,
with the built-in += operator

a()[foo()] += 1;

not only avoids calling a() and foo() twice, it also avoids doing the internal
indexing twice, while in python the internal indexing, locating that item, is
performed first in __getitem__ and then in __setitem__ (unless that is optimized
away at lower level by caching last access, but that in itself has overhead).

Another reason was that §6.2 does explicitly discuss attribute references as
targets, but not subscription as target. It would have been more clear to me if
all (four?) possible target forms were discussed. Happily you did now discuss
that in the part that I snipped above, but would've been nice, and easier for
for an other-language-thinking person , if it was in documentation.


>> I did find discussion (end of §6.2 of the language reference) of the
>> case where the target is an attibute reference, with this example:
>>
>> class A:
>> x = 3 # class variable
>> a = A()
>> a.x += 1 # writes a.x as 4 leaving A.x as 3

>
> Do you want to discuss this example?


Thanks but no, it's OK, I understand it.


Cheers,

- Alf
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      11-06-2009
En Fri, 06 Nov 2009 04:29:05 -0300, Alf P. Steinbach <>
escribió:
> * Gabriel Genellina:
>> En Thu, 05 Nov 2009 21:23:27 -0300, Alf P. Steinbach <>
>> escribió:
>>
>>> foo()[bar()] += 1
>>>

> One reason was as mentioned that the C++ standard has essentially the
> /same wording/ about "only evaluated once" but with a more strict
> meaning; in C++, with the built-in += operator
>
> a()[foo()] += 1;
>
> not only avoids calling a() and foo() twice, it also avoids doing the
> internal indexing twice, while in python the internal indexing, locating
> that item, is performed first in __getitem__ and then in __setitem__
> (unless that is optimized away at lower level by caching last access,
> but that in itself has overhead).


Yes, that's a common misunderstanding in people coming from other
languages with a different semantics for "assignment" and "variable".
You're not alone

Python does not have "lvalues" as in C++. In the statement x=1, the left
hand side does not denote an object, but a name. x.attr=1 and x[index]=1
act more like a function call (they *are* function calls actually) than
assignments.

> Another reason was that §6.2 does explicitly discuss attribute
> references as targets, but not subscription as target. It would have
> been more clear to me if all (four?) possible target forms were
> discussed. Happily you did now discuss that in the part that I snipped
> above, but would've been nice, and easier for for an
> other-language-thinking person , if it was in documentation.


Yes, probably that section should be improved (except the final example
added, the text hasn't changed since it was first written, more than 9
years ago).

Reading reference material may be terribly boring, I admit. Most people
read only the specific sections required to solve a specific problem; and
some concepts that are introduced earlier in the book are missed or
skipped.
If you haven't already done so, try at least to read these two sections
from the Language Reference: 3.1. Objects, values and types, and 4.1.
Naming and binding. They define the most important concepts in Python; the
rest are just details.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM
populating an asp list box from a simple access lookup list (single column not a table) gerry ASP .Net 0 04-24-2004 09:21 AM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57