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

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

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