Python Lists [ ] [SOLVED]



  • On 17/12/2014 at 07:31, xxxxxxxx wrote:

    Say I have three Lists:

    list_A = [(1,2,3,4,5), (1,2,3,4,5)]
          list_B = [(1,2,6,7,5), (1,2,6,7,5)]
          list_C = [ ]

    1]  I want to compare "list_A" and "list_B"
    2]  Then append  "list_C" by taking the duplicates and turn them to "0"

    list_C = [(0,0,6,7,0),(0,0,6,7,0)]

    I have tried set(list_A) - set(list_B) to compare but it doesn't work

    Any help please 😢



  • On 17/12/2014 at 08:02, xxxxxxxx wrote:

    This is how I would do it by comparing the same index of each list:

    list_A = [1,2,3,4,5]
    list_B = [1,2,6,7,5]
    list_C = [ ]
      
    for i in range(len(list_A)) :
        if list_A[i] == list_B[i]:
            list_C.append(0)
        else:
            list_C.append(list_B[i])
      
    print list_C
    


  • On 17/12/2014 at 08:09, xxxxxxxx wrote:

    @ShawnFrueh

    I have update my post a little, sorry 😊

    I will take a look if your solution works tonight, thanks



  • On 17/12/2014 at 12:09, xxxxxxxx wrote:

    To explain better I want to achieve this.
    This script is to move certain points of my cube with python but this script is a little long and I'm sure there is a simpler shorter way to do this.

        obj = doc.SearchObject("Null")
        poly = doc.SearchObject("Polygon")
        
        Points_List = [" 0, -100, -100, -100 ", " 1, -100, 100, -100 ", " 2, 100, -100, -100 ", " 3, 100, 100, -100 ", " 4, 100, -100, 100 ", " 5, 100, 100, 100 ", " 6, -100, -100, 100 ", " 7, -100, 100, 100 "]
        Targ_List = [" 0, -100, -100, -100 ", " 1, -100, 60.326, -100 ", " 2, 100, -100, -100 ", " 3, 100, 100, -59.691 ", " 4, 100, -100, 100 ", " 5, 100, 165.287, 100 ", " 6, -100, -100, 100 ", " 7, -100, 15.892, 100 "]
        
        cntrl = obj[c4d.ID_USERDATA,1]
        
        N_des_list = []    
        cmp_L_A = []
                
        for i,line in enumerate(Points_List) :
            
            in_L = Points_List[i]
                    
            in_line = in_L.split(",")
            
            in_nr = int(in_line[0])
            in_vec_X = float(in_line[1])
            in_vec_Y = float(in_line[2])
            in_vec_Z = float(in_line[3])
                     
            tar_L = Targ_List[i]
                    
            tar_line = tar_L.split(",")
            
            tar_nr = int(tar_line[0])
            tar_vec_X = float(tar_line[1])
            tar_vec_Y = float(tar_line[2])
            tar_vec_Z = float(tar_line[3])
                   
            if in_vec_X == tar_vec_X:            
               N_x = 0          
            else:
               N_x = tar_vec_X     
            
            if in_vec_Y == tar_vec_Y:
               N_y = 0
            else:
               N_y = tar_vec_Y     
            
            if in_vec_Z == tar_vec_Z:
               N_z = 0
            else:
               N_z = tar_vec_Z
                     
            N_des_list.append(c4d.Vector(N_x,N_y,N_z))
       
        for line in Points_List:
            
            coord = line.split(",")
               
            nr = int(coord[0])
            Vec_X = float(coord[1])
            Vec_Y = float(coord[2])
            Vec_Z = float(coord[3])
            
            cmp_L_A.append(c4d.Vector(Vec_X,Vec_Y,Vec_Z))
        
        for i in range(len(cmp_L_A)) :
            
            morph = cntrl*N_des_list[i] + cmp_L_A[i]   
            poly.SetPoint(i,morph)
           
            poly.Message(c4d.MSG_UPDATE)
    


  • On 18/12/2014 at 03:01, xxxxxxxx wrote:

    Hello,

    the way Shawn show it, is a good way to go.
    A few tips
    For an enumeration you don´t need an index in your List.
    If you use a vector you can access the coordinates with e.g vec = c4d.Vector(3,0,0) vec.x=3.
    in xrange is faster.
    You might use your slider control directly in the xrange iteration, therefore you don´t need to iterate twice and the list number 3 is not necessary.
    But to get started:

      
    import c4d  
      
      
    def main() :  
        
      List_1=[c4d.Vector(0,1,0),c4d.Vector(5,2,0),c4d.Vector(3,0,3)]  
      List_2=[c4d.Vector(1,1,0),c4d.Vector(5,2,0),c4d.Vector(0,3,0)]  
      List_3=[]  
      for i in xrange(len(List_2)) :  
            
          if List_2[i].x == List_1[i].x:  
              List_2[i].x=0    
          if List_2[i].y == List_1[i].y:  
              List_2[i].y=0    
          if List_2[i].z == List_1[i].z:   
              List_2[i].z=0     
            
          List_3.append(List_2[i])      
                
      print List_3          
        
    if __name__=='__main__':  
      main()  
      
    

    Best wishes
    Martin

    EDIT:

    If you do not write the list by your own and it is the type of list you show, than this is a way to access the items e.g.:

      
      Points_List = [" 0, -100, -200, -100 ", " 1, -100, 100, -100 ", " 2, 100, -100, -100 "]  
      #first member third item  
      print Points_List[0].split(",")[2]  
    


  • On 18/12/2014 at 03:31, xxxxxxxx wrote:

    Thanks Martin for all your help. Slowly but I'm getting there👍


Log in to reply