0

Following the previous comment I am including only the code necessary to understand the issue. I hope it is understandable. Here is the code:

list_dict = [[] for i in range(10)]
out_dict_2 = {}
out_dict_2.update({'key_a': list_dict, 'key_b': list_dict, 'key_c': list_dict})

for index in range(10):
    for line in list_2:
        line = line.strip()
        line = line.split()
        if areallnumbers(line): # check whether all the elements in the list are float
            out_dict_2['key_a'][index].append(line[0])
            out_dict_2['key_b'][index].append(line[1])
            out_dict_2['key_c'][index].append(line[2])

where list_2 looks like EDIT:(I can have also more list of the same type, in my case they are 10--> for index in range(10):):

['5.00', '-4601.24', '31.73']
['11.04', '-4517.66', '32.51']
['17.08', '-4418.59', '33.24']
['23.12', '-4305.34', '33.93']
['29.17', '-4179.09', '34.59']
......

I am trying to have the first column of each list appended to one dictionary element, i.e. the first column to out_dict_2[key_a][index] and so on, so each value is a list of 10 lists. Something like this (assuming the 10 lists are all equal to the example list I have given:

key_a [['5.00'    , '11.04'   , '17.08'   , '23.12'   , ...],...,['5.00'    , '11.04'   , '17.08'   , '23.12'   , ...]]
key_b [['-4601.24', '-4517.66', '-4418.59', '-4418.59', ...],...,['5.00'    , '11.04'   , '17.08'   , '23.12'   , ...]]
key_c [['31.73'   , '32.51'   , '33.24'   , '33.93'   , ...],...,['5.00'    , '11.04'   , '17.08'   , '23.12'   , ...]]

What I am actually getting is:

key_a [['5.00', '-4601.24', '31.73'...]['5.00', '-4601.24', '31.73'...]...['5.00', '-4601.24', '31.73'...]
key_b [['5.00', '-4601.24', '31.73'...]['5.00', '-4601.24', '31.73'...]...['5.00', '-4601.24', '31.73'...]
key_c [['5.00', '-4601.24', '31.73'...]['5.00', '-4601.24', '31.73'...]...['5.00', '-4601.24', '31.73'...]

as if each line is appended to each dictionary key, why?

drSlump
  • 307
  • 4
  • 16
  • This behavior comes from your dictionary 'out_dict_2' having several references to the same list. For more info on this behavior, see [this post.](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-why-is-this-and-how-can-i-prevent-it) – kcsquared Nov 02 '21 at 23:22

1 Answers1

0

I am not sure what list_2 is supposed to be. If list_2 is supposed to be a list of lists, then try:

for index in range(10):
   for line in list_2:
       print("index={0}, line = {1}".format(index, line))  #insert this line
   ...

I still am not sure what list_2 is. The following may give you what you want. If you could print the list_2 definition it would help:

list_2 = [['5.00', '-4601.24', '31.73'],
        ['11.04', '-4517.66', '32.51'],
        ['17.08', '-4418.59', '33.24'],
        ['23.12', '-4305.34', '33.93'],
        ['29.17', '-4179.09', '34.59']]

out_dict_2 = {'key_a': [[] for i in range(10)], 
              'key_b': [[] for i in range(10)],
              'key_c': [[] for i in range(10)]}

display(out_dict_2)

for lnum, line in enumerate(list_2):
    all_numeric = True
    row = []
    for item in line:
        try:
            row.append(float(item))
        except:
            print("skipped line: all values are not numeric:  {0}".format(line))
            all_numeric = False
    if all_numeric:
        out_dict_2['key_a'][lnum].append(row[0])
        out_dict_2['key_b'][lnum].append(row[1])
        out_dict_2['key_c'][lnum].append(row[2])
            
display(out_dict_2)
betacrash
  • 59
  • 7