Parsing string array for differences

R

Ray Slakinski

My below code doesn't do what I need it to do. What I want is to
capture the differences between two arrays of data into a new list.
So if list A has {Fred, Bob, Jim} and List B has {Jim, George, Fred} I
want to capture in list C just {Fred, Jim}

y = 0
z = 0
data = {}
for element in listB:
x = 0
for element in listA:
if listA[x] != listB[y]:
listC[z] = listB[y]
z = z + 1
x = x +1
y = y + 1

Any and all help would be /greatly/ appreciated!

Ray
 
T

Tim Daneliuk

Ray said:
My below code doesn't do what I need it to do. What I want is to
capture the differences between two arrays of data into a new list.
So if list A has {Fred, Bob, Jim} and List B has {Jim, George, Fred} I
want to capture in list C just {Fred, Jim}

y = 0
z = 0
data = {}
for element in listB:
x = 0
for element in listA:
if listA[x] != listB[y]:
listC[z] = listB[y]
z = z + 1
x = x +1
y = y + 1

Any and all help would be /greatly/ appreciated!

Ray

http://pydoc.org/2.3/sets.html
 
J

Josiah Carlson

My below code doesn't do what I need it to do. What I want is to
capture the differences between two arrays of data into a new list.
So if list A has {Fred, Bob, Jim} and List B has {Jim, George, Fred} I
want to capture in list C just {Fred, Jim}

According to your example just given, you don't want the difference, you
want the similarity.

a = {}
a.fromkeys(listA)
listC_similarity = [i for i in listB if i in a]

Using a dictionary for the lookup makes it faster than searching another
list.

- Josiah
 
R

Ray Slakinski

Yup, someone on IRC introduced me to sets, and I love it :)

Thanks for the help guys.

Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top