Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Lists-compare lists
#1
i have a 2 set of lists each set has 4 sets , i would like to compare the  1st list(1st set) with the 2nd list (2nd set):
the code doesn't return any errors , it is just doesn't return the correct result:


for line in all_lines:
    start=line.start
    print start
    end=line.end
    print end'
  if cmp(start,end)==0:
        print "One shape"
    else:
        print "Not one shape"
Reply
#2
post your full code in code tags. also desired (expected) result, e.g.
Reply
#3
i would like The output to be "One shape" instead of "Not one shape" basically i want to compare the 1st list(1st set) with the 2nd list (2nd set),2nd list (1st set) to 3rd list (2nd set ) and so on.
import dxfgrabber
import math

dwg = dxfgrabber.readfile("test.dxf")
print("DXF version:{} ".format(dwg.dxfversion))
all_lines = [entity for entity in dwg.entities if entity.dxftype=='LINE']

for line in all_lines:
    start=line.start
    #for i in start:
        #print i
    print start
    print start[0]
    end=line.end
    #for n in end:
        #print n
    print end
    print end[0]
    if cmp(start,end)==0:
        print "One shape"
    else:
        print "Not one shape"
    #start_deff=end[0]-start[0]
    #end_deff=end[1]-start[1]
    #print math.sqrt(start_deff**2+end_deff**2)#
Output:
DXF version:AC1024  (1.586566659262402, 8.156562005030274, 0.0) 1.58656665926 (3.041123276594333, 11.37341563410535, 0.0) 3.04112327659 Not one shape (3.041123276594333, 11.37341563410535, 0.0) 3.04112327659 (7.550248640806287, 11.37341563410535, 0.0) 7.55024864081 Not one shape (7.550248640806287, 11.37341563410535, 0.0) 7.55024864081 (7.550248640806287, 9.9829047026913, 0.0) 7.55024864081 Not one shape (7.550248640806287, 9.9829047026913, 0.0) 7.55024864081 (8.277526949472247, 9.54707293644433, 0.0) 8.27752694947 Not one shape (8.277526949472247, 9.54707293644433, 0.0) 8.27752694947 (1.586566659262402, 8.156562005030274, 0.0) 1.58656665926 Not one shape
Reply
#4
You most mark your code,then add code tag.
Reply
#5
i have edited adding the tags
Reply
#6
I went in and corrected the tags.
You need to place the code between  the two tags (one starts, the other stops the tag area)
as snippsat suggests highlight the tag area before clicking the python button
Reply
#7
There are some basic error in that loop.
What happens when you assassin variables in a loop?  
for i in range(5):
   start = i
   end = i
So if test:
>>> start
4
>>> end
4
There are several way to compare list(cmp don't use it,cmp is removed in Python 3).
set is maybe the bests way or iterate over and compare.
# Iterate with  list comprehension
>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> [x for x in a if x in b]
[5]
>>> [x for x in a if x not in b]
[1, 2, 3, 4]

>>> # set
>>> set(a) & set(b)
{5}
>>> set(a) - set(b)
{1, 2, 3, 4}
Reply
#8
@snippsat: the OP is not clear in the explanations. S/he works with CAD dxf files and it looks like s/he want to compare the end of one line with start of the next line and see if they are connected (i.e. coordinates of the end (x,y[,z]) of line1 are the same as the start (x,y[,z]) of the line2). That's why set will not work in this case.
However I'm afraid it's not that simple for several reasons and here are some of them:
1. the lines in the list that he creates is not necessary drawn in the same order as they appear on the drawing, i.e. even if two lines are connected, they not necessary have consecutive indexes in that list
2. there may be one more groups of connected lines, not necessary connected with one another
3. there are different type of elements - lines, polylines, rays, etc and many more. each with its certain specifics.
4. there may be different layers in the drawing. I don't know if it's possible to have two lines that appear connected, but being drawn on different layers and how you shall treat these.


I'm not familiar with CAD and with the input file the OP works with. I started to look at the dxfgrabber packaged few days ago to help OP, but s/he doesn't dive into the docs.

import dxfgrabber
from collections import deque

def compare_lines(line1,line2):
   print 'line1: {}\nline1 start:{}\nline1 end: {}\nline2: {}\nline2 start:{}\nline2 end: {}'.format(line1, line1.start, line1.end, line2, line2.start, line2.end)
   if line1.end == line2.start:
       return 'one shape'
   else:
       return 'not one shape'

dwg = dxfgrabber.readfile("./samples/bike.dxf")
print("DXF version:{} ".format(dwg.dxfversion))

all_lines = [entity for entity in dwg.entities if entity.dxftype=='LINE']
# create second list with first line from the original list moved to the end
if all_lines:
   all_lines2 = all_lines[1:]
   first_line = all_lines[0]
   all_lines2.append(first_line)

   for line1, line2 in zip (all_lines, all_lines2):
       print compare_lines(line1, line2)

   # anoteher way - using collections.deque
   dq1 = deque(all_lines)
   dq2 = deque(all_lines)
   dq2.rotate(1)

   for line1, line2 in zip (dq1, dq2):
       print compare_lines(line1, line2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 673 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,533 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 736 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 798 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,419 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 737 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,268 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 763 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

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