Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #68580

Re:Dictionaries

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'else:': 0.03; 'anyway.': 0.05; 'exception.': 0.09; 'f.close()': 0.09; 'meaningful': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'trees': 0.09; 'python': 0.11; 'wrote': 0.14; 'creates': 0.14; '(k,': 0.16; 'csv': 0.16; 'deprecated,': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'script?': 0.16; 'thread,': 0.16; 'throw': 0.16; 'obviously': 0.18; 'trying': 0.19; 'split': 0.19; 'print': 0.22; 'file.': 0.24; 'script': 0.25; 'asking': 0.27; 'header:X-Complaints-To:1': 0.27; 'leave': 0.29; "doesn't": 0.30; 'especially': 0.30; "i'm": 0.30; 'fine,': 0.31; 'file': 0.32; 'probably': 0.32; 'run': 0.32; 'open': 0.33; 'there,': 0.34; "can't": 0.35; 'no,': 0.35; 'but': 0.35; 'there': 0.35; 'hi,': 0.36; 'changing': 0.37; 'wrong': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'numbers': 0.61; "you're": 0.61; 'different': 0.65; 'phone': 0.66; 'sound': 0.68; 'subject': 0.69; 'limit': 0.70; 'compose': 0.84; 'ref': 0.84; 'subject::': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re:Dictionaries
Date Thu, 20 Mar 2014 10:44:32 -0400 (EDT)
Organization news.gmane.org
References <CANXBEFogXsze6WByg_iCit-oLEQK-RXsgJCDM=ocZ7JPNgdb8g@mail.gmail.com> <cc9658ed24ed4af1b65b5098a00f9aac@home.minuskel.de>
X-Gmane-NNTP-Posting-Host wsip-68-106-144-188.dc.dc.cox.net
X-Newsreader PiaoHong Usenet NewsReaders 1.36
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.8302.1395326399.18130.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395326399 news.xs4all.nl 2974 [2001:888:2000:d::a6]:37571
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:68580

Show key headers only | View raw


 Please don't leave new questions in an existing thread, and
 especially without changing subject line. Compose a new message
 with meaningful subject line. 


ishish <ishish@domhain.de> Wrote in message:
> Hi,
> 
> This might sound weird, but is there a limit how many dictionaries a 
> can create/use in a single script?

No, unless you run out of RAM.

> 
> My reason for asking is I split a 2-column-csv (phone#, ref#) file into 
> a dict and am trying to put duplicated phone numbers with different ref 
> numbers into new dictionaries. The script deducts the duplicated 46 
> numbers but it only creates batch1.csv. Since I obviously can't see the 
> wood for the trees here, can someone pls punch me into the right 
> direction....
> ...(No has_key is fine, its python 2.7)

But has_key is deprecated,  and you're using it wrong anyway. 

> 
> f = open("file.csv", 'r')
> 
> myDict = {}
> Batch1 = {}
> Batch2 = {}
> Batch3 = {}
> 
> for line in f:
> 	if line.startswith('Number' ):
> 		print "First line ignored..."
> 	else:
> 		k, v = line.split(',')
> 		myDict[k] = v
> f.close()
> 
> for k, v in myDict.items():
> 	if Batch1.has_key(k):
> 		if k in Batch2.has_key(k):

I'm surprised this doesn't throw an exception.   has_key returns a
 bool, and in isn't meaningful on a bool.  Just use
 
      if k in Batch2:


> 			Batch3[k] = v
> 		else:
> 			Batch2[k] = v
> 	else:
> 		Batch1[k] = v
> 
> for k, v in Batch1.items():
> 	newLine = "%s,%s" % (k, v)
> 	with open("batch1.csv", "a") as f:

This is unusual,  and a performance killer,  to repeatedly open
 the file.  But you may have some reason. 

> 		f.write(newLine)

There's no newline there, so your csv is going to be one long
 line. You probably want newLine+'\n' inside those
 parens.

> 
> for k, v in Batch2.items():
> 	newLine = "%s,%s" % (k, v)
> 	with open("batch2.csv", "a") as f:
> 		f.write(newLine)
> 
> for k, v in Batch3.items():
> 	newLine = "%s,%s" % (k, v)
> 	with open("batch3.csv", "a") as f:
> 		f.write(newLine)
> 


-- 
DaveA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re:Dictionaries Dave Angel <davea@davea.name> - 2014-03-20 10:44 -0400

csiph-web