The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Can somebody help me?
Discuss Can somebody help me? in the Python Programming forum on Dev Shed. Can somebody help me? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 9th, 2012, 08:55 PM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 29
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 .
|

November 9th, 2012, 10:27 PM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 29
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.....
|

November 9th, 2012, 11:36 PM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 29
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!'
|

November 10th, 2012, 07:19 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Location: Texas
Posts: 24
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")
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|