-4

I have this list of lists:

a = [[741.0, 743.0, 3386.0, 284577.0, 290611.0, 300889.0, 305256.0, 917458.0, 917905.0, 917906.0, 922187.0, 925852.0, 1260021.0, 1377096.0, 1524210.0, 1680657.0, 1692571.0, 1692645.0, 1692647.0, 1713958.0, 1801008.0, 1818975.0, 1858888.0, 1880544.0, 1880898.0, 1880899.0, 1880900.0, 1881062.0, 1881073.0, 1881240.0, 1881433.0, 1881434.0, 1881435.0, 1881436.0, 1881438.0, 1958358.0, 1958478.0, 1958479.0, 1958481.0, 1967310.0], [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

and i want to create two different 1 dimension-lists:

b[0] = [741.0, 743.0, 3386.0, 284577.0, 290611.0, 300889.0, 305256.0, 917458.0, 917905.0, 917906.0, 922187.0, 925852.0, 1260021.0, 1377096.0, 1524210.0, 1680657.0, 1692571.0, 1692645.0, 1692647.0, 1713958.0, 1801008.0, 1818975.0, 1858888.0, 1880544.0, 1880898.0, 1880899.0, 1880900.0, 1881062.0, 1881073.0, 1881240.0, 1881433.0, 1881434.0, 1881435.0, 1881436.0, 1881438.0, 1958358.0, 1958478.0, 1958479.0, 1958481.0, 1967310.0]

and

b[1] = [0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

with a for loop so that i can do the same if i have 3 or more lists inside the a list...

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Thanos Smar
  • 631
  • 1
  • 8
  • 12
  • Do you need to use a for loop? It may be easier just to refer to the nested lists by index. For example ``a[0] = myOneDimensionalListOne`` and ``a[1] = myOneDimensionalListTwo``. – Igor May 18 '16 at 15:10
  • 1
    Use a simple unpacking `b0, b1 = a` – Mazdak May 18 '16 at 15:11
  • 1
    @Kasramvd woah, I thought you could only do this with tuples. Neat! Good to know. – Igor May 18 '16 at 15:15
  • You can do it, it doesn't seem to be a good idea though. http://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop-python – Kapten Koben May 18 '16 at 15:20

2 Answers2

2

"i understand what you are saying..lets assume i have the a list of lists...how can i make a list of lists with the differences between the values from each list of a?? – Thanos Smar 1 hour ago"

if the list is called a[0] or a doesn't make a difference, you do it the exact same way except you loop over a[0][x] .. a[1][x] ... instead of a[x] ... b[x] .... It wouldn't be easier to have them as separate lists, it's the exact same thing. Basically you have them as separate variables already, they're just called a[0] ... a[1] and so on.

EDIT - This would be a way to do it:

>>> mylist = [[6, 6, 6], [1, 1, 1], [1, 2, 3]]
>>> mylist2 = list(mylist[0])
>>> for x in range(1, len(mylist)):
        for y in range(0, len(mylist[x])):
            mylist2[y] = mylist2[y] - mylist[x][y]


>>> mylist2
[4, 3, 2]
>>> 

using mylist2 = list(mylist[0]) makes it a new list and not change the values of the original one.


EDIT 2:

This should be it.

>>> myList = [[10, 5, 6, 7, 8], [9, 5, 6, 7, 8], [8, 5, 6, 7, 8]]
>>> myList2 = []
>>> for x in range(0, len(myList)):
        subList = []
        for y in range(0, len(myList[x])-1):
            subList.append(myList[x][0] - myList[x][y+1])
        myList2.append(subList)


>>> myList2
[[5, 4, 3, 2], [4, 3, 2, 1], [3, 2, 1, 0]]
>>> 

myList2 is a list of lists with the subtracted values of the first element of each list in myList. If this isn't it, you should be able to easily modify it to make it do what you want.


EDIT3:

Ok, so this is what you want then. I'm sure an actual good programmer could do this more elegant with generators or something. I haven't delved into that yet since I'm pretty much a beginner at this, but it works.

myList = [[10, 5, 6, 7, 8], [9, 5, 6, 7, 8], [8, 5, 6, 7, 8]]
myList2 = []


for x in range(len(myList)):
        subList1 = [] 
        for y in range(len(myList[x])): 
            subList2 = [] 
            for z in range(len(myList[x])): 
                subList2.append(myList[x][y] - myList[x][z]) 
            subList1.append(subList2)
        myList2.append(subList1)

# - First loop goes through all lists in myList
# - Create an empty subList to append lists of subLists to
# - Second loop goes through all positions in list x of mylist
# - Create another empty subList2 to append the actual values to
# - Third loop does the same as loop 2.
# - Each value in myList from the second loop (y) gets - each value from myList
#        (including itself) from the third loop (z) and appended to a subList2
# - The subList2 is appended to subList1 creating a subList of subLists
# - Finally the list of subLists is appended to myList2 for each turn of the 
#        first loop

This will give the resulting list of lists of lists:

myList2
[[[0, 5, 4, 3, 2], [-5, 0, -1, -2, -3], [-4, 1, 0, -1, -2], [-3, 2, 1, 0, -1], [-2, 3, 2, 1, 0]], 
 [[0, 4, 3, 2, 1], [-4, 0, -1, -2, -3], [-3, 1, 0, -1, -2], [-2, 2, 1, 0, -1], [-1, 3, 2, 1, 0]], 
 [[0, 3, 2, 1, 0], [-3, 0, -1, -2, -3], [-2, 1, 0, -1, -2], [-1, 2, 1, 0, -1], [0, 3, 2, 1, 0]]]

myList2[0][0] contains all the values of myList[0][0] compared to myList[0][x]

myList[0][1] contains all the values of myList[0][1] compared to myList[0][x]

and so on...

If you had been more explicit of what you were hoping to accomplish from the get go I'm sure one of the good programmers would have answered you too. I'm doing this because I need the practice.

  • i want to append the subtraction between all values in every list...N = 2(the number of lists a has)............................................................................‌​..............................for i in range(N): c.append([]) for j in range(R): c[i].append([]) for k in range(R): c[i][j].append(b[j] - b[k]) print b...........R = 40,number of values in each list – Thanos Smar May 18 '16 at 16:50
  • Ok, so, if you have 3 lists for example: a = [5, 5, 5, 5] b = [4, 3, 2, 1] c = [0, 1, 2, 3] you want a fourth list which is: d = [(5-4-0), (5-3-1), (5-2-2), (5-1-3)] i.e. d = [1, 1, 1, 1] Is that correct? – Kapten Koben May 18 '16 at 16:53
  • @ Kapten Kober if my list is b = [[741.0, 743.0, 3386.0, 917458.0, 917905.0, 917906.0, 922187.0, 925852.0,1692647.0, 1713958.0, 1801008.0, 1818975.0, 1858888.0, 1880544.0, 1880898.0, 1880899.0, 1880900.0, 1881062.0, 1881073.0, 1881240.0, 1881433.0, 1881434.0, 1881435.0, 1881436.0, 1881438.0, 1958358.0, 1958478.0, 1958479.0, 1958481.0, 1967310.0], [0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], i want to create a c[i] list so that it will have c[0] = difference between all values between them, eg. 741.0 - 743.0, 741.0 - 3386.0 and so on for b[1] too – Thanos Smar May 18 '16 at 17:02
  • I think I got it, check the edit. – Kapten Koben May 18 '16 at 17:33
  • Ah, i didn't get it, but that isn't harder and can be done about the same way. Can check it later. – Kapten Koben May 18 '16 at 17:47
  • check and tell me :D – Thanos Smar May 18 '16 at 17:55
  • So, is c[0] supposed to have the values only compared to the first one or between all? So is it c[0] = [b[0][0] - b[0][1], b[0][0] - b[0][2], b[0][0] - b[0][3] ....] or is it c[0] = [b[0][0] - b[0][1], b[0][1] - b[0][2], b[0][2] - b[0][3] ....] ? – Kapten Koben May 18 '16 at 19:52
  • it is c[0] = [b[0][0] - b[0][1], b[0][0] - b[0][2], b[0][0] - b[0][3] ....] – Thanos Smar May 18 '16 at 22:22
  • exactly!!!THANK YOU !! – Thanos Smar May 19 '16 at 09:26
  • @Kaptain Koben can you tell me what should i add to this code you wrote so that i get myList2 = [[10-10,10-5,10-6,10-7,10-8,5-10,5-5,5-6,5-7,5-8....],[the same way for the second list],[the same way for the third list]]??? and if you can explain these two lines of your code please: for y in range(0, len(myList[x])-1): subList.append(myList[x][0] - myList[x][y+1])...why len(myList[x])-1) and (myList[x][0] - myList[x][y+1]) ??? thank you :) – Thanos Smar May 19 '16 at 20:52
  • Edited again. You need a third for loop to do that. The lines in the code you wanted explained is because I didn't think you wanted the value compared to itself and I only thought you wanted the first value compared to the rest, not all compared to all. This is not a super efficient piece of code if there's lots and lots of values to compare, I'm sure it can be done better in that case, but this is quite easy to understand I think. Added comments this time. – Kapten Koben May 19 '16 at 21:53
  • @Kaptain Koben you have been more than helpful...thank you really much!! – Thanos Smar May 20 '16 at 00:14
1

It doesn't really make any sense.

You have a list of lists (in your case, a). Then you ask how to use a for loop to create 2 separate lists, b[0] and b[1], which can already be achieved by directly accessing a[0] and a[1].

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • I need to do it because i will need the one dimensional lists to create lists of differences between every value in the list with every other value in a list...i cant thing another way... – Thanos Smar May 18 '16 at 15:24
  • @ThanosSmar I still don't see why it matters if the variable is called `a[0]` or `b[0]`. – DeepSpace May 18 '16 at 15:26
  • i understand what you are saying..lets assume i have the a list of lists...how can i make a list of lists with the differences between the values from each list of a?? – Thanos Smar May 18 '16 at 15:29
  • N = 2(the number of lists a has)..........................................................................................................for i in range(N): c.append([]) for j in range(R): c[i].append([]) for k in range(R): c[i][j].append(b[j] - b[k]) print b...........R = 40,number of values in each list – Thanos Smar May 18 '16 at 15:50