Create adjacency matrix from edge listAssign value to an individual cell in a two dimensional python array2d array of zerosHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?How to randomly select an item from a list?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?
Why can't we use uninitialized local variable to access static content of its type?
Microservices and Stored Procedures
Simulate a 1D Game-of-Life-ish Model
How do you determine which representation of a function to use for Newton's method?
Persuading players to be less attached to a pre-session 0 character concept
Is the name of an interval between two notes unique and absolute?
Decimal “XOR” operator
What can I actually do with a high credit score?
Are lay articles good enough to be the main source of information for PhD research?
Intuitive methods for representation of Cartesian Coordinates in terms of Spherical Coordinates as basis
Is it safe to put a microwave in a walk-in closet?
Unable to see packet drops on tunnels
Algorithm for competing cells of 0s and 1s
Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?
Incorrect syntax near '+' in stored procedure sql server
Minimize taxes now that I earn more
What was the deeper meaning of Hermione wanting the cloak?
Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?
What are the end bytes of *.docx file format
Is it possible that the shadow of The Moon is a single dot during solar eclipse?
What do you call the battery slot's ends?
How to ask a man to not take up more than one seat on public transport while avoiding conflict?
Fun time! Guess what I am!
Dear Fellow PSE Users,
Create adjacency matrix from edge list
Assign value to an individual cell in a two dimensional python array2d array of zerosHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?How to randomly select an item from a list?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Hello i'm trying to code the generation of an adjacency matrix from an edge list but i can't get my code to work and i don't understand why
i've tried inversing the indexes and running it step by step
graph1=[[0,2,3,4],[1,2,4],[0,2,3,4],[1,2,3,4],[0,2,4]]
def Adjacency(graph):
index = 0 #Index of the sublist
matrix = [[0]*len(graph)]*len(graph)
print(matrix)
for sublist in graph:
for value in sublist:
print(value)
matrice[index][value] = 1
index+=1
print(matrix)
Adjacence(graphe1)
the expected output should be
[[1 0 1 1 1]
[0 1 1 0 1]
[1 0 1 1 1]
[0 1 1 1 1]
[1 0 1 0 1]]
but instead i got
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
i'm pratically sure that i forgot a small detail but i can't figure it out.
i'd be glad if somebody could me.
python graph
add a comment
|
Hello i'm trying to code the generation of an adjacency matrix from an edge list but i can't get my code to work and i don't understand why
i've tried inversing the indexes and running it step by step
graph1=[[0,2,3,4],[1,2,4],[0,2,3,4],[1,2,3,4],[0,2,4]]
def Adjacency(graph):
index = 0 #Index of the sublist
matrix = [[0]*len(graph)]*len(graph)
print(matrix)
for sublist in graph:
for value in sublist:
print(value)
matrice[index][value] = 1
index+=1
print(matrix)
Adjacence(graphe1)
the expected output should be
[[1 0 1 1 1]
[0 1 1 0 1]
[1 0 1 1 1]
[0 1 1 1 1]
[1 0 1 0 1]]
but instead i got
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
i'm pratically sure that i forgot a small detail but i can't figure it out.
i'd be glad if somebody could me.
python graph
add a comment
|
Hello i'm trying to code the generation of an adjacency matrix from an edge list but i can't get my code to work and i don't understand why
i've tried inversing the indexes and running it step by step
graph1=[[0,2,3,4],[1,2,4],[0,2,3,4],[1,2,3,4],[0,2,4]]
def Adjacency(graph):
index = 0 #Index of the sublist
matrix = [[0]*len(graph)]*len(graph)
print(matrix)
for sublist in graph:
for value in sublist:
print(value)
matrice[index][value] = 1
index+=1
print(matrix)
Adjacence(graphe1)
the expected output should be
[[1 0 1 1 1]
[0 1 1 0 1]
[1 0 1 1 1]
[0 1 1 1 1]
[1 0 1 0 1]]
but instead i got
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
i'm pratically sure that i forgot a small detail but i can't figure it out.
i'd be glad if somebody could me.
python graph
Hello i'm trying to code the generation of an adjacency matrix from an edge list but i can't get my code to work and i don't understand why
i've tried inversing the indexes and running it step by step
graph1=[[0,2,3,4],[1,2,4],[0,2,3,4],[1,2,3,4],[0,2,4]]
def Adjacency(graph):
index = 0 #Index of the sublist
matrix = [[0]*len(graph)]*len(graph)
print(matrix)
for sublist in graph:
for value in sublist:
print(value)
matrice[index][value] = 1
index+=1
print(matrix)
Adjacence(graphe1)
the expected output should be
[[1 0 1 1 1]
[0 1 1 0 1]
[1 0 1 1 1]
[0 1 1 1 1]
[1 0 1 0 1]]
but instead i got
[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]
i'm pratically sure that i forgot a small detail but i can't figure it out.
i'd be glad if somebody could me.
python graph
python graph
asked Mar 28 at 14:15
SliteSlite
391 silver badge9 bronze badges
391 silver badge9 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
you need to change the line:
matrix = [[0]*len(graph)]*len(graph)
to:
matrix = [[0]*len(graph) for i in range(len(graph))]
This is becasue when you make an array the way you did it stores them differently and they can edit multiple values at once. Try reading this question or this one
add a comment
|
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/4.0/"u003ecc by-sa 4.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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399815%2fcreate-adjacency-matrix-from-edge-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you need to change the line:
matrix = [[0]*len(graph)]*len(graph)
to:
matrix = [[0]*len(graph) for i in range(len(graph))]
This is becasue when you make an array the way you did it stores them differently and they can edit multiple values at once. Try reading this question or this one
add a comment
|
you need to change the line:
matrix = [[0]*len(graph)]*len(graph)
to:
matrix = [[0]*len(graph) for i in range(len(graph))]
This is becasue when you make an array the way you did it stores them differently and they can edit multiple values at once. Try reading this question or this one
add a comment
|
you need to change the line:
matrix = [[0]*len(graph)]*len(graph)
to:
matrix = [[0]*len(graph) for i in range(len(graph))]
This is becasue when you make an array the way you did it stores them differently and they can edit multiple values at once. Try reading this question or this one
you need to change the line:
matrix = [[0]*len(graph)]*len(graph)
to:
matrix = [[0]*len(graph) for i in range(len(graph))]
This is becasue when you make an array the way you did it stores them differently and they can edit multiple values at once. Try reading this question or this one
answered Mar 28 at 14:25
BuzzBuzz
1,57218 silver badges24 bronze badges
1,57218 silver badges24 bronze badges
add a comment
|
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399815%2fcreate-adjacency-matrix-from-edge-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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