skip to Main Content

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

4

Answers


  1. 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

    Login or Signup to reply.
  2. 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(","))
    
    Login or Signup to reply.
  3. 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))
    
    Login or Signup to reply.
  4. 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]]]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search