Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2012, 09:53 AM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
Accessing the next element

Hello, i have a text file, say called 'Text.list' setup similar to the following:

element[1]
element[2]
element[3]
...
element[n]

i have the following code:

Code:
Text = open('Text.list','r').readlines()
for Line in Text :
# now i want to see if the current value of Line is equal to the
# next element in the file, ie. is Line == element[x+1]


Is there a slick way in Python to do this, without having to use a counter variable, etc?

Thank you.

Reply With Quote
  #2  
Old July 26th, 2012, 01:36 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
zip is your friend. zip it!

Code:
#What do you mean by element?  How does an "element" differ from Line?
#this code compares pairs of lines without counters, at some memory
#cost.

# the names curtail and behead come from the j definitions
# http://www.jsoftware.com
# curtail=: }:
# behead=: }.
def curtail(LIST):
    '''
        >>> curtail([1,2,3])
        [1, 2]
    '''
    return LIST[:-1]

def behead(LIST):
    '''
        >>> behead([1,2,3])
        [2, 3]
    '''
    return LIST[1:]

# preferred file use.  Use the "with" statement.
with open('Text.list','r') as input_stream:
    Text = input_stream.readlines()

# compare pairs of lines
for (LINE,NEXTLINE,) in zip(curtail(Text),behead(Text)):
    if LINE == NEXTLINE:
        print('duplicate adjacent lines of\n"{}"'.format(LINE))
Comments on this post
WynnDeezl agrees!
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old July 26th, 2012, 02:07 PM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
clarification

thanks!

what i meant by element[x] is just some line of text.

Line is just a variable containing element[x].

wow. i'll have to study the j stuff.

Reply With Quote
  #4  
Old July 27th, 2012, 06:39 AM
Quackajack Quackajack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 35 Quackajack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 55 m 6 sec
Reputation Power: 1
The easiest way to do this without counter variables or loading the entire file into memory is to turn the check on its head. Rather than try and compare to the next line which you don't know, compare it to the previous line which you have already read.

Code:
lastline = None
with open('Text.list','r') as Text:
    for Line in Text:
        if Line == lastline:
            # duplicate line found
            print "Duplicate line: ",line
        lastline = Line


Depending upon what you are trying to do this may not be practical but as it is only keeping two lines and a file pointer in memory it is very efficient.
Comments on this post
WynnDeezl agrees: Perfect. Thanks!!!

Reply With Quote
  #5  
Old July 28th, 2012, 06:00 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
And of course the gnu-linux command

uniq --repeated file

does the work

Reply With Quote
  #6  
Old July 31st, 2012, 01:09 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 480 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 57 sec
Reputation Power: 63
A bit more general, maybe you can adapt for adjacent lines:
Code:
python
'''list_get_duplicates1.py

create a list of duplicates in a given list
'''

def get_duplicate_items(mylist):
    """
    return a list of duplicate items in mylist
    """
    return [item for item in set(mylist) if mylist.count(item) > 1]


# testing ...
mylist1 = [ 'oranges' , 'apples' , 'oranges' , 'grapes' ]
mylist2 = list('abcdefgbd')

sf = 'Duplicates = %s'

print(mylist1)
print(sf % get_duplicate_items(mylist1))

print('-'*50)

print(mylist2)
print(sf % get_duplicate_items(mylist2))

'''result -->
['oranges', 'apples', 'oranges', 'grapes']
Duplicates = ['oranges']
--------------------------------------------------
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'b', 'd']
Duplicates = ['b', 'd']
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
  #7  
Old August 1st, 2012, 02:02 AM
SachinS SachinS is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 12 SachinS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 37 m 41 sec
Reputation Power: 0
I am not sure if I understand the original question correctly. But if you are looking for unique entries in the file then you can use the "set" function and generators. Example below:

if __name__ == '__main__':
with open ("unsorted.txt", "r") as f:
x= list(set(line for line in f))
print x

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Accessing the next element

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap