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

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Need to code this using python


    I have one file which have repeated numbers in it (say one number coming three times).
    I want to change the number when it appears second time and third time.

    For example, I have a file like this.

    A,1,3
    A,2,3
    A,2,4
    A,2,11
    A,3,7
    A,4,5
    A,4,13
    A,4,7
    A,5,11
    A,5,13
    A,7,8

    In the above file, lets say 3 is coming three time, I want to replace the number 3 when it appears second time as 1003 and, when it appears third time then 10003.
    So, like this I want to do this for all the numbers and the file size is very big.
    I am really struggling with this to code it in python.
    If anyone can help me in this then I will be really thankful to him and appreciate his contribution.
    Thank you.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    dictionary tracks status of each number


    Code:
    import io                               # use StringIO in place of an actual file for demonstration
    input_file = io.StringIO(
    '''
        A,1,3
        A,2,3
        A,2,4
        A,2,11
        A,3,7
        A,4,5
        A,4,13
        A,4,7
        A,5,11
        A,5,13
        A,7,8
    '''.strip())
    
    output_file = io.StringIO()
    
    d = {}  # dictionary to track status of each number
    
    output_fields = [None,]*3  # allocate some memory
    
    for line in input_file:
        fields = line.strip().split(',')  # split the line on commas.
        output_fields[0] = fields[0]
        for (i, v,) in enumerate(map(int,fields[1:3])):    # convert str to int
            if v in d: # change the value already seen
                value = d[v] + v
                d[v] *= 10
            else:  # use the current value---be ready to change it.
                value = v
                d[v] = 1000
            output_fields[i+1] = '{}'.format(value) # convert the altered int back to str
        output_file.write(','.join(output_fields)+'\n')
    
    print('demonstration of result')
    output_file.seek(0)
    print(output_file.read())
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    14
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    import io                               # use StringIO in place of an actual file for demonstration
    input_file = io.StringIO(
    '''
        A,1,3
        A,2,3
        A,2,4
        A,2,11
        A,3,7
        A,4,5
        A,4,13
        A,4,7
        A,5,11
        A,5,13
        A,7,8
    '''.strip())
    
    output_file = io.StringIO()
    
    d = {}  # dictionary to track status of each number
    
    output_fields = [None,]*3  # allocate some memory
    
    for line in input_file:
        fields = line.strip().split(',')  # split the line on commas.
        output_fields[0] = fields[0]
        for (i, v,) in enumerate(map(int,fields[1:3])):    # convert str to int
            if v in d: # change the value already seen
                value = d[v] + v
                d[v] *= 10
            else:  # use the current value---be ready to change it.
                value = v
                d[v] = 1000
            output_fields[i+1] = '{}'.format(value) # convert the altered int back to str
        output_file.write(','.join(output_fields)+'\n')
    
    print('demonstration of result')
    output_file.seek(0)
    print(output_file.read())
    (
    This thread drew my attention!Out of curiosity i copy pasted your example code and tried to execute it and i got an error:
    File "<pyshell#227>", line 14, in <module>
    '''.strip())
    TypeError: initial_value must be unicode or None, not str

    I tried to isolate the source of the problem,my python seems to complain with these codelines:
    import io
    input_file = io.StringIO(
    '''
    ....... (i put dots to keep my reply short)
    .......
    '''.strip())

    do not freak out,i am new to python
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    I'm bad with unicode. My program of post two this thread works in python 3, and fails as you've shown in python2.

    Sorry!
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Thumbs up Thanks b49P23TIvg


    Thank you very much b49P23TIvg for such a nice help.
    Its good guideline for me. I have slightly modified your program to suit my requirement and it works fine for me.
    I really appreciate that you have spend some time to post a reply to this thread.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Dear b49P23TIvg,

    Thanks for the very quick help. I really appreciate your efforts to have a look at this post.
    I need your one more favor. Can you please help me out to solve the another query.
    I am a Computational Engineer and really don't have much idea about python, So requesting you to please do me a favor.

    In have a file in that, I want to arrange the numbers in such a way that, if any number comes more than 2 times in a file then their respective numbers should form the pairs with each other.

    The File is as follows,
    ************************************************
    A,1,3
    A,2,3
    A,2,4
    A,2,11
    A,3,7
    A,4,5
    A,4,7
    A,7,8
    A,8,15
    .
    .
    .
    .
    ***************************************************************************
    For example, in above file, number 3 is coming three times. So, I need to add three more line in the same file as,
    A,1,2
    A,2,7
    A,1,7

    Number 2 is appearing three times. So, I want to add
    A,3,4
    A,4,11
    A,3,11

    Now, Number 8 is coming only twice. So we need to add only single line as
    A,7,15

    Now, if I consider Number 7 then it is also coming three times. So, I need to add
    A3,4
    A,4,8
    A,3,8

    Now if we see the file then after adding some extra lines we got
    A,3,4 coming twice, which is not desirable. I need to have the added line only once. If such line is coming then it should be ignored.

    For more understanding, I am writing the output file manually. My need is.. the Final file should look like this

    A,1,3
    A,2,3
    A,2,4
    A,2,11
    A,3,7
    A,4,5
    A,4,7
    A,7,8
    A,8,15

    A,3,4
    A,4,11
    A,3,11

    A,1,2
    A,2,7
    A,1,7

    A,2,5
    A,5,7
    A,2,7

    A,4,8
    A,3,8

    A,7,15


    *** Please try to help me out in this logical operation.

IMN logo majestic logo threadwatch logo seochat tools logo