简体   繁体   English

追加到另一个数组python中的数组

[英]append to array inside another array python

Basically I have data that spans several years and I would like to append all this data into an array using one array and the years as keys. 基本上,我有跨越数年的数据,我想使用一个数组和年份作为键将所有这些数据附加到一个数组中。 Then would would like to callback all the data in the array relevant to a particular year by just calling the date. 然后想通过仅调用日期来回调数组中与特定年份相关的所有数据。

for example: 例如:

all data that is relevant to 2016 would be as such 与2016年相关的所有数据都将是这样

array[2016].append('1')
array[2016].append('2')

and then when the date changes to 2015 然后当日期更改为2015年

array[2015].append('3')
array[2015].append('4')

and then call the data 然后调用数据

print array[2016]
1,2

How about python dictionaries: python字典怎么样:

>>> from collections import defaultdict
>>> data = defaultdict(list)
>>> data[2016].append('1')
>>> data[2016].append('2')
>>> data[2015].append('3')
>>> data[2015].append('4')
>>> print data[2016]
['1', '2']
>>> print data[2015]
['3', '4']

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

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