Catherine
Catherine

Reputation: 747

Python: How to create an array of arrays based on a variable?

I want to create an array of arrays.

I get an array called data which I can’t change. It is shown in the code below.

What I want to do is take the data array and create a new array of each data title (Memory,Network etc.) and for each of those titles have the numbers that correspond to them.

So far I can detect when the entry in data corresponds to a give array of potential titles. However I am unsure how to name a new array after an entry and how to put all of these new arrays into one array.

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

example of what I want to achieve:

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

where

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,31,33,34,35]
System=[]

Upvotes: 1

Views: 861

Answers (6)

François
François

Reputation: 325

Here is some code that put the result into 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)

Output:

{'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': []}

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49803

Just in case you really do want an "array of arrays", and assuming that titles is a list of the sections to look for in data in the order they should appear in your 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 

Upvotes: 0

timgeb
timgeb

Reputation: 78670

Here's another solution with defaultdict:

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

d does not have the key 'System', but since it is a defaultdict you'll get an empty list when you ask for that key.

>>> 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']
[]

Upvotes: 0

Obsidian
Obsidian

Reputation: 515

I think a dictionary is what you're looking for. I'm not sure what titles is but I'm assuming its a list of strings, and so I'm checking just for string type

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)

yielding the output

{'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]}

Upvotes: 0

tom10
tom10

Reputation: 69182

You probably want to use the dictionary structure. Something like this:

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]}

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599530

You don't want an "array" (actually in Python these are called lists), you want a dictionary. Now you need to iterate through the data, checking if you have a string, and if you have, start a new list of values to append to your dict.

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

Result:

{'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': []}

Upvotes: 1

Related Questions