Creating an alternating dictionary with a loop in Python -


i'm trying create dictionary loop, alternating entries, although entries not need alternating, long in dictionary, need simplest solution them in 1 dictionary. simple example of i'm trying achieve:

normally, creating dictionary loop this:

{i:9 in range(3)} output: {0: 9, 1: 9, 2: 9} 

now i'm trying follwing:

{i:9, i+5:8 in range(3)} syntaxerror: invalid syntax 

the desired output is:

{0:9, 5:8, 1:9, 6:8, 2:9, 7:8} 

or, output fine, long elements in dictionary (order not matter):

{0:9, 1:9, 2:9, 5:8, 6:8, 7:8} 

the context i'm using

theano.clone(cloned_item, replace = {replace1: new_item1, replace2: new_item2}) 

where items want replace must given in 1 dictionary.

thanks in advance!

comprehensions cool, not strictly necessary. also, standard dictionaries cannot have duplicate keys. there data structures that, can try having list of values key:

d = {} d[8] = [] in range(3):     d[i] = 9     d[8].append(i) 

 

>>> d {8: [0, 1, 2], 0: 9, 2: 9, 1: 9} 

for new example without duplicate keys:

d = {} in range(3):     d[i] = 9     d[i+5] = 8 

 

>>> d {0: 9, 1: 9, 2: 9, 5: 8, 6: 8, 7: 8} 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -