Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving list in a list
#1
Hello I am trying to save my list into another list. I actually do it but it is saved like

array([0, 0]), array([0, 1])
However I want to save like that:

[0,1],[0,0]...
my code is here:

results_ = []
measurements = results.samples[0] # I could manage to get my list like [0,0] [1,1]
print("measurements",measurements) # I am printing my measurements and it is ok they are coming like [0,0][1,1]
results_.append(measurements) # I am adding to another list and here is the problem.they are coming : array([0, 0]),array([0, 1])
print("results",results_)    
sebep = sorted(results_)    
print(results_)
here is my output:
Output:
measurements [1 1] measurements [0 1] measurements [0 0] measurements [0 1] measurements [0 0] measurements [1 1] measurements [0 1] measurements [0 1] results [array([1, 1]), array([0, 1]), array([0, 0]), array([0, 1]), array([0, 0]), array([1, 1]), array([0, 1])
Reply
#2
measurements is set from results (which is empty to begin with)
then you try to append measurements back to results.
Think about what you are doing here.
Does it make sense?
Reply
#3
measurements is return a NumPy Array
>>> import numpy as np
>>>
>>> n1 = np.array([0, 1])
>>> n1
array([0, 1])
>>> type(n1)
<class 'numpy.ndarray'>
So will work like a normal Python list,but you can convert.
>>> import numpy as np
>>> 
>>> n1 = np.array([0, 1])
>>> n2 = np.array([1, 2])
>>> lst = []
>>> # add list 
>>> lst.append(list(n1))
>>> lst.append(list(n2))
>>> lst
[[0, 1], [1, 2]]
On a finish list.
>>> import numpy as np
>>> 
>>> n1 = np.array([0, 1])
>>> n2 = np.array([1, 2])
>>> lst = []
>>> lst.append(n1)
>>> lst.append(n2)
>>> lst
[array([0, 1]), array([1, 2])]
>>> 
>>> new_lst = [list(i) for i in lst]
>>> new_lst
[[0, 1], [1, 2]] 
If it's a NumPy Array with serval item use .tolist.
>>> import numpy as np
>>> 
>>> m = np.array([[0,1], [2,3]])
>>> m
array([[0, 1],
       [2, 3]])
>>> m.tolist()
[[0, 1], [2, 3]]
quest_ likes this post
Reply
#4
(Mar-08-2021, 02:41 PM)snippsat Wrote: measurements is return a NumPy Array
>>> import numpy as np
>>>
>>> n1 = np.array([0, 1])
>>> n1
array([0, 1])
>>> type(n1)
<class 'numpy.ndarray'>
So will work like a normal Python list,but you can convert.
>>> import numpy as np
>>> 
>>> n1 = np.array([0, 1])
>>> n2 = np.array([1, 2])
>>> lst = []
>>> # add list 
>>> lst.append(list(n1))
>>> lst.append(list(n2))
>>> lst
[[0, 1], [1, 2]]
On a finish list.
>>> import numpy as np
>>> 
>>> n1 = np.array([0, 1])
>>> n2 = np.array([1, 2])
>>> lst = []
>>> lst.append(n1)
>>> lst.append(n2)
>>> lst
[array([0, 1]), array([1, 2])]
>>> 
>>> new_lst = [list(i) for i in lst]
>>> new_lst
[[0, 1], [1, 2]] 
If it's a NumPy Array with serval item use .tolist.
>>> import numpy as np
>>> 
>>> m = np.array([[0,1], [2,3]])
>>> m
array([[0, 1],
       [2, 3]])
>>> m.tolist()
[[0, 1], [2, 3]]

Thanks you very much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why changing data in a copied list changes the original list? plumberpy 3 368 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 923 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Order a list with successive permutations based on another list yvrob 3 753 Mar-19-2021, 08:20 AM
Last Post: supuflounder
Star Convert Bytearray into List using list() Shlok 2 658 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  Adding List Element if Second part of the List Elements are the Same quest_ 3 754 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 722 Nov-03-2020, 05:37 AM
Last Post: deanhystad
Question Save list with nested list into CSV SpongeB0B 1 1,412 Oct-12-2020, 07:26 AM
Last Post: bowlofred
  Appending to list of list in For loop nico_mnbl 2 677 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  How to make a list of values from a dictionary list? faryad13 2 745 Sep-03-2020, 03:45 PM
Last Post: faryad13
  Append list into list within a for loop rama27 2 868 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020