Re: [Tutor] how to remove the coming duplication
I was so miffed at not reading the OP's mail carefully that I wrote a one liner. Ok. Here it goes: def no_adjacent_dup(lst): return [ x for x, y in zip(lst, lst[1:]) if x != y] + [lst[-1]] :-) Enjoyed working that one out; but whether it is a good solution Asokan Pichai ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
snip Thanks for all, I found the problems I faced is more tricky than the simple list I gave. Here the list is: a row of numbers, not one number, such as print a_list[1] is: 1 1 9 7 7 9 9 9 print(a_list) is: 617 617 790 571 571 790 790 790 I attached the codes written based on the suggestions all you have given, and also attached the to-be-handled file in the following link: #!/usr/bin/python3 import os.path INFILEEXT=.out OUTFILEEXT=.bri atoms=[] def fetchonefiledata(infilename): for line in open(infilename,r): parts=line.strip().split() atoms=parts[2] print(atoms[0]) def remove_coming_duplications(a_list): for idx, element in enumerate(a_list): if element != a_list[idx-1]: print(element) if __name__==__main__: for filename in os.listdir(.): base, ext = os.path.splitext(filename) if ext == INFILEEXT: fetchonefiledata(filename) remove_coming_duplications(atoms) https://docs.google.com/open?id=0B93SVRfpVVg3MjVlODdiOWYtY2FlYy00NDIzLThjMzAtMDk0NTQ4ZTRjZjRh Thanks with best regards, P.S My primary interest is getting the lists from different files, hopefully in the end find some rules between those numbers. or pathway, such as 1 2 4 8 7 6 4 8 7 6 5 1 so it shares a feature of 4 - 8 - 7 - 6. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
Christian Witts wrote: def remove_coming_duplication(a_list): return [element for idx, element in enumerate(a_list) if element != a_list[idx-1]] Beware of negative indices: remove_coming_duplication([1, 2, 1]) [2, 1] # should be [1, 2, 1] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
Asokan Pichai wrote: On Thu, Nov 10, 2011 at 2:07 PM, Peter Otten __pete...@web.de wrote: Christian Witts wrote: def remove_coming_duplication(a_list): return [element for idx, element in enumerate(a_list) if element != a_list[idx-1]] Beware of negative indices: remove_coming_duplication([1, 2, 1]) [2, 1] # should be [1, 2, 1] I ran into that and hence I chose to zip, compare and add the last element I have one for you, too ;) def no_adjacent_dup(lst): ...return [ x for x, y in zip(lst, lst[1:]) if x != y] + [lst[-1]] ... no_adjacent_dup([]) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 2, in no_adjacent_dup IndexError: list index out of range And a subtle one as a bonus: no_adjacent_dup([1, 1.0]) [1.0] # should be 1 Here's a highlevel approach, probably not very efficient: from itertools import groupby [k for k, g in groupby([1, 1.0, 2, 1, 3, 3, 3])] [1, 2, 1, 3] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
On 2011-11-10 09:26, lina wrote: atoms=[] def fetchonefiledata(infilename): for line in open(infilename,r): parts=line.strip().split() atoms=parts[2] print(atoms[0]) First you define atoms as an empty list, but in the line atoms = parts[2] you are redefining it by assigning a string to it. Thus, at the end of the for-loop, only the last string is stored (in every iteration you overwrite the former value with a new one). You probably want to append the values: atoms.append(parts[2]) Bye, Andreas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
lina wrote: Hi, How to remove the coming duplication, Here I wrote one (not working): a=['2', '5', '7', '5', '5'] [...] I wish to get a is [2,5,7,5] just remove the coming duplication, not unique the list. a = [2, 5, 7, 5, 5] b = a[0:1] # slice of the first item only for x in a[1:]: # slice of the second item to the end if x != b[-1]: b.append(x) gives b = [2, 5, 7, 5] as requested. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
On Thu, Nov 10, 2011 at 8:48 PM, Steven D'Aprano st...@pearwood.info wrote: lina wrote: Hi, How to remove the coming duplication, Here I wrote one (not working): a=['2', '5', '7', '5', '5'] [...] I wish to get a is [2,5,7,5] just remove the coming duplication, not unique the list. a = [2, 5, 7, 5, 5] b = a[0:1] # slice of the first item only for x in a[1:]: # slice of the second item to the end if x != b[-1]: b.append(x) for index, b in enumerate([b_list]): print(b) how can I avoid the output not as: ['53', '76', '57'] but as 53 76 57 Thanks gives b = [2, 5, 7, 5] as requested. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
b = [] b=b.append(a[i]) for i in range(len(a)) if a[i] != b[-1] showed me: SyntaxError: invalid syntax ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
for i in range(len(a)): if i == 0: b.append(a[i]) if a[i]!=b[-1]: b.append(a[i]) This one seems work, but looks clumsy, Thanks for any suggestions that helps to improve. Best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
Bad to reply to my own post. Failed to understand the just remove the coming duplication, not unique the list. Sorry; ignore my ideas Asokan Pichai ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
Try this a=['2', '5', '7', '5', '5'] d=list(set(a)) d ['2','5','7'] Is there any reason why your numbers are in quotes??? Hope this helps. Sent from my BlackBerry wireless device from MTN -Original Message- From: lina lina.lastn...@gmail.com Sender: tutor-bounces+delegbede=dudupay@python.org Date: Thu, 10 Nov 2011 15:10:57 To: tutorTutor@python.org Subject: [Tutor] how to remove the coming duplication Hi, How to remove the coming duplication, Here I wrote one (not working): a=['2', '5', '7', '5', '5'] for i in range(len(a)-1): if a[i+1]==a[i]: a.remove(a[i+1]) if i not in range(len(a)): break a ['2', '7', '5', '5'] I wish to get a is [2,5,7,5] just remove the coming duplication, not unique the list. Thanks for any advice, Best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
Hello, list.remove will remove the first occurrence of the value from the list. So the output is expected as far as Python is concerned. May be you should think about using pop function. Please take a look at the code below def RemoveComingDuplicates(a): for i in range(len(a)-1): print i,i+1 if a[i+1] == a[i]: a.pop(i+1) print a a = ['2','5','7','5','5'] RemoveComingDuplicates(a) 0 1 ['2', '5', '7', '5', '5'] 1 2 ['2', '5', '7', '5', '5'] 2 3 ['2', '5', '7', '5', '5'] 3 4 ['2', '5', '7', '5'] Hope this helps. Thanks and Regards, Sumod On Thu, Nov 10, 2011 at 12:54 PM, lina lina.lastn...@gmail.com wrote: for i in range(len(a)): if i == 0: b.append(a[i]) if a[i]!=b[-1]: b.append(a[i]) This one seems work, but looks clumsy, Thanks for any suggestions that helps to improve. Best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- http://spawgi.wordpress.com We can do it and do it better. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
You have the error because the square brackets are missing. You should have done this: b=[b.append(a[i]) for i in range(len(a)) if a[i] != b[-1]] This would however give you an index out of range error. That said, may I ask what it is exactly you are trying to achieve? Cheers. Sent from my BlackBerry wireless device from MTN -Original Message- From: lina lina.lastn...@gmail.com Sender: tutor-bounces+delegbede=dudupay@python.org Date: Thu, 10 Nov 2011 15:20:49 To: tutorTutor@python.org Subject: Re: [Tutor] how to remove the coming duplication b = [] b=b.append(a[i]) for i in range(len(a)) if a[i] != b[-1] showed me: SyntaxError: invalid syntax ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to remove the coming duplication
On 2011/11/10 09:10 AM, lina wrote: Hi, How to remove the coming duplication, Here I wrote one (not working): a=['2', '5', '7', '5', '5'] for i in range(len(a)-1): if a[i+1]==a[i]: a.remove(a[i+1]) if i not in range(len(a)): break a ['2', '7', '5', '5'] I wish to get a is [2,5,7,5] just remove the coming duplication, not unique the list. Thanks for any advice, Best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor def remove_coming_duplication(a_list): return [element for idx, element in enumerate(a_list) if element != a_list[idx-1]] remove_coming_duplication([2, 5, 7, 5, 5]) [2, 5, 7, 5] remove_coming_duplication([2, 5, 7, 5, 5, 5, 5, 5, 7, 7, 5]) [2, 5, 7, 5, 7, 5] remove_coming_duplication(['2', '5', '7', '5', '5']) ['2', '5', '7', '5'] With this you're simply iterating through the list and checking if the current element in the list is not equal to the previous element, and if so it is not a duplicate and will be added to the new list you're creating. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor