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 November 9th, 2012, 08:55 PM
WandyLau WandyLau is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 29 WandyLau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
Can somebody help me?

I wrote some lines to extract the duplicated data from a piece of sorted data about 496 ,whose is formatted as "12 23 34 45 1234". I just want to get the ones who have the same last for numbers.And I have already known that there are 48 couples in all .So I wrote a piece of script but I only got a half ,just 24 couples.I checked that it just skipped some data.I can't understand it. Perhaps some bugs exist in my lines ,but I can not figure it out .Some body helps me please. Thank a lot in advance!! Code as follows>>
Code:
#!/usr/lib/env python

f = open("bit16_21",'r')
lines = f.readlines()
f.close()
fobj = open('dup','w')
i = 0

while i<496:
    line = lines[i]
    j = i+1
    if  j<496 and line[9:] == lines[j][9:]:
        fobj.write(line)
        fobj.write(lines[j])
    i += 1
        
        
fobj.close()
print 'done!'

And I wonder if I do not know that it has 48 couples. Some one piece maybe have 3 or more duplicated ones how to get all the duplicated ones.I just trapped by this .

Reply With Quote
  #2  
Old November 9th, 2012, 10:27 PM
WandyLau WandyLau is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 29 WandyLau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
Quote:
Originally Posted by WandyLau
I wrote some lines to extract the duplicated data from a piece of sorted data about 496 ,whose is formatted as "12 23 34 45 1234". I just want to get the ones who have the same last for numbers.And I have already known that there are 48 couples in all .So I wrote a piece of script but I only got a half ,just 24 couples.I checked that it just skipped some data.I can't understand it. Perhaps some bugs exist in my lines ,but I can not figure it out .Some body helps me please. Thank a lot in advance!! Code as follows>>
Code:
#!/usr/lib/env python

f = open("bit16_21",'r')
lines = f.readlines()
f.close()
fobj = open('dup','w')
i = 0

while i<496:
    line = lines[i]
    j = i+1
    if  j<496 and line[9:] == lines[j][9:]:
        fobj.write(line)
        fobj.write(lines[j])
    i += 1
        
        
fobj.close()
print 'done!'

And I wonder if I do not know that it has 48 couples. Some one piece maybe have 3 or more duplicated ones how to get all the duplicated ones.I just trapped by this .



Yeah,I got the silly bug ,I counted the wrong number which should be like :line[11:] == lines[j][11:] . OMG,It nearly drived me out of my mind ,this little bug . Small mind-absence results in big mistake.
Now I wonder if there are more than two duplicated ones.How to handle it.....

Reply With Quote
  #3  
Old November 9th, 2012, 11:36 PM
WandyLau WandyLau is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 29 WandyLau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 44 sec
Reputation Power: 0
Solved

Quote:
Originally Posted by WandyLau
Yeah,I got the silly bug ,I counted the wrong number which should be like :line[11:] == lines[j][11:] . OMG,It nearly drived me out of my mind ,this little bug . Small mind-absence results in big mistake.
Now I wonder if there are more than two duplicated ones.How to handle it.....


Wow,I finally made it. Just some tricks. Ok I'will post the script,not so hard. But I am a green hand in programming so not easy for me!! Happy..
Code:
#!/usr/lib/env python

f = open("bit16_21",'r')
lines = f.readlines()
f.close()
fobj = open('dup','w')
i=0
while i in range(496):
    line = lines[i]
    j = i+1
    n = j+1
    if j<496 and line[11:] == lines[j][11:]:
        fobj.write(line)
        fobj.write(lines[j])
        while  n<496:
            if lines[j][11:] == lines[n][11:]:
                fobj.write(lines[n])
                n += 1
                
                
            else:
                i = n
                break
    else:
        i = j

fobj.close()
print 'done!'

Reply With Quote
  #4  
Old November 10th, 2012, 07:19 AM
KING-OLE KING-OLE is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Texas
Posts: 24 KING-OLE User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 54 m 45 sec
Reputation Power: 0
Good job.

You may consider using .split to create a list containing each of the values in the line you read from the file.

Here's an example with 2 strings that I convert to 2 lists, and compare the last element:

Code:
s1="41 42 14 15 2012"
s2="44 63 44 78 2012"

l1=s1.split()
l2=s2.split()

if l1[4]==l2[4]:
  print("They match")


This way, your program would work if the first 4 values did not have the same digits.

Example: This would work with the split solution, but not with your solution:

Code:
s1="41 2 14 15 2012"
s2="44 63 144 78 2012"

l1=s1.split()
l2=s2.split()

if l1[4]==l2[4]:
  print("They match")

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Can somebody help me?

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