繁体   English   中英

将字典值转换为列表

[英]Converting dictionary values to a list

我写了一个 for 循环,将我生成的值保存到字典中:

minThresh = d['move'].min()
minThresh = minThresh.astype(int)
maxThresh = -4
allLeverPos = {}

for i in range (minThresh,maxThresh):
    threshPos_idx = d['move']<=i
    threshPos_binary = np.array(d['move']<=i, dtype=int)
    threshCross_idx = np.where(np.diff(threshPos_binary) == 1)
    lever_thresh = len(threshCross_idx[0]) 
    allLeverPos[i]= lever_thresh
print(allLeverPos)

{-32: 1, -31: 3, -30: 122, -29: 140, -28: 170, -27: 219, -26: 238, -25: 285, -24: 315, -23: 370, -22: 433, -21: 484, -20: 528, -19: 582, -18: 638, -17: 683, -16: 728, -15: 789, -14: 845, -13: 908, -12: 1005, -11: 1057, -10: 1142, -9: 1169, -8: 1234, -7: 1277, -6: 1327, -5: 1365}

我想使用字典中的值来创建一个列表

list = (1, 3, 122, 140, 170...) 

我试着用这行代码做到这一点:

x = list(allLeverPos.values())

但是,我收到此错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-870-67da022ee34c> in <module>
----> 1 list(allLeverPos.values())
      2 #allLeverPosList = list(allLeverPos.values())

TypeError: 'dict_values' object is not callable

有人可以指导我出了什么问题/另一种方法吗?

您可以使用列表推导来提取所有值:

x = [value for key,value in allLeverPos.items()]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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