Python:如何基于变量创建数组数组?

时间:2016-02-10 15:55:34

标签: python arrays

我想创建一个数组数组。

我得到一个名为data的数组,我无法改变。它显示在下面的代码中。

我想要做的是获取数据数组并创建每个数据标题(内存,网络等)的新数组,并且每个标题都有与之对应的数字。

到目前为止,我可以检测数据中的条目何时对应于潜在标题的给定数组。但是,我不确定如何在条目后命名新数组以及如何将所有这些新数组放入一个数组中。

.factory('Service1', ['NotificationService',
function(NotificationService) {
    var someData = ...;
    return {
       // define public API for Service1 here
       callService2Method: function() {
         // publish event
         NotificationService.event1Happened(someData);
       }
    }
}])

我想要实现的例子:

data = ['Memory', 1, 2, 3, 4, 5, 6, 27, 'Network', 7, 8, 9, 10, 11, 12, 13, 14, 'Processes', 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 'CPU', 30, 31, 32, 33, 34, 35, 'System']

for j in range(0,len(titles)):
    for entry in data:
        if titles[j] == entry:
            # Need to add code in here

,其中

new_array=[Memory,Network,Processes,CPU,System]

6 个答案:

答案 0 :(得分:1)

你不想要一个"阵列" (实际上在Python中这些被称为列表),你想要一本字典。现在你需要遍历数据,检查你是否有一个字符串,如果你有,请启动一个新的值列表以附加到你的字典。

d = {}
key = None
for elem in data:
    if isinstance(elem, str):
        if key:
            d[key] = values
        values = []
        key = elem
    else:
        values.append(elem)
d[key] = values

结果:

{'CPU': [30, 31, 32, 33, 34, 35],
 'Memory': [1, 2, 3, 4, 5, 6, 27],
 'Network': [7, 8, 9, 10, 11, 12, 13, 14],
 'Processes': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29],
 'System': []}

答案 1 :(得分:1)

您可能想要使用字典结构。像这样:

data = ['Memory', 1, 2, 3, 4, 5, 6, 27, 'Network', 7, 8, 9, 10, 11, 12, 13, 14, 'Processes', 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 'CPU', 30, 31, 32, 33, 34, 35, 'System']

output = {}
for j in data:
    if type(j) is int:
        current.append(j)
    else:
        current = []
        output[j] = current

output = {'System': [], 'Processes': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29], 'CPU': [30, 31, 32, 33, 34, 35], 'Network': [7, 8, 9, 10, 11, 12, 13, 14], 'Memory': [1, 2, 3, 4, 5, 6, 27]}

答案 2 :(得分:0)

我认为字典是您正在寻找的。我不确定标题是什么,但我假设它是一个字符串列表,所以我只检查字符串类型

data = ['Memory', 1, 2, 3, 4, 5, 6, 27, 'Network', 7, 8, 9, 10, 11, 12, 13, 14, 'Processes', 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 'CPU', 30, 31, 32, 33, 34, 35, 'System']
outDict = {}

for entry in data:
    if type(entry)==str: #Replace with titles check if necessary
       title = entry
       outDict[entry] = []
    else:
       outDict[title].append(entry)

print(outDict)

产生输出

{'Memory': [1, 2, 3, 4, 5, 6, 27], 'Processes': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29], 'CPU': [30, 31, 32, 33, 34, 35], 'System': [], 'Network': [7, 8, 9, 10, 11, 12, 13, 14]}

答案 3 :(得分:0)

以下是defaultdict的另一种解决方案:

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for x in data:
...     if isinstance(x, str):
...         key = x
...     else:
...         d[key].append(x)

d没有密钥'System',但由于它是defaultdict,因此当您要求密钥时,您将获得一个空列表。

>>> d
defaultdict(<type 'list'>, {'Processes': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29], 'CPU': [30, 31, 32, 33, 34, 35], 'Network': [7, 8, 9, 10, 11, 12, 13, 14], 'Memory': [1, 2, 3, 4, 5, 6, 27]})
>>> d['System']
[]

答案 4 :(得分:0)

以防你真的想要一个&#34;数组&#34;,并假设titlesdata中要查找的部分的列表它们应该出现在result

data = ['Memory', 1, 2, 3, 4, 5, 6, 27, 'Network', 7, 8, 9, 10, 11, 12, 13, 14, 'Processes', 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 'CPU', 30, 31, 32, 33, 34, 35, 'System']
titles = ['Memory', 'Network','Processes', 'CPU', 'System']

parts = {}
result = []
for t in titles:
    parts[t] = []
    result.append( parts[t] )
currPart = None
for d in data:
    if d in titles:
        currPart = d
    else:
        parts[currPart].append( d )
print result 

答案 5 :(得分:0)

以下是一些将结果放入 res

的代码
import pprint
data = ['Memory', 1, 2, 3, 4, 5, 6, 27, 'Network', 7, 8, 9, 10, 11, 12, 13, 14, 'Processes', 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 'CPU', 30, 31, 32, 33, 34, 35, 'System']

res = {}
name = 'default' # in case you do not start with a label
for a in data:
    if type(a) is str:
        name = a
        if name not in res: # in case a label appear more than once
            res[name] = []
    else:
        res[name].append(a)

pprint.pprint(res)

输出:

{'CPU': [30, 31, 32, 33, 34, 35],
 'Memory': [1, 2, 3, 4, 5, 6, 27],
 'Network': [7, 8, 9, 10, 11, 12, 13, 14],
 'Processes': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29],
 'System': []}