I want to perform an index operation using the list I5
to create a new list I6
. However, there seems to be an error in notation.
This is the logic
I6=[[(I5[0][0]),(I5[0][1]-I5[0][0])],[(I5[1][0]),(I5[1][1]-I5[1][0])],...]
The code is
I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]
for i in range (0,len(I5)):
I6=I5[i][0]-I5[i][1]
print(I6)
The error is
in <module>
I6=I5[i][0]-I5[i][1]
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
The expected output is
I6 = [[(0.5, -0.5), (1, 0)], [(0.5, -0.5), (0, -1)], [(1.5, -0.5), (0, -1)], [(0.5, -1.5), (1, 0)]]
CodePudding user response:
Try:
I5 = [
[(0.5, -0.5), (1.5, -0.5)],
[(0.5, -0.5), (0.5, -1.5)],
[(1.5, -0.5), (1.5, -1.5)],
[(0.5, -1.5), (1.5, -1.5)],
]
I6 = []
for i in range(0, len(I5)):
I6.append(
[I5[i][0], (I5[i][1][0] - I5[i][0][0], I5[i][1][1] - I5[i][0][1])]
)
print(I6)
Prints:
[
[(0.5, -0.5), (1.0, 0.0)],
[(0.5, -0.5), (0.0, -1.0)],
[(1.5, -0.5), (0.0, -1.0)],
[(0.5, -1.5), (1.0, 0.0)],
]
CodePudding user response:
I think this code works.
I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]
I6 = []
for i in range (0,len(I5)):
I6.append([])
I6[i].append(I5[i][0])
toadd = []
toadd.append(I5[i][1][0]-I5[i][0][0])
toadd.append(I5[i][1][1]-I5[i][0][1])
I6[i].append(toadd)
print(I6)
CodePudding user response:
A concise way is:
I6 = [[s[0], tuple(y - x for x, y in zip(s[0], s[1]))] for s in I5]
which gives:
[[(0.5, -0.5), (1.0, 0.0)], [(0.5, -0.5), (0.0, -1.0)], [(1.5, -0.5), (0.0, -1.0)], [(0.5, -1.5), (1.0, 0.0)]]
Even shorter (but maybe not so transparent):
I6 = [[s[0], tuple(y - x for x, y in zip(*s))] for s in I5]
CodePudding user response:
This seems to give the desired output although the zero values are float rather than int as implied in the question.
I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]
I6 = []
for e1, e2 in I5:
I6.append([e1, (e2[0]-e1[0], e2[1]-e1[1])])
print(I6)
Output:
[[(0.5, -0.5), (1.0, 0.0)], [(0.5, -0.5), (0.0, -1.0)], [(1.5, -0.5), (0.0, -1.0)], [(0.5, -1.5), (1.0, 0.0)]]
CodePudding user response:
You can use pandas:
I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]
import pandas as pd
df = pd.DataFrame(I5)
df[2] = df[0].map(lambda x: x[0])
df[3] = df[0].map(lambda x: x[1])
df[4] = df[1].map(lambda x: x[0])
df[5] = df[1].map(lambda x: x[1])
will return:
#df:
0 1 2 3 4 5
0 (0.5, -0.5) (1.5, -0.5) 0.5 -0.5 1.5 -0.5
1 (0.5, -0.5) (0.5, -1.5) 0.5 -0.5 0.5 -1.5
2 (1.5, -0.5) (1.5, -1.5) 1.5 -0.5 1.5 -1.5
3 (0.5, -1.5) (1.5, -1.5) 0.5 -1.5 1.5 -1.5
then you can create the new columns you want based on other columns, I think easier than using nested lists:
df[6] = df[3] - df[2]
0 1 2 3 4 5 6
0 (0.5, -0.5) (1.5, -0.5) 0.5 -0.5 1.5 -0.5 -1.0
1 (0.5, -0.5) (0.5, -1.5) 0.5 -0.5 0.5 -1.5 -1.0
2 (1.5, -0.5) (1.5, -1.5) 1.5 -0.5 1.5 -1.5 -2.0
3 (0.5, -1.5) (1.5, -1.5) 0.5 -1.5 1.5 -1.5 -2.0