Help me out with bugs in my list ?
Christophe Delord
christophe.delord at free.fr
Fri Nov 8 01:23:59 EST 2002
Hi,
You can use zip to make tuples from a list and check unicity of the tuples
instead of its components.
For example:
-----
def uzip(*Ls):
""" zip filtering duplicate tuples """
L = []
for x in zip(*Ls): # for each tuple
if x not in L: # filter duplicate tuples
L.append(x)
return zip(*L) # returns a list for each item of the tuples
binding_list = ["ls","ls","ls","netscape","netscape","lib","lib"]
to_list = ["ls","lib","ls","netscape","ls","lib","lib"]
new_binding_list, new_to_list = uzip(binding_list, to_list)
print "new_binding_list:", new_binding_list
print "new_to_list :", new_to_list
-----
new_binding_list: ('ls', 'ls', 'netscape', 'netscape', 'lib')
new_to_list : ('ls', 'lib', 'netscape', 'ls', 'lib')
-----
Christophe.
On Thu, 7 Nov 2002 20:18:29 -0800 (PST)
Mindy <csshi99 at yahoo.com> wrote:
> Hey, I have two lists like:
> binding_list =
> ["ls","ls","ls","netscape","netscape","lib","lib"]
>
> to_list =
> ["ls","lib","ls","netscape","ls","lib","lib"]
>
> I want to get non-duplicated pairs of strings from
> them like
> (binding_list[i],to_list[i]) by eliminating the
> duplicated pairs. For the above two lists, I want to
> get two new lists:
> binding_list = ['ls', 'ls', 'netscape', 'netscape',
> 'lib']
> to_list = ['ls', 'lib', 'netscape', 'ls', 'lib']
>
> My codes are like the following, but I got the result
> as:
> binding_list = ['ls', 'ls', 'netscape', 'netscape',
> 'lib','lib']
> to_list = ['ls', 'lib', 'netscape', 'ls', 'lib','lib']
>
> With the last two pairs duplicated. I know I was wrong
> because at the last loop, index1 = 4, index2 =1, even
> though the fifth pair is the same with the fourth
> pair, my codes can't detect this. Could anyone help me
> to get a correct piece of codes? Thanks so much!
>
> My codes:
> ----------------
> for i in range(1,len(binding_list)):
> if (i == len(binding_list)):
> break
> index1 = binding_list.index(binding_list[i])
> index2 = to_list.index(to_list[i])
> if ((index1 >=0)and(index2 >=0)and(index1==index2)):
> del binding_list[i]
> del to_list[i]
>
> print binding_list
> print to_list
>
>
>
> =====
> Cheers
> -Mindy
>
> __________________________________________________
> Do you Yahoo!?
> U2 on LAUNCH - Exclusive greatest hits videos
> http://launch.yahoo.com/u2
>
--
(o_ Christophe Delord _o)
//\ http://christophe.delord.free.fr/ /\\
V_/_ mailto:christophe.delord at free.fr _\_V
More information about the Python-list
mailing list