1

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=[]
2
  • What would be in titles? Commented Feb 10, 2016 at 15:57
  • 1
    Python list is not the same as array. Commented Feb 10, 2016 at 15:58

6 Answers 6

1

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]}
Sign up to request clarification or add additional context in comments.

1 Comment

Simple code that does exactly what I wanted, Thank you very much!
1

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

Comments

0

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

Comments

0

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

Comments

0

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 

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.