Skip to main content
edited body; edited title
Source Link
Reinderien
  • 65.3k
  • 5
  • 69
  • 187

Converting string of coma separatedcomma-separated items into 2d table

ImI'm writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comascommas are possible, the input can also start/end with comacomma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            yield data[i:]
            break


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

Converting string of coma separated items into 2d table

Im writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            yield data[i:]
            break


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

Converting string of comma-separated items into 2d table

I'm writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple commas are possible, the input can also start/end with comma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            yield data[i:]
            break


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

bug in code
Source Link
Kazz
  • 186
  • 5

Im writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            returnyield data[i:]
            break


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

Im writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            return data[i:]


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

Im writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            yield data[i:]
            break


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

added 1288 characters in body; edited title
Source Link
Kazz
  • 186
  • 5

List to Converting string of coma separated items into 2d array without numpytable

Is thereIm writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a way to expres this better?string containing items separated by range(len(...)), gives me bad memories . Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:P

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def tablemake_table(data: liststr, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy cancould do it, but this seems simple enoughthat's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for mesome lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            return data[i:]


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to be doable with intertools or somethingmake zip take arbitrary number of references to g.

How can I improve any of those?

List to 2d array without numpy

Is there a way to expres this better? range(len(...)) gives me bad memories :P

def table(data: list, w: int) -> list:
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

I know numpy can do it, but this seems simple enough for me to be doable with intertools or something.

Converting string of coma separated items into 2d table

Im writing the following:

def make_table(data: str, w: int) -> List[List]:
    pass
    

data is a string containing items separated by ,. Some items may be an empty sequence so multiple comas are possible, the input can also start/end with coma which mean that first/last item is empty. w stands for width and is the number of columns of the output table. We're assuming the input is valid.

Examples:

In [1]: make_table("a,b,c,d,e,f,g,h,i",3)
Out[1]: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

In [2]: make_table(",,1,,1,,1,,",3)
Out[2]: [['', '', '1'], ['', '1', ''], ['1', '', '']]

My first working solution is this:

def make_table(data: str, w: int) -> list:
    data = data.split(",")
    return [data[i*w:(i+1)*w] for i in range(len(data)//w)]

What I don't like here is that

  1. range(len(...)) gives me bad memories
  2. I feel like this could be done prettier.

I know numpy could do it, but that's an overkill. I'm looking through std libs but don't see anything related.

My second solution is more efficient but a little roundabout, I was looking for some lazy split solution but found only some rejected proposals. I did this:

def coma_split(data: str):
    i, j = 0, -1
    while True:
        i, j = j + 1, data.find(",", j + 1)
        if j != -1:
            yield data[i:j]
        else:
            return data[i:]


def make_3_column_table(data: str) -> list:
    g = coma_split(data)
    return list(zip(g,g,g))
    

But here I don't know how to make zip take arbitrary number of references to g.

How can I improve any of those?

deleted 879 characters in body; edited tags
Source Link
Peilonrayz
  • 43.5k
  • 7
  • 76
  • 155
Loading
Source Link
Kazz
  • 186
  • 5
Loading