1

Say you have 3 lists

List1 = [['_', '_', '_'], ['_', '_', '_'], ['_','_','_']]
List2 = [['Test', 'Word', 'Sudo'], ['Fu', 'Lu', 'Shou'], ['Ham', 'Spam', 'Eggs']]
List3 = [3, 5, 7,]

Using the values from List3, I'd like to transfer 'Fu' from List2[0][2] into List1[0][2], because the first value of List3 is a 3, which means take the 3rd value (counting from 0 it's list2[0][2]) from List2 and place it into the same spot as List1

The final result, using the other values in List3, should be:

List1 = [['_', '_', 'Fu'], ['_', 'Shou', '_'], ['Spam','_','_']]

I've been at it for a few hours but can't get it to work!!

How is this done?

Hailey Monette
  • 97
  • 1
  • 1
  • 7
  • 1
    wouldn't it be easier to have each list of triplets as a single list, do the transfers, and then split them up into the groups of 3? – Jainathan Leung Apr 10 '13 at 18:43
  • Are all of the sublists the same length? – mgilson Apr 10 '13 at 18:44
  • @Jaynathan I think that's redundant, because the sublists would have to be added together again. @mgilson! Yes, all the sublists are the same length – Hailey Monette Apr 10 '13 at 18:46
  • The 3rd value is at index 2 (`0` <- first, `1` <- second, `2` <- third) – jadkik94 Apr 10 '13 at 18:46
  • I think if you 'flatten' your lists, you may have a very easy time of things: http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python ... in another step, you can then re-chunk your lists – pyInTheSky Apr 10 '13 at 18:48
  • but if you consider list2 as flat 3 equal to 'Fu' and 5 equal to 'Shou' . there is mistake in your description – Moj Apr 10 '13 at 19:15

4 Answers4

6
In [184]: List1 = [['_', '_', '_'], ['_', '_', '_'], ['_','_','_']]

In [185]: List2 = [['Test', 'Word', 'Sudo'], ['Fu', 'Lu', 'Shou'], ['Ham', 'Spam', 'Eggs']]

In [186]: List3 = [3, 5, 7,]

In [187]: for x in List3:
    q,r=divmod(x,3)
    List1[q][r]=List2[q][r]
   .....:     

In [188]: List1
Out[188]: [['_', '_', '_'], ['Fu', '_', 'Shou'], ['_', 'Spam', '_']]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
2
>>> List1 = [['_', '_', '_'], ['_', '_', '_'], ['_','_','_']]
>>> List2 = [['Test', 'Word', 'Sudo'], ['Fu', 'Lu', 'Shou'], ['Ham', 'Spam', 'Eggs']]
>>> List3 = [3, 5, 7,]
>>> List4 = [item for sublist in List1 for item in sublist]
>>> List5 = [item for sublist in List2 for item in sublist]
>>> for val in List3:
...     List4[val] = List5[val]
>>> List1 = [ List4[i:i+3] for i in xrange(0,len(List4),3) ]
pyInTheSky
  • 1,459
  • 1
  • 9
  • 24
1
# If you must have original data as lists of lists:
def flat(lst):
    ret = []
    for x in lst:
        if hasattr(x, '__iter__'):
            ret += flat(x)
        else:
            ret.append(x)
    return ret

List1 = [['_', '_', '_'], ['_', '_', '_'], ['_','_','_']]
List2 = [['Test', 'Word', 'Sudo'], ['Fu', 'Lu', 'Shou'], ['Ham', 'Spam', 'Eggs']]
List3 = [3, 5, 7,]

lst1 = flat(List1)
lst2 = flat(List2)

# Now given flat lists, you can just do this:
def splitby(x, n=3):
    i = iter(x)
    while True:
        yield [next(i) for _ in range(n)]

for i in List3:
    lst1[i] = lst2[i]

print list(splitby(lst1))
gatto
  • 2,947
  • 1
  • 21
  • 24
  • Haha, yeah I could just do that, but a lot of your code I don't even understand yet! – Hailey Monette Apr 10 '13 at 19:08
  • Well, [pyInTheSky](http://stackoverflow.com/users/411046/pyinthesky)'s code is simpler and I guess more efficient for your task, but the idea is the same. I'd use his code if I were you =) – gatto Apr 10 '13 at 19:10
1
for index in List3:
    first, second = index/3, index%3 
    List1[first][second] = List2[first][second]
print List1
Zangetsu
  • 1,900
  • 17
  • 25
  • 1
    Thanks a lot! This answer I think is the best one, it's very simple and easy to understand. Someone else posted this same exact answer earlier, and I was lucky enough to copy down the code, but when I refreshed the page, the answer was gone. So I tried out the code, and this one works best in my opinion, very simple! – Hailey Monette Apr 10 '13 at 19:13
  • Actually can you explain how this works? I think I understand it a little bit, but I'm still not solid on how this method works! – Hailey Monette Apr 11 '13 at 03:53
  • 1
    The third list is 1-d array. It should be broken down into 2-d array. Here "first" is the position of the inner list and "second" is the position of each member in that inner list. – ashokadhikari Apr 11 '13 at 04:43