0

I have written a python program which gives me the following output:

{'D': {('B', 2.0), ('E', 1.0), ('C', 2.0)}, 'E': {('D', 1.0), ('B', 4.0), ('C', 3.0)}, 

'A': {('B', 1.0), ('C', 5.0)}, 'B': {('A', 1.0), ('D', 2.0), ('E', 4.0)}, 'C': {('E', 3.0), 
('A', 5.0), ('D', 2.0)}`}`

These are nodes of a graph and their corresponding weights. I am trying to remove weights from the above output. Can anyone help me with this.? My output should look something like this :

{ "a" : ["b","c"],"b" : ["a", "e","d"],"c" : ["a", "d", "e"],
          "d" : ["b","c","e"],"e" : ["b", "c","d"] }
Rachel
  • 383
  • 2
  • 4
  • 13

3 Answers3

3
{k:[x for x,_ in v] for k,v in d.items()} # use iteritems() in python 2
Out[32]: 
{'A': ['C', 'B'],
 'B': ['D', 'A', 'E'],
 'C': ['D', 'A', 'E'],
 'D': ['E', 'C', 'B'],
 'E': ['C', 'B', 'D']}
roippi
  • 25,533
  • 4
  • 48
  • 73
0
dic = {'D': {('B', 2.0), ('E', 1.0), ('C', 2.0)}, 
       'E': {('D', 1.0), ('B', 4.0), ('C', 3.0)}, 
       'A': {('B', 1.0), ('C', 5.0)}, 
       'B': {('A', 1.0), ('D', 2.0), ('E', 4.0)}, 
       'C': {('E', 3.0), ('A', 5.0), ('D', 2.0)}}

dic_node = {}
for node in dic:
    dic_node.update({node : [nnode for nnode, weigth in dic[node]]})

dic_node_one = {node : [nnode for nnode, weigth in dic[node]] for node in dic}

Out

>>> dic_node_one
{'A': ['C', 'B'],
 'B': ['A', 'E', 'D'],
 'C': ['A', 'D', 'E'],
 'D': ['B', 'E', 'C'],
 'E': ['B', 'C', 'D']}
>>> dic_node
{'A': ['C', 'B'],
 'B': ['A', 'E', 'D'],
 'C': ['A', 'D', 'E'],
 'D': ['B', 'E', 'C'],
 'E': ['B', 'C', 'D']}
embert
  • 7,336
  • 10
  • 49
  • 78
0

Using dictionary comprehension, see Python Dictionary Comprehension:

from operator import itemgetter

dic = {'D': {('B', 2.0), ('E', 1.0), ('C', 2.0)}, 
       'E': {('D', 1.0), ('B', 4.0), ('C', 3.0)}, 
       'A': {('B', 1.0), ('C', 5.0)}, 
       'B': {('A', 1.0), ('D', 2.0), ('E', 4.0)}, 
       'C': {('E', 3.0), ('A', 5.0), ('D', 2.0)}}

# Simply extracting only the 1st element of the tuple from the key.
print {k:[alpha for alpha, num in v] for k,v in dic.items()}
print
# Extracting only the 1st element and sorting the tuples by its 2nd element, ascending.
print {k:[alpha for alpha, num in sorted(v, key=itemgetter(1))] for k,v in dic.items()}
print
# Extracting only the 1st element and sorting the tuples by its 2nd element, descending.
print {k:[alpha for alpha, num in sorted(v, key=itemgetter(1), reverse=True)] for k,v in dic.items()}
print
# Extracting only the 1st element and sorting by alphabetical order.
print {k:[alpha for alpha, num in sorted(v)] for k,v in dic.items()}
print
# Extracting only the 1st element and sorting by alphabetical order, lower()
print {k.lower():[alpha.lower() for alpha, num in sorted(v)] for k,v in dic.items()}

[out]:

{'A': ['C', 'B'], 'C': ['A', 'D', 'E'], 'B': ['A', 'E', 'D'], 'E': ['B', 'C', 'D'], 'D': ['B', 'E', 'C']}

{'A': ['B', 'C'], 'C': ['D', 'E', 'A'], 'B': ['A', 'D', 'E'], 'E': ['D', 'C', 'B'], 'D': ['E', 'B', 'C']}

{'A': ['C', 'B'], 'C': ['A', 'E', 'D'], 'B': ['E', 'D', 'A'], 'E': ['B', 'C', 'D'], 'D': ['B', 'C', 'E']}

{'A': ['B', 'C'], 'C': ['A', 'D', 'E'], 'B': ['A', 'D', 'E'], 'E': ['B', 'C', 'D'], 'D': ['B', 'C', 'E']}

{'a': ['b', 'c'], 'c': ['a', 'd', 'e'], 'b': ['a', 'd', 'e'], 'e': ['b', 'c', 'd'], 'd': ['b', 'c', 'e']}
Community
  • 1
  • 1
alvas
  • 115,346
  • 109
  • 446
  • 738