Home > OS >  I want to transform a list or concatenate this same list
I want to transform a list or concatenate this same list

Time:12-28

I have a list like here that I coded in python

li = []                                                                  
for x in T:
   li.append(x.split())                                            
print(li)
[['pts', 'J.', 'G.', 'N.', 'P.', 'p.', 'c.', ' /-', 'G.', 'N.', 'P.'],
 ['1', 'Paris-SG', '45', '18', '14', '3', '1', '38', '16', ' 22'],
 ['2', 'Marseille'],
 ['32', '17', '9', '5', '3', '25', '14', ' 11'],
 ['3', 'Rennes'],
 ['31', '18', '9', '4', '5', '33', '16', ' 17'],
 ['4', 'Nice', '30', '18', '9', '4', '5', '27', '16', ' 11'],
 ['5', 'Montpellier'],
 ['28', '18', '8', '4', '6', '29', '23', ' 6'],
 ['6', 'Lens'],
 ['27', '18', '7', '6', '5', '32', '26', ' 6'],
 ['7', 'Strasbourg'],
 ['26', '18', '7', '5', '6', '34', '24', ' 10'],
 ['8', 'Monaco'],
 ['26', '18', '7', '5', '6', '27', '22', ' 5']]

and I would like to transform it to be able to integrate it into a dataframe more easily

[['pts', 'J.', 'G.', 'N.', 'P.', 'p.', 'c.', ' /-', 'G.', 'N.', 'P.'],
 ['1', 'Paris-SG', '45', '18', '14', '3', '1', '38', '16', ' 22'],
 ['2', 'Marseille','32', '17', '9', '5', '3', '25', '14', ' 11'],
 ['3', 'Rennes', '31', '18', '9', '4', '5', '33', '16', ' 17'],
 ['4', 'Nice', '30', '18', '9', '4', '5', '27', '16', ' 11'],
 ['5', 'Montpellier', '28', '18', '8', '4', '6', '29', '23', ' 6'],
 ['6', 'Lens','27', '18', '7', '6', '5', '32', '26', ' 6'],
 ['7', 'Strasbourg', '26', '18', '7', '5', '6', '34', '24', ' 10'],
 ['8', 'Monaco', '26', '18', '7', '5', '6', '27', '22', ' 5']]

CodePudding user response:

Assuming the bad lines always have 8 elements:

li = []                                                                  
for x in T:
    p = x.split()
    if len(p) == 8:
        li[-1].extend(p)
    else:
        li.append(p)
print(li)

Alternatively, I suppose you could use:

    if p[1].isdigit():

CodePudding user response:

So we have your original data as data.

Obviously we don't touch the first line.

first_line = data[0]

The remaining data can be accessed as data[1:]. We need to split out the correct and incorrect lines. We can write a function part that will do this.

def part(pred, lst):
    t, f = [], []
    for x in lst:
        (t if pred(x) else f).append(x)
    return (t, f)

Now we can create variable names for these.

correct, incorrect = part(lambda x: len(x) == 10, data[1:])

Now we define another very simple function that will put the incorrect data lines into pairs.

def pairs(lst):
    for i in range(0, len(lst) - 1, 2):
        yield (lst[i], lst[i   1])

The rest can essentially be a single line.

[first_line]   correct   [x   y for x, y in pairs(incorrect)]

But it looks like you want the cities sorted.

corrected_lines = correct   [x   y for x, y in pairs(incorrect)]

[first_line]   list(sorted(corrected_lines, key=lambda a: a[0]))

CodePudding user response:

This solution is a bit fragile -- it assumes that the lists that are cut off have exactly two elements, and that the lists that aren't cut off do not have two elements. If this assumption holds, then the following will do.

result = []
index = 0
while index < len(data):
 # Check if the list should be merged with the list that follows.
 # In the given data, these are lists with length 2.
 if len(data[index]) == 2:
     result.append(data[index]   data[index   1])
     index  = 2
 else:
     result.append(data[index])
     index  = 1

print(result)
  • Related