Home > OS >  How to merge two list into one dictionary?
How to merge two list into one dictionary?

Time:11-03

I have this function setValue(binaryString) that takes inputs binary string "binaryString" and returns the set of positions between 0 and len(binaryString)-1 whose corresponding entry in binaryString is '1'.

so for example:

binaryString = 10101 will return set {0, 2, 4}. the returned set indicates the position of number '1' ini binaryString

The final result is will be put together in dictionary D={}, with binaryString as key and stringToSet(binaryString) as value.

So far I have tried this code:

def setValue(binaryString):
    values = set()
    for pos,char in enumerate(binaryString):
        if(char == '1'):
            values.add(pos)
    print(values)

def main():
    D = {}
    keys = []
    while True:
        binaryString = input(str("Input Binary String: "))
        if binaryString == "exit":
            break
        else:
            keys.append(binaryString)

    print(keys)
    for i in keys:
        setValue(i)
                
if __name__ == "__main__":
    main()

my code resulted in:

Input Binary String: 10101
Input Binary String: 110011
Input Binary String: exit
['10101', '110011']
{0, 2, 4}
{0, 1, 4, 5}

while I wish to get result like this:

Input Binary String: 10101
Input Binary String: 0 
Input Binary String: 1 
Input Binary String: 1111 
Input Binary String: exit 
10101: {0, 2, 4} 
0: set()
1: {0} 
1111: {0, 1, 2, 3}

I don't know how to fetch the key from binaryString and value from setValue(binaryString) and put them in dictionary D{}

Thanks in advance

CodePudding user response:

If you have a list of tuples [(a, b), (c, d)...], you can use them as input to the dict constructor to make a dict object:

>>> dict ([(1, 2), (3, 4)])
{1: 2, 3: 4}

If you have two lists, you can use the zip function to create an iterable sequence of tuples, taking elements from each list:

>>> list(zip([1,3], [2,4]))
[(1, 2), (3, 4)]
>>> 

Putting these together, if you have two lists and you want one to be the keys and the other to be the values in a dict, you can zip them together and use the dict constructor:

>>> keys = [1,3]
>>> vals = [2, 4]
>>> dict(zip(keys, vals))
{1: 2, 3: 4}
>>> 

CodePudding user response:

You need to return the values from your setValues function and then you can use a dictionary-comprehension to construct the dictionary

def setValue(binaryString):
    values = set()
    for pos, char in enumerate(binaryString):
        if char == '1':
            values.add(pos)
    return values
strings = ['10101', '110011']
d = {s: setValue(s) for s in strings}

Output

{'10101': {0, 2, 4}, '110011': {0, 1, 4, 5}}

CodePudding user response:

import itertools

inputs = [10101, 0, 1, 1111]

for i in inputs:
    s = str(i)
    indices = range(len(s))
    selectors = map(int,list(s))
    c = itertools.compress(indices,selectors)
    print(f'{i}: {set(c)}')

The itertools.compress will remove the indices that have 0 in the binary string.

Outputs:

10101: {0, 2, 4}
0: set()
1: {0}
1111: {0, 1, 2, 3}
  • Related