-1

I am writing a script in order to calculate all the euclidean distances between a X value and a lot of other values in a dictonary, obtaining a float that, then, I convert in a list. The problem is I don't obtain a list with all the outcomes but many lists with only one element inside, my outcome. My script for the moment is:

single_mineral = {}                          
for k in new_dict.keys():
    single_mineral = new_dict[k]
    Zeff = single_mineral["Zeff_norm"]  
    rhoe =  single_mineral["Rhoe_norm"]
    eucl_Zeff= (calculated_Zeff_norm, Zeff)
    eucl_rhoe= (calculated_rhoe_norm, rhoe)
    dst= [(distance.euclidean(eucl_Zeff, eucl_rhoe))] 
    print(dst)

I obtain something like that:

[0.29205348037179407]

[0.23436642937625374]

[0.3835446564476642]

[0.11616594912309205]

[0.21792958584034935]

and they are not linked somehow (so I can't use intertools.chain).

I want to create a single list with all these lists (the final goal is the ascending order...for this reason I need only one list). I guess the solution is a for loop but I have no idea how to do it. I don't understand where it needs to run and how can I add my outcomes, which are always called "dst"?

Please, help me! Thank you very much in advance!

  • You can use `list.extend(list)` or simply `list += list2` to merge two lists. You can then use either `list.sort()` or `sorted(list)` to sort the collection – Joshua Nixon Dec 09 '20 at 21:01
  • You don't even obtain _many lists_. The only thing you do is print them out. Printing something is not the same as having it in a variable. You should create a list _outside the loop_ that will hold everything, and then `.append()` to that list every time you want to add something. – Pranav Hosangadi Dec 09 '20 at 21:02
  • you should create `dst = []` before loop. Inside loop you should use `dst.append( distance.... )` to get all values in one list. And print this list after loop. And then you see one list with many values. – furas Dec 09 '20 at 21:04
  • The problem is I don't need to merge only two lists, but they are several and they don't have a name so I can't use the attribute you suggested me. Here I found a similar problem (https://stackoverflow.com/questions/13481703/merge-different-lists-together-in-python) and the only solution seems to be a loop, but I cam't understand how doing it. Even creating an empty list outside the loop, I obtain a syntax error. :-( I can't understand why... Thank's a lot for your help! – Margherita Dec 09 '20 at 21:32

1 Answers1

0

if you want to do get all in one list then you need

# before loop

dst = []

# loop

for k in new_dict.keys():
    # ... code ...

    #dst.append( [distance.euclidean(eucl_Zeff, eucl_rhoe)] ) 
    dst.append( distance.euclidean(eucl_Zeff, eucl_rhoe) ) 

# after loop

print(dst)
furas
  • 134,197
  • 12
  • 106
  • 148
  • I am not sure because I obtain always "invalid syntax"... what am I doing wrong? – Margherita Dec 09 '20 at 21:26
  • what `invalid syntax` ? You didn't create minimal working code so I can't run it and see this error. – furas Dec 09 '20 at 21:30
  • Invalid syntax where I have "print(dst)". Is it weird, isn't it? – Margherita Dec 09 '20 at 21:34
  • maybe you forgot some `)` in previous line – furas Dec 09 '20 at 21:34
  • maybe bettter add new code in question - but don't change previous one. OR create new question on new page - you will have more space for questiona and maybe new people will see it. – furas Dec 09 '20 at 21:37
  • Found it! I have to create an empty list listoflists = [] , then dst= [(distance.euclidean(eucl_Zeff, eucl_rhoe))] listoflists.append(dst) – Margherita Dec 09 '20 at 21:38