0

I need to add numbers to each value according to a certain sequence.

My code:

f = open("demofile.txt", "r")
lines = f.readlines()
for i in list(lines):
    w = i[3:]
    w = ', '.join(w.split())
    #print(w)
    #time.sleep(1)
    y = i[2]
    y=int(y)+1
    #print(y)
    c1=np.array([w])
    c1 = [int(i) for i in c1[0].replace(" ", "").split(",")]
    c1=np.array([c1]*3)
    c1=np.transpose(c1)
    a=str(c1).replace("[",'')
    a=str(a).replace("]",'')
    a=str(a).replace("\n ",'\n')
    #print('\n')
    print(a)

Input:

<=1 1 2 3
<=1 4 5 6

My Output:

1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6

I need:

011 021 031
012 022 032
013 023 033
044 054 054
045 055 065
046 056 066
... etc.

I tried to write it by hand but it is not effective.

4
  • 3
    Can you summarize the logic you want to use to make the transformation? Commented Jan 6, 2021 at 20:49
  • Can you elaborate on what is the first integer of these input sequence? Commented Jan 6, 2021 at 20:49
  • first integers are 1 1 1.. It is my output. Logic transformation is add before each number 01 02 03 and apply to the first 3 lines to individual numbers, next 04 05 06 ... etc Commented Jan 6, 2021 at 21:11
  • In your posted question, under the heading "Input", you've mentioned <= 1 1 2 3 <= 1 4 5 6. In the comments above, you've mentioned that the first integer of input sequence is 1 1 1. That seems to be a contradiction. Please clarify what exactly is your input. Also please clarify whether the input you are describing is ALWAYS the input or just a SAMPLE input. Please provide all clarifications by editing your posted question, and briefly refer to the edits, in your comments. Commented Jan 7, 2021 at 1:23

1 Answer 1

0

You can create two arrays from your input and modify each array one you multiply by 10 and the other you transpose. Then when you add them together you get the required array.

You need to change a few lines in your code, before and after you transpose array:

f = open("demofile.txt", "r")
lines = f.readlines()
for i in list(lines):
    w = i[3:]
    w = ', '.join(w.split())
    #print(w)
    #time.sleep(1)
    y = i[2]
    y=int(y)+1
    #print(y)
    c1=np.array([w])
    c1 = [int(i) for i in c1[0].replace(" ", "").split(",")]
    c1=np.array([c1]*3)

    #change code from here
    #to get a 2nd array just multiply c1 each element by 10
    c2=c1*10
    #now you can do the 1st array transpose c1
    c1=np.transpose(c1)
    #add the two resulting arrays
    c3=c2+c1
    #convert to strings
    cmb3 = c3.astype(str)
    #Adding leading zeros to strings in NumPy array, ensuring 3 characters for each element
    cmb3 = np.char.zfill(cmb3, 3)
    # now continue with printing your output

    a=str(cmb3).replace("[",'')
    a=str(a).replace("]",'')
    a=str(a).replace("\n ",'\n')
    #print('\n')
    print(a)

That gives you the output you are looking for:

011 021 031
012 022 032
013 023 033
044 054 064
045 055 065
046 056 066

Look at this link about adding leading zeros to strings in NumPy array:

Adding leading zeros to strings in NumPy array

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.