Pkzzz
Pkzzz

Reputation: 111

How to split string and get the preferred value?

Look like this, give the position as string type:

str1 = "0,0|2,5|...|"

This is the user's input, every position is split by "|", so we don't know the number of positions. I need to store every position to the defined variables pos1, pos2, ...., pos4....

For example:

pos1 = 0,0 pos2 = 2,5,..., pos4=...

My question is how could I store all the user input to the defined variables?

How can I do these in a less time complexity?

enter image description here

Upvotes: 0

Views: 321

Answers (3)

Roeniss
Roeniss

Reputation: 404

First, in your screenshot, you didn't declare ps2 = Pos(). So the original code would not work at this time. And one more thing : declare class name in PascalCase. It's a python convention.

you actually asked 3 questions. Let's get in.

FYI : I assumed that every pos values(x and y) would be integer.

  1. How to handle undetermined-sized inputs (in this case, str1) ?

you can use string.split() built-in python method. It goes like this:

my_str = "Hi, I'm now making answer to your questions"
DELIMITER = " "
my_str.split(DELIMITER) # ['Hi,', "I'm", 'now', 'making', 'answer', 'to', 'your', 'questions']

In your case, every pos pair would be divided by vertical line character (|).

my_str = "2,0|3,4|7,2|8,6|9,1"
delimiter = "|"
for (x,y) in my_str.split(delimiter):
    print(f'x is {x}, y is {y}')

# the output is:
# 2,0
# 3,4
# 7,2
# 8,6
# 9,1

You don't have to worry about the size of pair because split() method will split a target chunk into every pair unless you didn't specify maxsplit numbers. It's optional parameter of split() (python3.8 official docs : https://docs.python.org/3/library/stdtypes.html#str.split)

After splitting, you can split again on each pair (ex. "2,0").

  1. How to store those values (in this case, each POS instances) ?

One of the most simplest way is using python built-in list and its method, append().

storage = []
for num in range(0, 10):
    storage.append(num)
    print(storage)

# the output is :
# [0]
# [0, 1]
# [0, 1, 2]
# [0, 1, 2, 3]
# [0, 1, 2, 3, 4]
# [0, 1, 2, 3, 4, 5]
# [0, 1, 2, 3, 4, 5, 6]
# [0, 1, 2, 3, 4, 5, 6, 7]
# [0, 1, 2, 3, 4, 5, 6, 7, 8]
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

Because python's list is dynamic array, the storage gets bigger on each append().

In short, you can use python 'list' data structure to store unknown-sized values. (Of course you can store POS instances although I showed only integer case!)

  1. How to cut off time complexity ?

I'm not sure on this. I think there would be no critical difference if the number of pairs is less than 10k.

But if size is bigger than that I may use this techniques (and probably gonna use c++ rather than python) :

# notice that this is pseudo code
storage = []

p = pos() 
x, y = 0, 0 # initialize
status = 0 # "what am I working for now?" (0 is for x, 1 is for y)
for character in str1:
    if character is number:
        if status == 0:
            x = 10*x + number(character)
        else: # status == 1
            y = 10*y + number(character)
    elif character is ",":
        p.x = x
        x = 0 # re-initialize
        status = 1 # "x is done"
    else: # status is "|"
        p.y = y
        y = 0 # re-initialize
        status = 0 # "y" is done"
        storage.append(p) # add new instance
        p = pos() # start another instance

above code will check each character only one time during whole execution. (I think it's very cpp-style. Isn't it?) So basically Time Complexity would be O(n) where n is the length of string. But be careful! I didn't do any tests on this.

Upvotes: 0

Gabio
Gabio

Reputation: 9504

You can create a dictionary which each key will be pos (pos1, pos2, etc) and the value will be the corresponding Pos object.

Try this:

class Pos:
    # make sure you add x,y to your class constructor in order to be able to init it dynamically
    def __init__(self, x, y):
        self.x = x
        self.y = y

pos_mapping = {}
pos_list = str1.split("|") # pos_list = ["0,0", "2,5" ...]
for i,p in enumerate(pos_list):
    x,y = p.split(",") # split the position string by comma to extract x,y 
    pos_mapping[f"pos{i+1}"] = Pos(x,y)

So if you would like to get pos2, just do:

pos_mapping["pos2"]

Upvotes: 1

DYZ
DYZ

Reputation: 57105

First, there is no reason to have the class constructor without parameters. Let it take x and y at once:

class Pos:
    def __init__(self, x, y):
        self.x = x
        self.y = y

Now, you can split the string first by pipes and then by commas. Apply int to each resulting tuple to convert its elements to numbers. Finally, construct a Pos from each tuple:

positions = [Pos(*map(int, point.split(","))) 
          for point in str1.split("|")]

Upvotes: 0

Related Questions