Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.04; 'skip:p 40': 0.04; 'test,': 0.04; 'does.': 0.07; 'initialize': 0.07; 'python': 0.08; '3.x': 0.09; '__future__': 0.09; 'iterate': 0.09; 'object.': 0.09; 'omit': 0.09; 'operator,': 0.09; 'pos': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'this:': 0.10; 'def': 0.12; 'error:': 0.12; 'debugging': 0.14; 'meaningful': 0.14; '**,': 0.16; '*before*': 0.16; 'confusing.': 0.16; 'division,': 0.16; 'except:': 0.16; 'francesc': 0.16; 'from:addr:yahoo.com.ar': 0.16; 'item:': 0.16; 'iterates': 0.16; 'mistake;': 0.16; 'nodes': 0.16; 'operator.': 0.16; 'statement;': 0.16; 'try/except': 0.16; 'url:effbot': 0.16; 'url:zone': 0.16; "subject:' ": 0.16; 'traceback': 0.16; '(most': 0.16; 'rewrite': 0.19; 'guess': 0.19; 'modify': 0.22; 'clause': 0.23; 'division': 0.23; 'indices': 0.23; 'integer': 0.23; 'last):': 0.23; 'ones.': 0.23; 'unsure': 0.23; 'fri,': 0.23; "doesn't": 0.25; 'values': 0.25; 'function': 0.25; 'statement': 0.26; 'later': 0.26; 'object': 0.26; 'fixed': 0.27; "i'm": 0.27; 'raise': 0.28; 'problem': 0.28; 'random': 0.28; 'skip:( 40': 0.29; 'import': 0.29; 'elements': 0.29; 'version': 0.29; 'all,': 0.30; 'chase': 0.30; 'iterating': 0.30; 'now)': 0.30; 'operand': 0.30; 'relying': 0.30; 'typeerror:': 0.30; 'changes': 0.30; 'it.': 0.31; 'ago': 0.31; 'usually': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr :python-list': 0.33; 'break': 0.33; 'operations': 0.33; 'list': 0.33; 'actually': 0.33; "i've": 0.33; 'file': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'duplicate': 0.35; 'try:': 0.35; 'version.': 0.35; 'here,': 0.35; 'using': 0.35; 'problems': 0.36; 'addition,': 0.37; 'similar': 0.37; 'instead,': 0.37; 'another': 0.37; 'put': 0.37; 'two': 0.37; 'think': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'but': 0.38; 'creates': 0.38; 'earlier': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'comments': 0.39; 'perhaps': 0.39; "i'd": 0.39; 'attempt': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'error.': 0.40; 'more': 0.60; 'your': 0.60; 'total': 0.61; 'cost': 0.65; 'benefit': 0.70; 'as:': 0.71; 'url:htm': 0.71; 'evaluate': 0.72; 'hand,': 0.74; 'received:190': 0.74; 'skip:n 40': 0.84; '-0300,': 0.84; 'collection,': 0.84; 'genellina': 0.84; 'node': 0.84; 'summing': 0.84; '==>': 0.91; 'gabriel': 0.91; 'type(s)': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Gabriel Genellina" Subject: Re: Unsupported operand type(s) for +: 'float' and 'tuple' Date: Fri, 10 Jun 2011 22:02:50 -0300 References: Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 190.2.4.25 User-Agent: Opera Mail/11.11 (Win32) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 149 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1307754006 news.xs4all.nl 49044 [::ffff:82.94.164.166]:39798 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7423 En Fri, 10 Jun 2011 07:30:28 -0300, Francesc Segura escribió: > Hello all, I'm new to this and I'm having problems on summing two > values at python. > > I get the following error: > > Traceback (most recent call last): > File "C:\edge-bc (2).py", line 168, in > if (costGG <= cost + T0): > TypeError: unsupported operand type(s) for +: 'float' and 'tuple' I see Tim Chase already told you about this error. Let me make a few comments about the rest. > try: > import matplotlib.pyplot as plt > except: > raise I guess the above try/except was left from some earlier debugging attempt - such an except clause is useless, just omit it. > T0 = 0.5 > RO = 0.99 Perhaps those names make sense in your problem at hand, but usually I try to use more meaningful ones. 0 and O look very similar in some fonts. > for i in range(len(edges)): > total = 0 > cost = 0 > factor = 1 > liedges = list(edges[i]) > linode1 = list(liedges[0]) > linode2 = list(liedges[1]) list(something) creates a new list out of the elements from `something`. You're just iterating here, so there is no need to duplicate those lists. In addition, Python is not C: the `for` statement iterates over a collection, you don't have to iterate over the indices and dereference each item: for liedges in edges: linode1 = liedges[0] linode2 = liedges[1] > distance = (((linode2[0]-linode1[0])%N)^2)+(((linode2[1]- > linode1[1])%N)^2) That doesn't evaluate what you think it does. ^ is the "bitwise xor" operator, and I bet you want **, the "power" operator. > total = total + cost > return(total) return is not a function but a statement; those () are unnecesary and confusing. And I think you want to initialize total=0 *before* entering the loop; also, initializing cost and factor is unnecesary. > def costGeasy(G): > bc = NX.edge_betweenness_centrality(G,normalized=True) > total = 0 > for i in range(len(bc)): > total=total+bc.values()[i] > > return (total) bc = NX.edge_betweenness_centrality(G,normalized=True) values = bc.values() total = sum(values) return total ==> return sum(bc.values()) > pos={} > for i in range(NODES): > pos[nod[i]]=(nod[i][0]/(N*1.0),nod[i][1]/(N*1.0)) In Python version 2.x, 1/3 evals to 0, but that's a mistake; it is fixed in the 3.x version. If you put this line at the top of your script: from __future__ import division then 1/3 returns 0.3333... When you actually want integer division, use //, like 1//3 So we can rewrite the above as: from __future__ import division ... for node in nod: pos[node] = (node[0] / N, node[1] / N) Another way, not relying on true division: divisor = float(N) for node in nod: pos[node] = (node[0] / divisor, node[1] / divisor) or even: pos = dict((node, (node[0] / divisor, node[1] / divisor)) for node in nod) > for y in range(NK): > for x in range(ITERATIONS): > cost = costG(G) > if (cost < (best_cost)): > best_graph = G > best_cost = cost > GG = G Again, I think this doesn't do what you think it does. GG = G means "let's use the name GG for the object currently known as G". GG is not a "copy" of G, just a different name for the very same object. Later operations like GG.remove_edge(...) modify the object - and you'll see the changes in G, and in best_graph, because those names all refer to the same object. I think you'll benefit from reading this: http://effbot.org/zone/python-objects.htm > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > adj=G.adjacency_list() > while ((nod[b] in adj[a]) or (b == a)): > a = random.randint(0,NODES-1) > b = random.randint(0,NODES-1) > GG.add_edge(nod[a],nod[b]) As above, I'd avoid using indexes, take two random nodes using random.sample instead, and avoid adjacency_list(): while True: a, b = random.sample(nod, 2) if b not in G[a]: break GG.add_edge(a, b) (mmm, I'm unsure of the adjacency test, I've used networkx some time ago but I don't have it available right now) -- Gabriel Genellina