在python中使用嵌套映射和itemgetter进行解析

时间:2015-11-23 05:08:08

标签: python list

我想用嵌套列表

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

我想像这样映射

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

那就是将list1的第一个术语映射到list2的第n个术语,就像那样。

喜欢

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

然后我想考虑每个temp并检查temp_n中是否有零,然后删除其中为零的列

表示temp1

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

获取

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

现在我的temp1的output1应该是

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

(不要考虑temp2& 0)

表示temp2

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

所以它变成了

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

现在我的temp2的output2应该是

 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和temp4没有零,所以它们的输出是

   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与output3相同。

我尝试使用地图和itemgetter,但似乎无法得到它。

 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)

我无法弄清楚如何删除包含零的列。

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

1 个答案:

答案 0 :(得分:2)

不确定总和在你的outputX中意味着什么(是addind整数值,它是连接字符串吗?),但假设你知道如何处理它们,这里是你如何得到那些'修剪'阵列:

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'

请注意,您可能不需要将[list1]添加到arr并进行转置,但我仍然会将其包含在与您的显示内容相匹配的位置...如果您想要摆脱它们,只需使用:

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'

编辑以匹配您的格式:

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 ""