Python, reset



  • Python, one-dimensional mass is to be transferred to a certain symbol, such as the "n" symbol:

    It was:

    ['255', '123', '15', 'n', '233', '109', '45', 'n', '10', '89', '3']
    

    It was:

    [['225', '123', '15'], ['233', '109', '45'], ['10', '89', '3']]
    

    However, it should be possible to transfer any number of dividers (n)



  • Incorrect forehead with non-correct work due to non-conforming types (not lines):

    data = ['255', '123', '15', 'n', '233', '109', '45', 'n', '10', '89', '3']
    

    res = [[i for i in obj.split(',') if i != ''] for obj in ','.join(data).split('n')]

    print(res)

    Adjustment for:

    data = ['255', '123', '15', 'n', '233', '109', '45', 'n', '10', '89', '3']

    res = []
    tmp = []

    for i in range(len(data)):
    if data[i] == 'n':
    if tmp != []:
    res.append(tmp)
    tmp = []
    else:
    tmp.append(data[i])

    if tmp != []:
    res.append(tmp)

    print(res)

    A little shorter than that:

    res = []
    tmp = []

    for obj in data:
    if obj == 'n':
    res += [tmp] if tmp else []
    tmp = []
    else:
    tmp.append(obj)

    res += [tmp] if tmp else []

    And so far, the shortest and a little twisted version that came into the head:

    res, tmp = [], []

    for obj in data:
    res, tmp = (res + [tmp] if tmp else [], []) if obj == 'n' else (res, tmp + [obj])

    res += [tmp] if tmp else []



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2