[英]Making lists from a dict of lists
I have this dict
: 我有这样的
dict
:
{'Hours Outside Sprint': [5.25, 5.0, 0.0],
'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'],
'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'],
'Status': ['done', 'done', 'done'],
'Story': ['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
'Story Actual (Hrs)': [11.0, 12.75, 0.0],
'Story Estimate (Hrs)': [16.0, 12.0, 0.0]}
I think this is a fairly simple task but the solution is not apparent at the moment. 我认为这是一个相当简单的任务,但目前解决方案尚不明确。 What I want to do is iterate through this
dict
and make the following: 我想要做的是遍历此
dict
并进行以下操作:
[[done, 2017-02-14, SPGC-14075, 16.0, 5.25, 2017-01-31, 11.0], ... ]
So all the 1st elements of each list go together, all the 2nd, and so on until I have a list of lists. 因此,每个列表的所有第一个元素都放在一起,所有第二个元素都在一起,依此类推,直到有列表列表为止。 How do I do this?
我该怎么做呢?
EDIT: 编辑:
Here is what the pandas dataframe looks liek that produced the above dict: 这是产生上述命令的pandas数据框的外观:
Story Status Story Estimate (Hrs) Story Actual (Hrs) Hours Outside Sprint Sprint Start Sprint End
0 SPGC-14075 done 16.0 11.00 5.25 2017-01-31 2017-02-14
1 SPGC-9456 done 12.0 12.75 5.00 2017-01-31 2017-02-14
2 SPGC-9445 done 0.0 0.00 0.00 2017-01-31 2017-02-14
Would iterrows
work? 将
iterrows
工作?
Here's how I would do this in Python: 这是我在Python中的操作方法:
df_dict = {'Status': [u'done', u'done', u'done'], 'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'], 'Story': [u'SPGC-14075', u'SPGC-9456', u'SPGC-9445'], 'Story Estimate (Hrs)': [16.0, 12.0, 0.0], 'Hours Outside Sprint': [5.25, 5.0, 0.0], 'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'], 'Story Actual (Hrs)': [11.0, 12.75, 0.0]}
result = []
lengthOfFirstArrInDict = len(df_dict[df_dict.keys()[0]])
for i in range(0, lengthOfFirstArrInDict):
nestedList = []
for key in df_dict.keys():
nestedList.append(df_dict[key][i])
result.append(nestedList)
print(result)
And here's the output: 这是输出:
[['done', '2017-02-14', 'SPGC-14075', 16.0, 5.25, '2017-01-31', 11.0], ['done', '2017-02-14', 'SPGC-9456', 12.0, 5.0, '2017-01-31', 12.75], ['done', '2017-02-14', 'SPGC-9445', 0.0, 0.0, '2017-01-31', 0.0]]
df.iterrows
makes a pretty neat solution. df.iterrows
了一个非常简洁的解决方案。 Make sure to slice out the row index: 确保切出行索引:
( i[0] = row_index; i[1] = row_values
) (
i[0] = row_index; i[1] = row_values
)
df = pd.DataFrame(df_dict)
#re-order columns (may not be necessary depending on your original df)
df = df[['Status','Sprint End','Story','Story Estimate (Hrs)','Hours Outside Sprint','Sprint Start','Story Actual (Hrs)']]
values = [i[1].tolist() for i in df.iterrows()]
Whenever you need to combine successive elements from two or more sequences in Python, think zip()
: 每当需要在Python中组合两个或多个序列中的连续元素时,请考虑
zip()
:
from pprint import pprint
data = {'Hours Outside Sprint': [5.25, 5.0, 0.0],
'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'],
'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'],
'Status': ['done', 'done', 'done'],
'Story': ['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
'Story Actual (Hrs)': [11.0, 12.75, 0.0],
'Story Estimate (Hrs)': [16.0, 12.0, 0.0]}
# desired order of items in the result
key_order = ('Status', 'Sprint End', 'Story', 'Story Estimate (Hrs)',
'Hours Outside Sprint', 'Sprint Start', 'Story Actual (Hrs)')
pprint([x[0] for x in zip(data[k] for k in key_order)])
Output: 输出:
[['done', 'done', 'done'],
['2017-02-14', '2017-02-14', '2017-02-14'],
['SPGC-14075', 'SPGC-9456', 'SPGC-9445'],
[16.0, 12.0, 0.0],
[5.25, 5.0, 0.0],
['2017-01-31', '2017-01-31', '2017-01-31'],
[11.0, 12.75, 0.0]]
map(lambda x: list(x),zip(*map(lambda (k,v): v, df_dict.iteritems())))
or 要么
map(lambda x: list(x),zip(*df_dict.values()))
you could strip absolving method call one by one to see what you got each step 您可以逐个剥离绝对方法调用,以查看每一步得到了什么
It's no more than transforming of your data. 仅仅是转换数据而已。
*df_dict.values()
means you could pass a list as parameters for a function that needs arguments likes this: *df_dict.values()
表示您可以将列表作为参数传递给需要像这样的参数的函数:
def fun(arg1, arg2, arg3 ...)
You can try this: 您可以尝试以下方法:
df_dict = {'Status': [u'done', u'done', u'done'], 'Sprint End': ['2017-02-14', '2017-02-14', '2017-02-14'], 'Story': [u'SPGC-14075', u'SPGC-9456', u'SPGC-9445'], 'Story Estimate (Hrs)': [16.0, 12.0, 0.0], 'Hours Outside Sprint': [5.25, 5.0, 0.0], 'Sprint Start': ['2017-01-31', '2017-01-31', '2017-01-31'], 'Story Actual (Hrs)': [11.0, 12.75, 0.0]}
vals = df_dict.values()
final_data = list(map(list, zip(*vals)))
print(final_data)
Output: 输出:
[[16.0, 5.25, 11.0, '2017-02-14', 'done', 'SPGC-14075', '2017-01-31'], [12.0, 5.0, 12.75, '2017-02-14', 'done', 'SPGC-9456', '2017-01-31'], [0.0, 0.0, 0.0, '2017-02-14', 'done', 'SPGC-9445', '2017-01-31']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.