I have a list in a list, and I am trying to delete the third number of each sublist, but every time I am getting an error TypeError: list indices must be integers or slices, not list
a = [[0.0, 0.0, 0.0], [0.19, 0.36, 0.0], [0.24, 0.42, 0.0], [0.16, 0.08, 0.0], [0.05, -0.57, 0.0] ]
Desired result:-
a_updated = [[0.0, 0.0], [0.19, 0.36], [0.24, 0.42], [0.16, 0.08], [0.05, -0.57] ]
In the second part of my code, I wanted to merge this sublist according to a dictionary shown below, for example, the first value of dictionary:- 1: [1, 2]
shows the merging of 1st and 2nd values i.e. [0, 0, 0.19, 0.36]
.
I guess this part of my code is right!
dict_a = { {1: [1, 2], 2: [2, 4], 3: [3, 5], 4: [4, 5] }
my attempt:-
dict_a = { 1: [1, 2], 2: [2, 4], 3: [3, 5], 4: [4, 5]}
a = [[0.0, 0.0], [0.19, 0.36], [0.24, 0.42], [0.16, 0.08], [0.05, -0.57]]
# first part
for i in a:
for j in a[i]:
del j[2]
print(j)
#second part
a_list = []
list_of_index = []
for i in dict_a:
index= []
a_list.append(index)
for j in dict_a_updated[i]:
print(j-1)
index.extend(a_updated[j-1])
print('index',index)
Error output -
file "D:\python programming\random python files\4 may axial dis.py", line 18, in <module>
for j in X[i]:
TypeError: list indices must be integers or slices, not list
CodePudding user response:
You can slice the sublists in a list comprehension to build a_updated
:
a_updated = [s_lst[:2] for s_lst in a]
Output:
[[0.0, 0.0], [0.19, 0.36], [0.24, 0.42], [0.16, 0.08], [0.05, -0.57]]
To build dict_a_updated
, you can use a loop. Note that list indices start from 0 in Python but your index starts from 1, so we have to subtract 1 here:
dict_a_updated = {}
for k, v in dict_a.items():
tmp = []
for i in v:
tmp.extend(a_updated[i-1])
dict_a_updated[k] = tmp
Output:
{1: [0.0, 0.0, 0.19, 0.36],
2: [0.19, 0.36, 0.16, 0.08],
3: [0.24, 0.42, 0.05, -0.57],
4: [0.16, 0.08, 0.05, -0.57]}
CodePudding user response:
Given a list of lists:
a = [[0.0, 0.0, 0.0], [0.19, 0.36, 0.0], [0.24, 0.42, 0.0], [0.16, 0.08, 0.0], [0.05, -0.57, 0.0]]
The first solution: if the third element is the last element in all arrays.
firstSolution = [el[:-1] for el in a]
print(firstSolution)
The second solution: is to remove the element by its index.
for el in a:
el.pop(2)
print(a)
CodePudding user response:
You can achieve you goal with a single dictionary comprehension and itertools.chain
, without needing to first rework a
:
from itertools import chain
out = {k: list(chain.from_iterable(a[i-1][:2] for i in v))
for k,v in dict_a.items()}
Output:
{1: [0.0, 0.0, 0.19, 0.36],
2: [0.19, 0.36, 0.16, 0.08],
3: [0.24, 0.42, 0.05, -0.57],
4: [0.16, 0.08, 0.05, -0.57]}
CodePudding user response:
a = [[0.0, 0.0, 0.0], [0.19, 0.36, 0.0], [0.24, 0.42, 0.0], [0.16, 0.08, 0.0], [0.05, -0.57, 0.0] ]
dict_a = {1: [1, 2], 2: [2, 4], 3: [3, 5], 4: [4, 5] }
# first part
for i in range(0,len(a)): # for every sublist position
a[i] = a[i][0:2]
dict_lists = {}
for key,value in dict_a.items():
dict_lists[key] = [a[value[0]-1], a[value[1]-1]]
Output:
In[19]: dict_lists
Out[19]:
{1: [[0.0, 0.0], [0.19, 0.36]],
2: [[0.19, 0.36], [0.16, 0.08]],
3: [[0.24, 0.42], [0.05, -0.57]],
4: [[0.16, 0.08], [0.05, -0.57]]}