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


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

Dictionaries

Started byishish <ishish@domhain.de>
First post2014-03-20 13:11 +0000
Last post2014-03-20 14:19 +0000
Articles 2 — 2 participants

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

  Dictionaries ishish <ishish@domhain.de> - 2014-03-20 13:11 +0000
    Re: Dictionaries John Gordon <gordon@panix.com> - 2014-03-20 14:19 +0000

#68577 — Dictionaries

Fromishish <ishish@domhain.de>
Date2014-03-20 13:11 +0000
SubjectDictionaries
Message-ID<mailman.8300.1395322251.18130.python-list@python.org>
Hi,

This might sound weird, but is there a limit how many dictionaries a 
can create/use in a single script?

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)

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):
			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:
		f.write(newLine)

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)

[toc] | [next] | [standalone]


#68579

FromJohn Gordon <gordon@panix.com>
Date2014-03-20 14:19 +0000
Message-ID<lgete6$fom$1@reader1.panix.com>
In reply to#68577
In <mailman.8300.1395322251.18130.python-list@python.org> ishish <ishish@domhain.de> writes:

> The script [...] only creates batch1.csv.

If the script only creates batch1.csv, that means Batch2 and Batch3 must
be empty.

> for k, v in myDict.items():
> 	if Batch1.has_key(k):
> 		if k in Batch2.has_key(k):
> 			Batch3[k] = v
> 		else:
> 			Batch2[k] = v
> 	else:
> 		Batch1[k] = v

'if k in Batch2.has_key(k):' is a very strange line of code.  In fact, it
should produce a syntax error if it were ever executed, because the 'in'
keyword requires an iterable as the second part, and has_key() returns
only True or False.

Therefore, I would say that line of code never executes, which means
that the preceding 'if Batch1.has_key(k)' statement always evaluates
to False.

Which therefore means that Batch2 and Batch3 never accumulate any items,
matching your observed output.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [standalone]


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


csiph-web