Home > OS >  Why is appending my list of tuples changing their content?
Why is appending my list of tuples changing their content?

Time:11-21

I am trying to make a list of tuples that contain a string and a dictionary. The string is a filename and the dictionary is a frequency list of n-grams.

('story.txt',
 {'back': 12,
  'been': 13,
  'bees': 58,
  'buzz': 13,
  'cant': 30,
  'come': 12,
  'dont': 64,
  'down': 16,
  'from': 22,
   ...})

For what I'm doing, I want to make a list of these tuples that would look something like

[('story.txt',
 {'back': 12,
  'been': 13,
  'bees': 58,
  'buzz': 13,
  'cant': 30,
  'come': 12,
  'dont': 64,
  'down': 16,
  'from': 22,
   ...}),
('great_expectations.txt',
 {'_he_': 12,
  'able': 32,
  'aged': 54,
  'aint': 56,
  'also': 34,
  'arms': 44,
  'away': 158,
  'baby': 23,
  ...})
]

I'm trying to do that with the following code:

documents = ['story.txt', 'great_expectations.txt']

outputs = []

for document in documents:
    doc_map = map_maker.make_map(document, 4, 10)
    list_tuple = (document, doc_map)
    # pprint.pprint(list_tuple)
    outputs.append(list_tuple) 
    # pprint.pprint(outputs)

For some reason, the code above is combining the data from the dictionaries before appending them, such that the 'story.txt' dictionary will have entries originally associated with 'great_expectations.txt' and vise-versa, like this:

[('story.txt',
  {'_he_': 12,
   'able': 32,
   'aged': 54,
   'aint': 56,
   'also': 34,
   'arms': 44,
   'away': 158,
   'baby': 23,
   'back': 238,
   ...}),
('great_expectations.txt',
  {'_he_': 12,
   'able': 32,
   'aged': 54,
   'aint': 56,
   'also': 34,
   'arms': 44,
   'away': 158,
   'baby': 23,
   'back': 238,
   ...})
]

Why is it doing this? I thought tuples were supposed to be immutable.

CodePudding user response:

There is no problem with code you presented, I speculate that you left hard coded file name somewhere in map_maker.make_map or you reusing result variable doc_map inside make_map (it's static or member of map_maker, beware of default arguments for mutable types in python)

  • Related