Home > Mobile >  repeat number N times in a string in input file python ::
repeat number N times in a string in input file python ::

Time:11-17

I am trying to repeat a number in an input file.dat N times, I have read I could do N* string, however, as my input is not a string and it's a number in string then I convert it to numpy, so all I need is a way to repeat the input from the beginning

My code as below:

import numpy as np
with open('trial.dat', 'r') as f:
     data = f.readlines()
     data = [(d ' ')[:d.find('#')].rstrip() for d in data]
     x=data[0]
     y=np.fromstring(x,dtype=int, sep=',') # should be  y=[1 2 3 3 3 3 3]

#output error is : DeprecationWarning: string or file could not be read to its end due to unmatched data; this will raise a ValueError in the future. y=np.fromstring(x,dtype=int, sep=',') Also asking if there is a way to add [] in the definition of input file ???

File .dat is the following:

1,2,3*5     # Can I add like [1,2,3*5] or I am also open to any other way to repeat number

CodePudding user response:

data = "1,2,3*5"

elements = data.split(",")
numbers = []
for elt in elements:
    if "*" in elt:
        n, mult = elt.split("*")
        numbers = numbers   [int(n)] * int(mult)
    else:
        numbers.append(int(elt))

print(numbers)
# [1, 2, 3, 3, 3, 3, 3]

CodePudding user response:

As the commenters above mentioned, you don't need numpy to do this.

Reading the data

For just a single line, you could do something like this:

# read line from data file and make line into list delimiting by ','
with open('trial.dat', 'r') as f:
     data = f.readline().rstrip().split(',') 

newlist = []

# step trough items in list data
for d in data:
   if "*" in d: # if there's a "*" in the list 
      [a,b] = d.split('*') # split it on that value (e.g. d = "a*b" --> ["a", "b"]
      for i in range(int(a)):  # append "b" a times
         newlist.append(b)
   else: # if there's no "*" in list just append normally
      newlist.append(d)
print(newlist)
# ['1', '2', '5', '5', '5']

If you have multiple lines in your file, then I would ammend the above with something like this (I have just written your line in trial.dat 3 times for demonstration):

#if you have multiple lines
with open('trial_multi.dat', 'r') as f:
     data = [line.rstrip().split(',') for line in f.readlines()]

newlist = []

# step trough items in list data
for line in data:
   for d in line:
      if "*" in d: # if there's a "*" in the list 
         [a,b] = d.split('*') # split it on that value (e.g. d = "a*b" --> ["a", "b"]
         for i in range(int(a)):  # append "b" a times
            newlist.append(b)
      else: # if there's no "*" in list just append normally
         newlist.append(d)
print(newlist)
# ['1', '2', '5', '5', '5', '1', '2', '5', '5', '5', '1', '2', '5', '5', '5']

Changing the type

If you want arrays, you can simply do something like newlist = np.array(newlist). If you want the values to be float or int type you can change the type with something like newlist = [int(i) for i in newlist]. Or you can combine them to make an array of int value with something like newlist = np.array(newlist, dtype = 'int'). Of course if you want float or some other type instead of int just change that in the above.

CodePudding user response:

with open('trial.dat', 'r') as f:
    data = f.readlines()
    data = data[0].split(',')  # Split input by commas
    output = ''

    for i in range(len(data)):  # Iterating through each group that was split by commas
        str_split = data[i].split('*')  # Splitting the split string by "*"

        if len(str_split) == 2:  # Checking if "*" was present in the split string
            output  = str_split[0] * int(str_split[1])  # If "*" is present, add multiplied string to output
        else:
            output  = str(data[i])

    output = list(map(int, list(output)))  # Convert list to array, the convert array elements to int

    print(output)

This should cut it. Instead of using vectors, I used array operations. The question was a bit confusing about the desired output, so this solution returns an array of integers.

Eg: 1,2,3*5 would return [1, 2, 3, 3, 3, 3, 3]

Hopefully the comments help you understand the logic, which is relatively simple

  • Related