简体   繁体   中英

parse using nested maps and itemgetter in python

I want to map a list of temp1,..tempn with a nested list

  list1 = ['temp1', 'temp2', 'temp3', 'temp4']
  list2 = [['30C', '86F', '303K', '545R' ], ['-5.55C', '22F', '267K', '481R' ], ['0', '32F', '273K', '491R'], ['-17C', '0', '256K', '461R']]

I want to map like this

  temp1   30C  -5.55C   0   -17C
  temp2   86F   22F    32F    0
  temp3   303K  267K   273K  256K
  temp4   545R  481R   491R  461R

That is to map 1st term of list1 to nth_1 term of list2 and like that.

like

  new_list = [['temp1',['30C','-5.55C', '0', '-17C']], ['temp2', ['86F', '22F', '32F', '0']], ['temp3', ['303K', '267K', '273K', '256K'], ['temp4',['545R', '481R', '491R', '461R']]]

I then want to consider each temp and check if that temp_n has a zero in it and then remove the columns which have zero in it

for temp1

  temp1   30C  -5.55C   **0**   -17C
  temp2   86F   22F    **32F**    0
  temp3   303K  267K   **273K**  256K
  temp4   545R  481R   **491R**  461R

to get

  temp1   30C  -5.55C  -17C
  temp2   86F   22F      0
  temp3   303K  267K   256K
  temp4   545R  481R   461R

Now my output1 for temp1 should be

 output1 = temp1&30C + temp2&86F + temp3&303K + temp4&545R, temp1&-5.55C + temp2&22F + temp3&267K + temp4&481R, temp1&-17C + temp3&256K + temp4&461R

(Dont consider temp2&0)

for temp2

  temp1   30C  -5.55C   0   **-17C**
  temp2   86F   22F    32F    **0**
  temp3   303K  267K   273K  **256K**
  temp4   545R  481R   491R  **461R**

so it becomes

   temp1   30C  -5.55C   0   
   temp2   86F   22F    32F   
   temp3   303K  267K   273K  
   temp4   545R  481R   491R  

Now my output2 for temp2 should be

 output2 = temp1&30C + temp2&86F + temp3&303K + temp4&545R, temp1&-5.55C + temp2&22F + temp3&267K + temp4&481R, temp2&32F + temp3&273K + temp4&491R

temp3 and temp4 dont have zeros, so their output is

   output3 = temp1&30C + temp2&86F + temp3&303K + temp4&545R, temp1&-5.55C + temp2&22F + temp3&267K + temp4&481R, temp2&32F + temp3&273K + temp4&491R, temp1&-17C + temp3&256K + temp4&461R

output4 is same as output3.

I tried using maps and itemgetter,but could'nt seem to get it.

 new_list = []    
 for temp, i in zip(list1,list2):
    for j in i:
       new_list.append(temp, list2[i][j], list2[i+1][j], list2[i+2][j], list2[len(list2)][j])

  for n in list1:    #create n lists for each temp
      outputlistn = []

  for i in new_list:
      for j in i:
          for k in j:
              if k != 0
                 outputlistn.append(j)

I can't figure out how to remove column containing zero.

for n in list1:
      outputn = ",".join("+".join("&".join(k)for k in outputlistn))

Not sure what the sum means in your outputX (is it addind integer values, is it concatenating strings?), but assuming you know what to do with them, here is how you can get those 'trimmed' arrays:

arr = np.array([list1]+list2).transpose()

for row in arr:
    cols = list(filter(lambda i: row[i] != '0',range(arr.shape[1])))
    trimmed = arr[:, cols]
    # do whatever you need to format your 'sum'

Note that you probably don't really need to add [list1] to arr and transpose but I still include it to match your display... If you want to get rid of them just use:

arr = np.array(list2)

for col in range(arr.shape[1]):
    rows = list(filter(lambda i: arr[i,col] != '0',range(arr.shape[0])))
    trimmed = arr[rows]
    # do whatever you need to format your 'sum'

EDIT to match your formatting:

arr = np.array(list2)

for col in range(arr.shape[1]):
    rows = list(filter(lambda i: arr[i,col] != '0',range(arr.shape[0])))
    trimmed = arr[rows]
    print ", ".join(" + ".join(temp+"&"+k for temp,k in zip(list1,row) if k != '0') for row in trimmed)
    print ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM