Why does my code take different values when i switch the order in a set (knowing that order doesn't matter...












8















I'm trying to implement a method that returns the edges of a graph, represented by an adjacency list/dictionary.



So to iterate through the dictionary, first I iterated through the keys, then through every value stored in the corresponding key. Inside the nested for-loop, I had a condition where, if a particular edge, say (a,b) is not in the set of edges, then add it to the set -- pass otherwise. On my first run, the method took in edges that are the same -- that is, in the set of edges, there are (a,b) and (b,a).



class Graph():
def __init__(self, grph={}):
self.graph = grph

def get_vertices(self):
for keys in self.graph:
yield keys

def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (key, adj_node) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


def main():
graph1 = {
'A': ['B','C','D'],
'B': ['A','E'],
'C': ['A', 'D'],
'D': ['A', 'C'],
'E': ['B'],
}
graph_one = Graph(graph1)
print(list(graph_one.get_vertices()))
print(graph_one.get_edges())

if __name__ =='__main__':
main()


the output is:




{('A','B'),('D','A'),('B','A'),('B','E'),('A','D'),('D','C'),('E','B'),('C','D'),('A','C'),('C','A')}




So what I did was that, I just changed the if statement:

"if (adj_node, key) not in edges:"



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Now the output was:




{('C','D'),('A','B'),('E','B'),('A','C'),('A','D')}




Im very curious as to why it is so, and I'd be so thankful if you guys could explain it to me. Thanks in advance!










share|improve this question

























  • you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

    – Jean-François Fabre
    Jan 13 at 13:10






  • 1





    The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

    – roganjosh
    Jan 13 at 13:11













  • sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

    – Francis Magnusson
    Jan 13 at 13:19











  • That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

    – roganjosh
    Jan 13 at 13:20











  • So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

    – Tiw
    Jan 13 at 13:47
















8















I'm trying to implement a method that returns the edges of a graph, represented by an adjacency list/dictionary.



So to iterate through the dictionary, first I iterated through the keys, then through every value stored in the corresponding key. Inside the nested for-loop, I had a condition where, if a particular edge, say (a,b) is not in the set of edges, then add it to the set -- pass otherwise. On my first run, the method took in edges that are the same -- that is, in the set of edges, there are (a,b) and (b,a).



class Graph():
def __init__(self, grph={}):
self.graph = grph

def get_vertices(self):
for keys in self.graph:
yield keys

def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (key, adj_node) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


def main():
graph1 = {
'A': ['B','C','D'],
'B': ['A','E'],
'C': ['A', 'D'],
'D': ['A', 'C'],
'E': ['B'],
}
graph_one = Graph(graph1)
print(list(graph_one.get_vertices()))
print(graph_one.get_edges())

if __name__ =='__main__':
main()


the output is:




{('A','B'),('D','A'),('B','A'),('B','E'),('A','D'),('D','C'),('E','B'),('C','D'),('A','C'),('C','A')}




So what I did was that, I just changed the if statement:

"if (adj_node, key) not in edges:"



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Now the output was:




{('C','D'),('A','B'),('E','B'),('A','C'),('A','D')}




Im very curious as to why it is so, and I'd be so thankful if you guys could explain it to me. Thanks in advance!










share|improve this question

























  • you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

    – Jean-François Fabre
    Jan 13 at 13:10






  • 1





    The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

    – roganjosh
    Jan 13 at 13:11













  • sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

    – Francis Magnusson
    Jan 13 at 13:19











  • That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

    – roganjosh
    Jan 13 at 13:20











  • So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

    – Tiw
    Jan 13 at 13:47














8












8








8


2






I'm trying to implement a method that returns the edges of a graph, represented by an adjacency list/dictionary.



So to iterate through the dictionary, first I iterated through the keys, then through every value stored in the corresponding key. Inside the nested for-loop, I had a condition where, if a particular edge, say (a,b) is not in the set of edges, then add it to the set -- pass otherwise. On my first run, the method took in edges that are the same -- that is, in the set of edges, there are (a,b) and (b,a).



class Graph():
def __init__(self, grph={}):
self.graph = grph

def get_vertices(self):
for keys in self.graph:
yield keys

def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (key, adj_node) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


def main():
graph1 = {
'A': ['B','C','D'],
'B': ['A','E'],
'C': ['A', 'D'],
'D': ['A', 'C'],
'E': ['B'],
}
graph_one = Graph(graph1)
print(list(graph_one.get_vertices()))
print(graph_one.get_edges())

if __name__ =='__main__':
main()


the output is:




{('A','B'),('D','A'),('B','A'),('B','E'),('A','D'),('D','C'),('E','B'),('C','D'),('A','C'),('C','A')}




So what I did was that, I just changed the if statement:

"if (adj_node, key) not in edges:"



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Now the output was:




{('C','D'),('A','B'),('E','B'),('A','C'),('A','D')}




Im very curious as to why it is so, and I'd be so thankful if you guys could explain it to me. Thanks in advance!










share|improve this question
















I'm trying to implement a method that returns the edges of a graph, represented by an adjacency list/dictionary.



So to iterate through the dictionary, first I iterated through the keys, then through every value stored in the corresponding key. Inside the nested for-loop, I had a condition where, if a particular edge, say (a,b) is not in the set of edges, then add it to the set -- pass otherwise. On my first run, the method took in edges that are the same -- that is, in the set of edges, there are (a,b) and (b,a).



class Graph():
def __init__(self, grph={}):
self.graph = grph

def get_vertices(self):
for keys in self.graph:
yield keys

def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (key, adj_node) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


def main():
graph1 = {
'A': ['B','C','D'],
'B': ['A','E'],
'C': ['A', 'D'],
'D': ['A', 'C'],
'E': ['B'],
}
graph_one = Graph(graph1)
print(list(graph_one.get_vertices()))
print(graph_one.get_edges())

if __name__ =='__main__':
main()


the output is:




{('A','B'),('D','A'),('B','A'),('B','E'),('A','D'),('D','C'),('E','B'),('C','D'),('A','C'),('C','A')}




So what I did was that, I just changed the if statement:

"if (adj_node, key) not in edges:"



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Now the output was:




{('C','D'),('A','B'),('E','B'),('A','C'),('A','D')}




Im very curious as to why it is so, and I'd be so thankful if you guys could explain it to me. Thanks in advance!







python python-3.x data-structures






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 at 14:00









roganjosh

6,45931328




6,45931328










asked Jan 13 at 13:05









Francis MagnussonFrancis Magnusson

464




464













  • you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

    – Jean-François Fabre
    Jan 13 at 13:10






  • 1





    The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

    – roganjosh
    Jan 13 at 13:11













  • sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

    – Francis Magnusson
    Jan 13 at 13:19











  • That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

    – roganjosh
    Jan 13 at 13:20











  • So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

    – Tiw
    Jan 13 at 13:47



















  • you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

    – Jean-François Fabre
    Jan 13 at 13:10






  • 1





    The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

    – roganjosh
    Jan 13 at 13:11













  • sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

    – Francis Magnusson
    Jan 13 at 13:19











  • That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

    – roganjosh
    Jan 13 at 13:20











  • So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

    – Tiw
    Jan 13 at 13:47

















you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

– Jean-François Fabre
Jan 13 at 13:10





you mean to sort the data or use an helper set with sorted data to avoid insertion. What's the type of key and adj_node?

– Jean-François Fabre
Jan 13 at 13:10




1




1





The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

– roganjosh
Jan 13 at 13:11







The check is irrelevant anyway because the set can only contain one of each value, so you can add the same value multiple times and you'll still only have one instance at the end in your set.

– roganjosh
Jan 13 at 13:11















sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

– Francis Magnusson
Jan 13 at 13:19





sorry, i forgot to say (or emphasize, I suppose) that I'm not asking how to remove the duplicates! I was just wondering why it ignored the duplicates when I changed the condition on the if-statement

– Francis Magnusson
Jan 13 at 13:19













That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

– roganjosh
Jan 13 at 13:20





That's fine, my comment was somewhat tangential to what you're asking, but I still think it's valid. It's very possible I'm not seeing the full picture but I can't see the purpose of the check at all.

– roganjosh
Jan 13 at 13:20













So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

– Tiw
Jan 13 at 13:47





So simple explaination is that set will only holds one instance of each value, and you used if to check opposite direction's tuple, so in effect both directions' duplicates are avoided.

– Tiw
Jan 13 at 13:47












2 Answers
2






active

oldest

votes


















9














When we say that sets have no order or that order doesn't matter, it means that {x, y} == {y, x}. But (a, b) and (b, a) are tuples, order matters for them, so (a, b) != (b, a) and therefore {(a, b), (b, a)} is a set with two distinct elements, although it's equal to {(b, a), (a, b)}.



When your code looks like this:



        if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)


then when the edge a <-> b is first encountered, it's as (key, adj_node) == (a, b) and is added to the set. When it's encountered the second (and only other) time, it's as (key, adj_node) == (b, a), meaning (adj_node, key) == (a, b) which is already in the set so (adj_node, key) not in edges is false and (b, a) doesn't get added to the set.






share|improve this answer



















  • 1





    Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

    – Francis Magnusson
    Jan 13 at 13:52











  • Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

    – Chris_Rands
    Jan 25 at 16:25



















3














I think it just needs a little change, try this:



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if ((key, adj_node) not in edges) and ((adj_node, key) not in edges):
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Update:

So it is an Undigraph.

And it's me overcomplicated this.

And your way is actually better than my checking both ways.



The reason your code succeed, is that set will only contain one instance of any value.

So each time do the add, if there's already same tuple exists, it simply won't change the set.

And you already used the if to check the tuple of opposite direction, so it won't create duplicate edges.



For example, when (a, b) hits the if checking, it will check (b,a) exists in the set or not, if exists, then pass. If not, add (a, b) in the set, if (a, b) exists, the set won't change since only one instace will be in the set.

And later when looped to (b, a), since (a, b) already in the set, the if will be false and passed.

So by this way, the set is safe, free of duplicate edges.






share|improve this answer





















  • 1





    I still don't get the point of the check because the set will only contain one instance of any value

    – roganjosh
    Jan 13 at 13:17











  • So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

    – roganjosh
    Jan 13 at 13:21













  • Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

    – roganjosh
    Jan 13 at 13:26








  • 1





    @FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

    – roganjosh
    Jan 13 at 13:55






  • 1





    oh is that so haha... thanks, then! :D

    – Francis Magnusson
    Jan 13 at 14:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54169158%2fwhy-does-my-code-take-different-values-when-i-switch-the-order-in-a-set-knowing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









9














When we say that sets have no order or that order doesn't matter, it means that {x, y} == {y, x}. But (a, b) and (b, a) are tuples, order matters for them, so (a, b) != (b, a) and therefore {(a, b), (b, a)} is a set with two distinct elements, although it's equal to {(b, a), (a, b)}.



When your code looks like this:



        if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)


then when the edge a <-> b is first encountered, it's as (key, adj_node) == (a, b) and is added to the set. When it's encountered the second (and only other) time, it's as (key, adj_node) == (b, a), meaning (adj_node, key) == (a, b) which is already in the set so (adj_node, key) not in edges is false and (b, a) doesn't get added to the set.






share|improve this answer



















  • 1





    Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

    – Francis Magnusson
    Jan 13 at 13:52











  • Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

    – Chris_Rands
    Jan 25 at 16:25
















9














When we say that sets have no order or that order doesn't matter, it means that {x, y} == {y, x}. But (a, b) and (b, a) are tuples, order matters for them, so (a, b) != (b, a) and therefore {(a, b), (b, a)} is a set with two distinct elements, although it's equal to {(b, a), (a, b)}.



When your code looks like this:



        if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)


then when the edge a <-> b is first encountered, it's as (key, adj_node) == (a, b) and is added to the set. When it's encountered the second (and only other) time, it's as (key, adj_node) == (b, a), meaning (adj_node, key) == (a, b) which is already in the set so (adj_node, key) not in edges is false and (b, a) doesn't get added to the set.






share|improve this answer



















  • 1





    Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

    – Francis Magnusson
    Jan 13 at 13:52











  • Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

    – Chris_Rands
    Jan 25 at 16:25














9












9








9







When we say that sets have no order or that order doesn't matter, it means that {x, y} == {y, x}. But (a, b) and (b, a) are tuples, order matters for them, so (a, b) != (b, a) and therefore {(a, b), (b, a)} is a set with two distinct elements, although it's equal to {(b, a), (a, b)}.



When your code looks like this:



        if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)


then when the edge a <-> b is first encountered, it's as (key, adj_node) == (a, b) and is added to the set. When it's encountered the second (and only other) time, it's as (key, adj_node) == (b, a), meaning (adj_node, key) == (a, b) which is already in the set so (adj_node, key) not in edges is false and (b, a) doesn't get added to the set.






share|improve this answer













When we say that sets have no order or that order doesn't matter, it means that {x, y} == {y, x}. But (a, b) and (b, a) are tuples, order matters for them, so (a, b) != (b, a) and therefore {(a, b), (b, a)} is a set with two distinct elements, although it's equal to {(b, a), (a, b)}.



When your code looks like this:



        if (adj_node, key) not in edges:
edge = (key, adj_node)
edges.add(edge)


then when the edge a <-> b is first encountered, it's as (key, adj_node) == (a, b) and is added to the set. When it's encountered the second (and only other) time, it's as (key, adj_node) == (b, a), meaning (adj_node, key) == (a, b) which is already in the set so (adj_node, key) not in edges is false and (b, a) doesn't get added to the set.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 13 at 13:21









Alex HallAlex Hall

21.8k32053




21.8k32053








  • 1





    Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

    – Francis Magnusson
    Jan 13 at 13:52











  • Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

    – Chris_Rands
    Jan 25 at 16:25














  • 1





    Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

    – Francis Magnusson
    Jan 13 at 13:52











  • Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

    – Chris_Rands
    Jan 25 at 16:25








1




1





Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

– Francis Magnusson
Jan 13 at 13:52





Looks like i got tuples and sets mixed up! thanks for pointing that out! It took me quite a while to understand your explanation on the if statement, but makes sense to me now; when it checks at "if (adj_node, key) not in edges" it still takes into account the tuple stored previously by the "edge = (key, adj_node)" statement, which is basically the same. Thanks!

– Francis Magnusson
Jan 13 at 13:52













Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

– Chris_Rands
Jan 25 at 16:25





Equality testing and ordering are not quite the same- for example in Python 3.7 dicts retain insertion order but {1:1,2:2} == {2:2,1:1} remains True (not the case for OrderedDict though stackoverflow.com/questions/50872498/…)

– Chris_Rands
Jan 25 at 16:25













3














I think it just needs a little change, try this:



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if ((key, adj_node) not in edges) and ((adj_node, key) not in edges):
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Update:

So it is an Undigraph.

And it's me overcomplicated this.

And your way is actually better than my checking both ways.



The reason your code succeed, is that set will only contain one instance of any value.

So each time do the add, if there's already same tuple exists, it simply won't change the set.

And you already used the if to check the tuple of opposite direction, so it won't create duplicate edges.



For example, when (a, b) hits the if checking, it will check (b,a) exists in the set or not, if exists, then pass. If not, add (a, b) in the set, if (a, b) exists, the set won't change since only one instace will be in the set.

And later when looped to (b, a), since (a, b) already in the set, the if will be false and passed.

So by this way, the set is safe, free of duplicate edges.






share|improve this answer





















  • 1





    I still don't get the point of the check because the set will only contain one instance of any value

    – roganjosh
    Jan 13 at 13:17











  • So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

    – roganjosh
    Jan 13 at 13:21













  • Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

    – roganjosh
    Jan 13 at 13:26








  • 1





    @FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

    – roganjosh
    Jan 13 at 13:55






  • 1





    oh is that so haha... thanks, then! :D

    – Francis Magnusson
    Jan 13 at 14:04
















3














I think it just needs a little change, try this:



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if ((key, adj_node) not in edges) and ((adj_node, key) not in edges):
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Update:

So it is an Undigraph.

And it's me overcomplicated this.

And your way is actually better than my checking both ways.



The reason your code succeed, is that set will only contain one instance of any value.

So each time do the add, if there's already same tuple exists, it simply won't change the set.

And you already used the if to check the tuple of opposite direction, so it won't create duplicate edges.



For example, when (a, b) hits the if checking, it will check (b,a) exists in the set or not, if exists, then pass. If not, add (a, b) in the set, if (a, b) exists, the set won't change since only one instace will be in the set.

And later when looped to (b, a), since (a, b) already in the set, the if will be false and passed.

So by this way, the set is safe, free of duplicate edges.






share|improve this answer





















  • 1





    I still don't get the point of the check because the set will only contain one instance of any value

    – roganjosh
    Jan 13 at 13:17











  • So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

    – roganjosh
    Jan 13 at 13:21













  • Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

    – roganjosh
    Jan 13 at 13:26








  • 1





    @FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

    – roganjosh
    Jan 13 at 13:55






  • 1





    oh is that so haha... thanks, then! :D

    – Francis Magnusson
    Jan 13 at 14:04














3












3








3







I think it just needs a little change, try this:



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if ((key, adj_node) not in edges) and ((adj_node, key) not in edges):
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Update:

So it is an Undigraph.

And it's me overcomplicated this.

And your way is actually better than my checking both ways.



The reason your code succeed, is that set will only contain one instance of any value.

So each time do the add, if there's already same tuple exists, it simply won't change the set.

And you already used the if to check the tuple of opposite direction, so it won't create duplicate edges.



For example, when (a, b) hits the if checking, it will check (b,a) exists in the set or not, if exists, then pass. If not, add (a, b) in the set, if (a, b) exists, the set won't change since only one instace will be in the set.

And later when looped to (b, a), since (a, b) already in the set, the if will be false and passed.

So by this way, the set is safe, free of duplicate edges.






share|improve this answer















I think it just needs a little change, try this:



def get_edges(self):
edges = set()
for key in self.graph:
for adj_node in self.graph[key]:
if ((key, adj_node) not in edges) and ((adj_node, key) not in edges):
edge = (key, adj_node)
edges.add(edge)
else:
pass
return edges


Update:

So it is an Undigraph.

And it's me overcomplicated this.

And your way is actually better than my checking both ways.



The reason your code succeed, is that set will only contain one instance of any value.

So each time do the add, if there's already same tuple exists, it simply won't change the set.

And you already used the if to check the tuple of opposite direction, so it won't create duplicate edges.



For example, when (a, b) hits the if checking, it will check (b,a) exists in the set or not, if exists, then pass. If not, add (a, b) in the set, if (a, b) exists, the set won't change since only one instace will be in the set.

And later when looped to (b, a), since (a, b) already in the set, the if will be false and passed.

So by this way, the set is safe, free of duplicate edges.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 14 at 3:53

























answered Jan 13 at 13:14









TiwTiw

2,4531925




2,4531925








  • 1





    I still don't get the point of the check because the set will only contain one instance of any value

    – roganjosh
    Jan 13 at 13:17











  • So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

    – roganjosh
    Jan 13 at 13:21













  • Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

    – roganjosh
    Jan 13 at 13:26








  • 1





    @FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

    – roganjosh
    Jan 13 at 13:55






  • 1





    oh is that so haha... thanks, then! :D

    – Francis Magnusson
    Jan 13 at 14:04














  • 1





    I still don't get the point of the check because the set will only contain one instance of any value

    – roganjosh
    Jan 13 at 13:17











  • So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

    – roganjosh
    Jan 13 at 13:21













  • Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

    – roganjosh
    Jan 13 at 13:26








  • 1





    @FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

    – roganjosh
    Jan 13 at 13:55






  • 1





    oh is that so haha... thanks, then! :D

    – Francis Magnusson
    Jan 13 at 14:04








1




1





I still don't get the point of the check because the set will only contain one instance of any value

– roganjosh
Jan 13 at 13:17





I still don't get the point of the check because the set will only contain one instance of any value

– roganjosh
Jan 13 at 13:17













So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

– roganjosh
Jan 13 at 13:21







So just add them to the set. What is the if check preventing? "If it's not in the set, add it" is no different, from what I can see, to "add it to the set and let the set mechanics mean I only have one instance"

– roganjosh
Jan 13 at 13:21















Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

– roganjosh
Jan 13 at 13:26







Or, just add both? The question has 3 upvotes so I'm guessing it's me that's missing something here, but I genuinely don't see it :)

– roganjosh
Jan 13 at 13:26






1




1





@FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

– roganjosh
Jan 13 at 13:55





@FrancisMagnusson that's not something to be sorry about. A big, and interesting, part of SO is helping people align their thinking :)

– roganjosh
Jan 13 at 13:55




1




1





oh is that so haha... thanks, then! :D

– Francis Magnusson
Jan 13 at 14:04





oh is that so haha... thanks, then! :D

– Francis Magnusson
Jan 13 at 14:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54169158%2fwhy-does-my-code-take-different-values-when-i-switch-the-order-in-a-set-knowing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mario Kart Wii

What does “Dominus providebit” mean?

Antonio Litta Visconti Arese