Массивы? Список? Сохранение значений (x,y) для каждой строки

Есть ли способ на Python создать какой-то массив для присвоения значений x и y каждой строке в файле каждый раз, когда он разбивается?

myArray=[][]?

говорят линии

0.0 0.0 NaN  ( I want this line to have value[1,1]

1.0 0.0 1.0  (and so increasing y by one leaving x alone [1,2]

2.0 0.0 NaN  ---[1,3]

3.0 0.0 NaN---[1,4]

0.0 1.0 2.0---(but when the middle term changes value (increase x by 1 and reset y to 1 leaving [2,1])

1.0 1.0 3.0---[2,2]

2.0 1.0 4.0---[2,3]

3.0 1.0 NaN--- and so on

0.0 2.0 5.0

1.0 2.0 6.0

2.0 2.0 7.0

3.0 2.0 NaN

0.0 3.0 NaN

1.0 3.0 NaN

2.0 3.0 8.0

3.0 3.0 9.0

0.0 4.0 NaN

1.0 4.0 NaN

2.0 4.0 NaN

3.0 4.0 10.0  ---[4,4]

Имеет ли это смысл того, что я спрашиваю? Есть ли способ сделать это?

1 ответ

text = """0.0 0.0 NaN

1.0 0.0 1.0

2.0 0.0 NaN

3.0 0.0 NaN

0.0 1.0 2.0

1.0 1.0 3.0

2.0 1.0 4.0

3.0 1.0 NaN

0.0 2.0 5.0

1.0 2.0 6.0

2.0 2.0 7.0

3.0 2.0 NaN

0.0 3.0 NaN

1.0 3.0 NaN

2.0 3.0 8.0

3.0 3.0 9.0

0.0 4.0 NaN"""

# read data

results = []
for x in text.splitlines():
    x = x.strip()
    if x:
        results.append( x.split() ) 

# count [1,1], [1,2], etc.

new_results = []

value = [0,0]
middle = None

for row in results:
    if row[1] != middle:
        middle = row[1]
        value[0] += 1
        value[1] = 1
    row.append(value[:])
    new_results.append(row)
    value[1] += 1

for x in new_results:
    print x

результат:

['0.0', '0.0', 'NaN', [1, 1]]
['1.0', '0.0', '1.0', [1, 2]]
['2.0', '0.0', 'NaN', [1, 3]]
['3.0', '0.0', 'NaN', [1, 4]]
['0.0', '1.0', '2.0', [2, 1]]
['1.0', '1.0', '3.0', [2, 2]]
['2.0', '1.0', '4.0', [2, 3]]
['3.0', '1.0', 'NaN', [2, 4]]
['0.0', '2.0', '5.0', [3, 1]]
['1.0', '2.0', '6.0', [3, 2]]
['2.0', '2.0', '7.0', [3, 3]]
['3.0', '2.0', 'NaN', [3, 4]]
['0.0', '3.0', 'NaN', [4, 1]]
['1.0', '3.0', 'NaN', [4, 2]]
['2.0', '3.0', '8.0', [4, 3]]
['3.0', '3.0', '9.0', [4, 4]]
['0.0', '4.0', 'NaN', [5, 1]]
Другие вопросы по тегам