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

2024-09-24 04:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想映射一个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的第1项映射到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中是否有一个0,然后删除其中有0的列

对于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 = 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 = 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

输出4与输出3相同。你知道吗

我试着使用地图和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))

Tags: in列表newfor地图templistjoin
1条回答
网友
1楼 · 发布于 2024-09-24 04:28:03

不确定求和在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 ""

相关问题 更多 >