Hi,
I am a beginner with python,and have this assignment that buggs me for some while..hope smb can help me...now, the thing is that I have merged the lists of view point positions of the camera in an function:

mergeList=list()
def linear_merge(list1, list2):
     mergelList = []
      for item in list1:
            mergelList.extend(item)
      for item in list2:
            mergelList.extend(item)
      return mergeList

and called the function in the body

merged = linear_merge(mergeProjectedPoints,projectedPoints)

where in loop

p = [_3dImage[i,j][0]*-1,_3dImage[i,j][1],_3dImage[i,j][2], 1] #camera coordinates
pp = matrix.MultiplyPoint(p) #world coordinates
mypoints.InsertNextPoint([_3dImage[i,j][0]*-1,_3dImage[i,j][1],_3dImage[i,j][2]])
a = conn.InsertNextCell(1)
projectedPoints.append([pp[0],pp[2]]))

...but how to access those same points in merged list (cuz I want to use them and display them on screen later in the code)...loop?how?

Recommended Answers

All 3 Replies

For one thing, I don't understand your linear_merge . Extend handles a whole list, not a single item at a time. I would do

def merge_to_new_list(list1,list2):
  ret = []
  ret.extend(list1)
  ret.extend(list2)
  return ret

Then, I do not understand your last question: how to access those same points in merged list (cuz I want to use them and display them on screen later in the code). You access them by index; and you know the index 'somehow' When you merge them the way you mention, you can access the first list's items at indices in [x for x in range(0,len(list1))] and the second lists items at indices in [len(list1)+x for x in range(0,len(list2))] Aside: Why do you want to merge the lists? What do you get from the merge? Why is it better to have one list than two? (And if you had two lists, would you be able to access the points in them?)

Another aside: You could just use list1.extend(list2) if you don't need list1 in its original form anymore.

You could just use list1.extend(list2) if you don't need list1 in its original form anymore.

There is also merged_list = list1 + List2 .

If you are doing the merge to loop the both lists in one go, you do not need to do so. Standard library way for this is to do itertools.chain(list1, list2).

import itertools as it
seq1 = (1,2,3)
seq2 = ['a', 'b','c']
print 'with itertools'
for item in it.chain(seq1, seq2):
    print item

print 'without itertools'
for item in (value
             for sequence in (seq1, seq2)
             for value in sequence):
    print item

print 'oneliner'
print('\n'.join(str(value)
             for sequence in (seq1, seq2)
             for value in sequence))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.