0
$\begingroup$

I have a dict() with 1000 keys. First 4 entries of dict are like.

{
    'aaa': [1,0,6,8,0,5,9,1,1,0],
    'abc': [1,1,1,2,4,0,0,0,9,8],
    'cfg': [0,0,0,4,3,1,0,0,0,1],
    'cghjj': [7,8,9,2,3,0,0,0,0,0]
}

I want to create a dataset using each key one by one. I want to pick key1. Then create a dataset using function1 using the values of key1 and then pick key2. Creating another dataset using values of key2 and appending the result of key2 to key1 row-wise. Then with key3... and then append the result to the result of key1 and key2 and so on.. up to 1000 keys.

dataset=dict()

create_dataset:
    select values of key 1.
    b1=function1()
    b1=np.asarray(b1)

    then select key 2.
    b2=function1()
    b2=np.asarray(b2)

    np.append(b1,b2,axis=0)

...upto 1000 key.

$\endgroup$
  • $\begingroup$ Your question title does not match with description. What do you want to do exactly? $\endgroup$ – Ankit Seth Jul 13 '18 at 6:02
  • $\begingroup$ if we write for key,values in dataset.items: print key, value. It will print all keys with values. can we do some computation on each key's values before or after printing. Like it should pick key1..perform function 1on key. then pick key2. Basically i am not able to pick keys in order through looping. Before keys are in not numbers ..they are some names and i can pick them manually each time as they are 1000 in number. so i want some automatic way of pick each key. $\endgroup$ – Hazel Jul 13 '18 at 7:30
  • $\begingroup$ Which order of keys? If you want alphabetical order you can use for key in sorted(dataset.keys()):. $\endgroup$ – Ankit Seth Jul 13 '18 at 7:38
  • $\begingroup$ This sounds like you may get more results if you ask on Stack Overflow since it appears to be a pure programming question. $\endgroup$ – kbrose Jul 13 '18 at 13:49
0
$\begingroup$

You could iterate over all the keys and apply function1 to them and them append to the full_data_set. At the end you have all them concatenated.

However, Python lists are unordered, so you don't control the order which keys are chosen.

data = {
    'aaa': [1, 0, 6, 8, 0, 5, 9, 1, 1, 0],
    'abc': [1, 1, 1, 2, 4, 0, 0, 0, 9, 8],
    'cfg': [0, 0, 0, 4, 3, 1, 0, 0, 0, 1],
    'cghjj': [7, 8, 9, 2, 3, 0, 0, 0, 0, 0]
}

def function1(l: list):
    # do something
    return l

full_data_set = []

for key in data.keys():
    full_data_set += function1(data[key])

If you do want to control the order of the keys, you need to make the ordered list of the keys somehow.

key_order = ['aaa', 'abc', 'cfg', 'cghjj']

data = {
    'aaa': [1, 0, 6, 8, 0, 5, 9, 1, 1, 0],
    'abc': [1, 1, 1, 2, 4, 0, 0, 0, 9, 8],
    'cfg': [0, 0, 0, 4, 3, 1, 0, 0, 0, 1],
    'cghjj': [7, 8, 9, 2, 3, 0, 0, 0, 0, 0]
}

def function1(l: list):
    # do something
    return l

full_data_set = []

for key in key_order:
    full_data_set += function1(data[key])
$\endgroup$
  • $\begingroup$ @Hazel, my pleasure :) $\endgroup$ – BrunoGL Jul 15 '18 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.