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;








1















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.










share|improve this question






























    1















    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.










    share|improve this question


























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 14:15









      SliteSlite

      391 silver badge9 bronze badges




      391 silver badge9 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          2
















          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






          share|improve this answer
























            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
            );



            );














            draft saved

            draft discarded
















            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









            2
















            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






            share|improve this answer





























              2
















              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






              share|improve this answer



























                2














                2










                2









                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






                share|improve this answer













                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







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 14:25









                BuzzBuzz

                1,57218 silver badges24 bronze badges




                1,57218 silver badges24 bronze badges





















                    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.




















                    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%2f55399815%2fcreate-adjacency-matrix-from-edge-list%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

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

                    해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh