#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    39
    Rep Power
    3

    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 .
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    39
    Rep Power
    3
    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.....
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    39
    Rep Power
    3

    Solved


    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!'
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Texas
    Posts
    24
    Rep 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")

IMN logo majestic logo threadwatch logo seochat tools logo