Re: [Tutor] how to delete some quasi-duplicated keys
On 2011-11-26 03:49, lina wrote: for k, v in occurence.items(): print(v,k) 292 frozenset({66, 69}) 222 frozenset({24, 27}) How can I let the result like: 292 {66,69} 222 {24,27} don't output the frozenset If you want to use your own output format you have to provide it. For example, you could use a combination of string formatting and .join() to get what you want: for k, v in occurence.items(): print({0} {{{1}}}.format(v, ,.join([str(x) for x in k]))) Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to delete some quasi-duplicated keys
pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str(k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) Thanks for any advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: Which one do you want to keep? If the order ('66', '69') is unimportant, you should use a frozenset instead of a tuple. In your code, where you set the pairs in the first place, instead of doing: pair = ('66', '69') # however you build the pair pairs[pair] = value (or how ever you set the initial values), change it to this: pair = frozenset(('66', '69')) pairs[pair] = pairs.get(pair, 0) + value -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On 2011/11/25 10:41 AM, lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str(k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) Thanks for any advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor pairs.items() is fine if it's a small dictionary but for larger ones it's going to slow things down as it generates the entire list of items before you continue, rather use .iteritems() as it creates an iterator which only yields one item at a time making it more efficient. str(k)[1]+str(k)[0] is string concatenation so your first key tested is '6669' and not ('66', '69') as you intended, you would have to create a new tuple to get the key you wanted like `if (k[1], k[0]) in pairs.keys():`. Also, your check to in pairs.keys() is wasteful as you generate a new list of keys for every key and you will be better off using `in pairs:` directly as it performs a dictionary lookup to test if the key exists. With that in mind, this is how I would re-write that code segment. YMMV for key in pairs.iterkeys(): # The [::-1] creates a reverse of the iterable so ('66', '69') will be ('69', '66') if key[::-1] in pairs: try: # The first instance of the pair gets kept and the reversed gets added pairs[key] += pairs[key[::-1]] del pairs[::-1] except KeyError: print Key ('%s', '%s') already accumulated) % key Hope that helps. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On Fri, Nov 25, 2011 at 5:05 PM, Christian Witts cwi...@compuscan.co.za wrote: On 2011/11/25 10:41 AM, lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str( k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) Thanks for any advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor pairs.items() is fine if it's a small dictionary but for larger ones it's going to slow things down as it generates the entire list of items before you continue, rather use .iteritems() as it creates an iterator which only yields one item at a time making it more efficient. str(k)[1]+str(k)[0] is string concatenation so your first key tested is '6669' and not ('66', '69') as you intended, you would have to create a new tuple to get the key you wanted like `if (k[1], k[0]) in pairs.keys():`. Also, your check to in pairs.keys() is wasteful as you generate a new list of keys for every key and you will be better off using `in pairs:` directly as it performs a dictionary lookup to test if the key exists. With that in mind, this is how I would re-write that code segment. YMMV for key in pairs.iterkeys(): # The [::-1] creates a reverse of the iterable so ('66', '69') will be ('69', '66') $ python3 dehydron_data_stastic.py | sort -nr Traceback (most recent call last): File dehydron_data_stastic.py, line 44, in module for k in pairs.iterkeys(): AttributeError: 'dict' object has no attribute 'iterkeys' if key[::-1] in pairs: try: # The first instance of the pair gets kept and the reversed gets added pairs[key] += pairs[key[::-1]] del pairs[::-1] except KeyError: print Key ('%s', '%s') already accumulated) % key Hope that helps. -- Christian Witts Python Developer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On 2011/11/25 11:15 AM, lina wrote: On Fri, Nov 25, 2011 at 5:05 PM, Christian Wittscwi...@compuscan.co.za wrote: On 2011/11/25 10:41 AM, lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str( k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) Thanks for any advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor pairs.items() is fine if it's a small dictionary but for larger ones it's going to slow things down as it generates the entire list of items before you continue, rather use .iteritems() as it creates an iterator which only yields one item at a time making it more efficient. str(k)[1]+str(k)[0] is string concatenation so your first key tested is '6669' and not ('66', '69') as you intended, you would have to create a new tuple to get the key you wanted like `if (k[1], k[0]) in pairs.keys():`. Also, your check to in pairs.keys() is wasteful as you generate a new list of keys for every key and you will be better off using `in pairs:` directly as it performs a dictionary lookup to test if the key exists. With that in mind, this is how I would re-write that code segment. YMMV for key in pairs.iterkeys(): # The [::-1] creates a reverse of the iterable so ('66', '69') will be ('69', '66') $ python3 dehydron_data_stastic.py | sort -nr Traceback (most recent call last): File dehydron_data_stastic.py, line 44, inmodule for k in pairs.iterkeys(): AttributeError: 'dict' object has no attribute 'iterkeys' if key[::-1] in pairs: try: # The first instance of the pair gets kept and the reversed gets added pairs[key] += pairs[key[::-1]] del pairs[::-1] except KeyError: print Key ('%s', '%s') already accumulated) % key Hope that helps. -- Christian Witts Python Developer Ah, should have mentioned .iterkeys() / .itervalues() are in Python 2.x and .keys() and .values() have been changed in Py3 to match .iter* from Py2.x. Just change that line to `for key in pairs.keys():` then. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On Fri, Nov 25, 2011 at 5:06 PM, Steven D'Aprano st...@pearwood.info wrote: lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: Which one do you want to keep? It does not matter, the order is not important, If the order ('66', '69') is unimportant, you should use a frozenset instead of a tuple. In your code, where you set the pairs in the first place, instead of doing: pair = ('66', '69') # however you build the pair pairs[pair] = value (or how ever you set the initial values), change it to this: pair = frozenset(('66', '69')) pairs[pair] = pairs.get(pair, 0) + value I don't get this pairs.get part. pairs[pair]=pairs.get(pair,0)+parts[2] TypeError: unsupported operand type(s) for +: 'int' and 'str' or line in f.readlines(): parts=line.split() #pair=set((parts[0],parts[1])) if (parts[0],parts[1]) not in dehydrons.keys(): dehydrons[(parts[0],parts[1])]=parts[2] occurence[(parts[0],parts[1])]=1 pair=frozenset(('parts[0]','parts[1]')) pairs[pair]=pairs.get(pair,0)+parts[2] else: occurence[(parts[0],parts[1])]+=1 I upload the file in: https://docs.google.com/open?id=0B93SVRfpVVg3NTg5YTkwMGUtMzdiNi00ZGI2LWE5ZGYtMTFmMTc0OTZhMzZl https://docs.google.com/open?id=0B93SVRfpVVg3NGQwZDRhNDMtMDUzZi00NDIxLWE2MDAtZGM0ZWEzZDczYTMz -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On Fri, Nov 25, 2011 at 5:19 PM, Christian Witts cwi...@compuscan.co.za wrote: On 2011/11/25 11:15 AM, lina wrote: On Fri, Nov 25, 2011 at 5:05 PM, Christian Witts cwi...@compuscan.co.za wrote: On 2011/11/25 10:41 AM, lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str( k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) Thanks for any advice, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor pairs.items() is fine if it's a small dictionary but for larger ones it's going to slow things down as it generates the entire list of items before you continue, rather use .iteritems() as it creates an iterator which only yields one item at a time making it more efficient. str(k)[1]+str(k)[0] is string concatenation so your first key tested is '6669' and not ('66', '69') as you intended, you would have to create a new tuple to get the key you wanted like `if (k[1], k[0]) in pairs.keys():`. Also, your check to in pairs.keys() is wasteful as you generate a new list of keys for every key and you will be better off using `in pairs:` directly as it performs a dictionary lookup to test if the key exists. With that in mind, this is how I would re-write that code segment. YMMV for key in pairs.iterkeys(): # The [::-1] creates a reverse of the iterable so ('66', '69') will be ('69', '66') $ python3 dehydron_data_stastic.py | sort -nr Traceback (most recent call last): File dehydron_data_stastic.py, line 44, in module for k in pairs.iterkeys(): AttributeError: 'dict' object has no attribute 'iterkeys' if key[::-1] in pairs: try: # The first instance of the pair gets kept and the reversed gets added pairs[key] += pairs[key[::-1]] del pairs[::-1] except KeyError: print Key ('%s', '%s') already accumulated) % key Hope that helps. -- Christian Witts Python Developer Ah, should have mentioned .iterkeys() / .itervalues() are in Python 2.x and .keys() and .values() have been changed in Py3 to match .iter* from Py2.x. Just change that line to `for key in pairs.keys():` then. for key in pairs.keys(): if key[::-1] in pairs: pairs[key] += pairs[key[::-1]] del pairs[key[::-1]] print(pairs) Traceback (most recent call last): File dehydron_data_stastic.py, line 47, in module for key in pairs.keys(): RuntimeError: dictionary changed size during iteration -- Christian Witts Python Developer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
#!/usr/bin/python3 dehydrons={} pairs={} #frozen set way pairs fs_pairs={} occurence={} total=0 dictionary={} candidate_dehydron={} if __name__==__main__: with open(dehydron_refined_data_18.txt,r) as f: for line in f.readlines(): parts=line.split() #pair=set((parts[0],parts[1])) if (parts[0],parts[1]) not in dehydrons.keys(): dehydrons[(parts[0],parts[1])]=parts[2] occurence[(parts[0],parts[1])]=1 #pair=frozenset(('parts[0]','parts[1]')) #pairs[pair]=pairs.get(pair,0)+parts[2] else: occurence[(parts[0],parts[1])]+=1 #for k, v in dehydrons.items(): #print(k,v) for k, v in occurence.items(): if v=25: #print(v,k) candidate_dehydron[k]=v #print({:.2f}.format(v/2768*100),k) total+=v print(total) for k, v in candidate_dehydron.items(): pairs[k] = v '''for key in pairs.keys(): if key[::-1] in pairs: pairs[key] += pairs[key[::-1]] del pairs[key[::-1]] print(pairs)''' #for k, v in pairs.items(): #print(v,k) I attached the not working code, Thanks for any advice, best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
for key, value in pairs.items(): if key[::-1] in pairs.keys() and pairs[key] != 0: pairs[key] += pairs[key[::-1]] pairs[key[::-1]]=0 for k, v in pairs.items(): if v != 0: print(v,k) Now very trivial, but works. On Fri, Nov 25, 2011 at 5:34 PM, lina lina.lastn...@gmail.com wrote: #!/usr/bin/python3 dehydrons={} pairs={} #frozen set way pairs fs_pairs={} occurence={} total=0 dictionary={} candidate_dehydron={} if __name__==__main__: with open(dehydron_refined_data_18.txt,r) as f: for line in f.readlines(): parts=line.split() #pair=set((parts[0],parts[1])) if (parts[0],parts[1]) not in dehydrons.keys(): dehydrons[(parts[0],parts[1])]=parts[2] occurence[(parts[0],parts[1])]=1 #pair=frozenset(('parts[0]','parts[1]')) #pairs[pair]=pairs.get(pair,0)+parts[2] else: occurence[(parts[0],parts[1])]+=1 #for k, v in dehydrons.items(): #print(k,v) for k, v in occurence.items(): if v=25: #print(v,k) candidate_dehydron[k]=v #print({:.2f}.format(v/2768*100),k) total+=v print(total) for k, v in candidate_dehydron.items(): pairs[k] = v '''for key in pairs.keys(): if key[::-1] in pairs: pairs[key] += pairs[key[::-1]] del pairs[key[::-1]] print(pairs)''' #for k, v in pairs.items(): #print(v,k) I attached the not working code, Thanks for any advice, best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
lina wrote: On Fri, Nov 25, 2011 at 5:06 PM, Steven D'Aprano st...@pearwood.info wrote: pair = frozenset(('66', '69')) pairs[pair] = pairs.get(pair, 0) + value I don't get this pairs.get part. The get method does a look-up on a dict, but instead of failing if the key is missing, it returns a default value: py d = {'a': 2} py d['b'] # fails Traceback (most recent call last): File stdin, line 1, in module KeyError: 'b' py d.get('b', 3) # use 3 as the default 3 pairs[pair]=pairs.get(pair,0)+parts[2] TypeError: unsupported operand type(s) for +: 'int' and 'str' Lina, in your original example, the values of the dict are integers: {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} The error you show above can only happen if they are not integers, but strings. When you show us examples, please get the examples right. If you give us wrong information, how do you expect us to help? You should convert all the strings into ints. or line in f.readlines(): parts=line.split() #pair=set((parts[0],parts[1])) if (parts[0],parts[1]) not in dehydrons.keys(): dehydrons[(parts[0],parts[1])]=parts[2] occurence[(parts[0],parts[1])]=1 pair=frozenset(('parts[0]','parts[1]')) pairs[pair]=pairs.get(pair,0)+parts[2] else: occurence[(parts[0],parts[1])]+=1 f = open(some file) dehydrons = {} occurrence = {} pairs = {} for line in f.readlines(): parts = line.split() # convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) # order doesn't matter if pair in dehydrons: occurrence[pair] += 1 else: dehydrons[pair] = parts[2] occurrence[pair] = 1 pairs[pair] = pairs.get(pair, 0) + parts[2] f.close() -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On 25/11/11 08:41, lina wrote: pairs {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} such as here ('66', '69') and ('69', '66') is one key, I wanna keep only one and add the value of those two keys, above is a very simple example: here is the (failed) code: for k, v in pairs.items(): if str(k)[1]+str(k)[0] in pairs.keys(): I don;t think this does what you think it does. k is a key from pairs which looks like ('66', '69') You convert that to a string (with str(k)) which yields : ('66', '69') You then add the first two characters of that string. Those are ( and ' so you get a value of (' and ask whether that appeats in pairs.keys() which it will not because the keys are tuples. So the if block never happens print(pairs[str(k)[1]+str(k)[0]]) pairs[k]+=pairs[str(k)[1]+str(k)[0]] del pairs[str(k)[1]+str(k)[0]] print(v,k) And you print the original value followed by the tuple key. I'm pretty sure thats not what you want. Maybe you are trying to add the two elements of the tuple? But even that won't disambiguate between (66,69) and (69,66) You would need to sort the tuples first so that both would render 66,69. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On Fri, Nov 25, 2011 at 7:19 PM, Steven D'Aprano st...@pearwood.info wrote: lina wrote: On Fri, Nov 25, 2011 at 5:06 PM, Steven D'Aprano st...@pearwood.info wrote: pair = frozenset(('66', '69')) pairs[pair] = pairs.get(pair, 0) + value I don't get this pairs.get part. The get method does a look-up on a dict, but instead of failing if the key is missing, it returns a default value: py d = {'a': 2} py d['b'] # fails Traceback (most recent call last): File stdin, line 1, in module KeyError: 'b' py d.get('b', 3) # use 3 as the default 3 pairs[pair]=pairs.get(pair,0)+parts[2] TypeError: unsupported operand type(s) for +: 'int' and 'str' Lina, in your original example, the values of the dict are integers: Ha ... it's me. {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25} The error you show above can only happen if they are not integers, but strings. When you show us examples, please get the examples right. If you give us wrong information, how do you expect us to help? Sorry, I was confused at that time. You should convert all the strings into ints. or line in f.readlines(): parts=line.split() #pair=set((parts[0],parts[1])) if (parts[0],parts[1]) not in dehydrons.keys(): dehydrons[(parts[0],parts[1])]=parts[2] occurence[(parts[0],parts[1])]=1 pair=frozenset(('parts[0]','parts[1]')) pairs[pair]=pairs.get(pair,0)+parts[2] else: occurence[(parts[0],parts[1])]+=1 f = open(some file) dehydrons = {} occurrence = {} pairs = {} for line in f.readlines(): parts = line.split() # convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) # order doesn't matter if pair in dehydrons: occurrence[pair] += 1 else: dehydrons[pair] = parts[2] occurrence[pair] = 1 pairs[pair] = pairs.get(pair, 0) + parts[2] f.close() for line in f.readlines(): parts = line.split() #pair=set((parts[0],parts[1])) #convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) print(pair) if pair in dehydrons: occurence[pair] += 1 else: dehydrons[pair] = parts[2] pairs[pair] = pairs.get(pair,0) + parts[2] print(pairs) $ python3 dehydron_data_frozenset_version.py frozenset({2, 15}) frozenset({2, 15}) Traceback (most recent call last): File dehydron_data_frozenset_version.py, line 35, in module occurence[pair] += 1 KeyError: frozenset({2, 15}) Thanks, -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On 2011-11-25 13:40, lina wrote: On Fri, Nov 25, 2011 at 7:19 PM, Steven D'Apranost...@pearwood.info wrote: f = open(some file) dehydrons = {} occurrence = {} pairs = {} for line in f.readlines(): parts = line.split() # convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) # order doesn't matter if pair in dehydrons: occurrence[pair] += 1 else: dehydrons[pair] = parts[2] occurrence[pair] = 1 pairs[pair] = pairs.get(pair, 0) + parts[2] f.close() for line in f.readlines(): parts = line.split() #pair=set((parts[0],parts[1])) #convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) print(pair) if pair in dehydrons: occurence[pair] += 1 else: dehydrons[pair] = parts[2] pairs[pair] = pairs.get(pair,0) + parts[2] print(pairs) $ python3 dehydron_data_frozenset_version.py frozenset({2, 15}) frozenset({2, 15}) Traceback (most recent call last): File dehydron_data_frozenset_version.py, line 35, inmodule occurence[pair] += 1 KeyError: frozenset({2, 15}) You want to add one to occurence[frozenset({2, 15})] but there is no such key in occurence yet. If you carefully re-read Steven's code snippet you will see that you missed the line occurence[pair] = 1 in the else-branch. Therefore occurence[frozenset({2, 15})] wasn't set in the first iteration and you get the error in the second. You can see that you are already in the second iteration by looking at the output of your program before the error message. Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to delete some quasi-duplicated keys
On Sat, Nov 26, 2011 at 12:49 AM, Andreas Perstinger andreas.perstin...@gmx.net wrote: On 2011-11-25 13:40, lina wrote: On Fri, Nov 25, 2011 at 7:19 PM, Steven D'Apranost...@pearwood.info wrote: f = open(some file) dehydrons = {} occurrence = {} pairs = {} for line in f.readlines(): parts = line.split() # convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) # order doesn't matter if pair in dehydrons: occurrence[pair] += 1 else: dehydrons[pair] = parts[2] occurrence[pair] = 1 pairs[pair] = pairs.get(pair, 0) + parts[2] f.close() for line in f.readlines(): parts = line.split() #pair=set((parts[0],parts[1])) #convert to ints parts = [int(s) for s in parts] pair = frozenset(parts[:2]) print(pair) if pair in dehydrons: occurence[pair] += 1 else: dehydrons[pair] = parts[2] pairs[pair] = pairs.get(pair,0) + parts[2] print(pairs) $ python3 dehydron_data_frozenset_version.py frozenset({2, 15}) frozenset({2, 15}) Traceback (most recent call last): File dehydron_data_frozenset_version.py, line 35, inmodule occurence[pair] += 1 KeyError: frozenset({2, 15}) You want to add one to occurence[frozenset({2, 15})] but there is no such key in occurence yet. If you carefully re-read Steven's code snippet you will see that you missed the line occurence[pair] = 1 in the else-branch. Thanks, I was so careless. for k, v in occurence.items(): print(v,k) 292 frozenset({66, 69}) 222 frozenset({24, 27}) How can I let the result like: 292 {66,69} 222 {24,27} don't output the frozenset Therefore occurence[frozenset({2, 15})] wasn't set in the first iteration and you get the error in the second. You can see that you are already in the second iteration by looking at the output of your program before the error message. Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor