I call the oxidation states of A and B in AB compounds from list1 ('CaFe', 'BaSi', 'SeOs', 'BaGeO', 'CdCe'):
dfA = pd.read_csv("oxi_state.csv",index_col=0, header =0)
A1 = []
A2 = []
final = []
for i in range(len(list1)):
A1 = dfA['OS'][list1[i][0]]
A2 = dfA['OS'][list1[i][1]]
A = (A1, A2)
final.append(A)
final
When I have called the data from DataFrame. The data is in the form:
[('2', '2,3,4'),
('2', '4'),
('-2,4,6', '4,5,6,7'),
('2', '2,4'),
('2', '3,4')]
Now I want to convert in the following form:
[([2], [2, 3, 4]), ([2], [4]), ([-2,4,6], [4,5,6,7]), ([2], [2,4]), ([2], [3,4])]
For post processing.
Thanks in advance
CodePudding user response:
you may have to loop through all of the items. Using
list('2,3,4')
will return
['2',',','3',',','4']
You can then go through and remove the commas and convert the str to int
CodePudding user response:
Use split method with ,
as a seperator.
A1 = list(int(x) for x in dfA['OS'][list1[i][0]].split(","))
A2 = list(int(x) for x in dfA['OS'][list1[i][1]].split(","))
CodePudding user response:
Here a brute force solution :
a = [('2', '2,3,4'),
('2', '4'),
('-2,4,6', '4,5,6,7'),
('2', '2,4'),
('2', '3,4')]
fin = []
for i in range(len(a)):
tup = []
for j in a[i]:
tmp = []
for v in j.split(','):
tmp.append(int(v))
tup.append(tmp)
fin.append(tuple(tup))
CodePudding user response:
It's unclear how the list of tuples is being created. However, based on the sample shown then:-
lot = [('2', '2,3,4'),
('2', '4'),
('-2,4,6', '4,5,6,7'),
('2', '2,4'),
('2', '3,4')]
for i, e in enumerate(lot):
lot[i] = [list(map(int, e_.split(','))) for e_ in e]
print(lot)
Output:
[[[2], [2, 3, 4]], [[2], [4]], [[-2, 4, 6], [4, 5, 6, 7]], [[2], [2, 4]], [[2], [3, 4]]]