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


Groups > comp.lang.python > #68580 > unrolled thread

Re:Dictionaries

Started byDave Angel <davea@davea.name>
First post2014-03-20 10:44 -0400
Last post2014-03-20 10:44 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#68580 — Re:Dictionaries

FromDave Angel <davea@davea.name>
Date2014-03-20 10:44 -0400
SubjectRe:Dictionaries
Message-ID<mailman.8302.1395326399.18130.python-list@python.org>
 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web