0

I have a dictionary in the format:

dictionary= {reference:annotation}

where the reference refers to a position, and the annotation contains information about that location.

I want to find reference positions that overlap, and update the annotation when that occurs. The annotation that I want to update is accessed by dictionary["reference"].qualifiers["ID"] (the annotation contains a second dictionary, where I can access the information I want). When if I try to add another ID to the annotation using: d

dictionary[reference].qualifiers["ID"].extend(["new ID"])

or

dictionary[reference].qualifiers["ID"].append("new ID")

all reference annotations in my dictionary are being updated with that new ID. However, if do this using basic list comprehension I get the desired result:

dictionary[reference].qualifiers["ID"] = dictionary[reference].qualifiers["ID"] + ["new ID"]

Only the annotation at that reference is updated. Can anyone explain why I am getting a different result using "append" or "extend"?

5
  • Say you print a value of a dictionary["reference"].qualifiers["ID"], what do you get? Nov 24, 2015 at 11:47
  • I provided an answer, because there's a reasonable chance that it might help, even though the question is deficient. However, this question doesn't meet the asking guidelines: stackoverflow.com/help/mcve ... and will likely be closed soon unless you rectify that. Nov 24, 2015 at 12:05
  • The code you have posted is not an MCVE. However, it appears that you think that the commented out line is the same as the line two above it. In your question, the the "desired result" code is in fact identical to the "supposedly problematic" code. In your actual code, the commented out takes the first element of the "taxa" array, rather than the array itself. This is not the same. It also bears no resemblence to your description of the problem ("all reference annotations are updated"), but at least its a discrepancy to look at... Nov 24, 2015 at 21:30
  • 2
    Sorry about I rolled back to your old question and voted to close it as This question can no longer be reproduce. I did it because seems like your example and your full code are different question. I mean your example works, but your code should be another question. As I said, if your problem didn't solve, you can ask another question but please with MCVE as @GreenAsJade said(like your old question). Also please remember that don't ask two questions in one question next time. :)
    – Remi Guan
    Nov 24, 2015 at 22:46
  • 1
    So remember what I said, ask a question with minimal reproducible example: The code included in your question should be Minimal (only the code necessary to reproduce the issue), Complete (all of the code necessary to reproduce the issue) and Verifiable (we should be able to reproduce the issue using only the code in your question, nothing less and nothing more). :D
    – Remi Guan
    Nov 25, 2015 at 2:06

1 Answer 1

1

The first example you give as not working works for me:

class Annotation:
    def __init__(self, initial_val):
        self.qualifiers = {'ID': [initial_val]}

an1 = Annotation("foo")
an2 = Annotation("bar")

d = {'ref1' : an1, 'ref2': an2}

print d['ref1'].qualifiers['ID']
print d['ref2'].qualifiers['ID']

d['ref1'].qualifiers['ID'].extend(['new foo'])

print d['ref1'].qualifiers['ID']
print d['ref2'].qualifiers['ID']

results in:

~ mgregory$ python foo.py
['foo']
['bar']
['foo', 'new foo']
['bar']
~ mgregory$ 

I think you have something wrong with the way you are creating annotations - possibly mistaking a shallow copy for a deep one, or a similar data structure pitfall like that.

You need to post actual code that doesn't work.


As a side note, the code you described as a comprehension, is not. It's just use of the array operator +.

5
  • Ok, thanks for your comment, I've updated my question with the original code. A colleague has also suggested a data structure issue since my post.
    – Conor
    Nov 24, 2015 at 14:21
  • Cool - maybe their suggestion will solve it for you. I hate to be pinickty, but note that the code that you have supplied is not an MVCE. More often than not, in the process of creating an MVCE, you find the issue yourself. Nov 24, 2015 at 21:21
  • That's ok, I'm impressed with the speed of responses on the forum. I will try and stick to MVCE in future. In this case the problem seems stem from another piece of software I've been using to read in the files, and probably as you say, from a deep vs shallow copy issue. Anyway, thanks again for the help, I seem to have got around the issue for now.
    – Conor
    Nov 25, 2015 at 16:18
  • 1
    Update: just had another look at this, and I think you're right. Making a deep copy of the feature seems to allow me to use append as I wanted. for feature in tmp_record: feature= copy.deepcopy(feature)
    – Conor
    Nov 25, 2015 at 16:22
  • You would have discovered this for yourself in the process of creating an MVCE ;) Nov 26, 2015 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.