Finding out if multiple values in a list of lists are the same value











up vote
0
down vote

favorite












This is my problem:



I have a list of lists (adjoining) that is setup of x and y coordinates.



I'm trying to create a function that can figure out which way to go, that should return a single element from that list.



That return value will be the direction the character moves.



(Example: adjoining = [[1, 0], [3, 2], [1, 1]]. Return returns the smallest both x and y coordinate which is [1, 0].)



My function should return the value with the lowest x coordinate. If there are multiple x coordinates that have the same low value, the one with the lowest y coordinate should be chosen.



How can I create a function that can figure out which x and y value is lowest?










share|improve this question




















  • 3




    You could use min
    – Daniel Mesejo
    Nov 10 at 17:29

















up vote
0
down vote

favorite












This is my problem:



I have a list of lists (adjoining) that is setup of x and y coordinates.



I'm trying to create a function that can figure out which way to go, that should return a single element from that list.



That return value will be the direction the character moves.



(Example: adjoining = [[1, 0], [3, 2], [1, 1]]. Return returns the smallest both x and y coordinate which is [1, 0].)



My function should return the value with the lowest x coordinate. If there are multiple x coordinates that have the same low value, the one with the lowest y coordinate should be chosen.



How can I create a function that can figure out which x and y value is lowest?










share|improve this question




















  • 3




    You could use min
    – Daniel Mesejo
    Nov 10 at 17:29















up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is my problem:



I have a list of lists (adjoining) that is setup of x and y coordinates.



I'm trying to create a function that can figure out which way to go, that should return a single element from that list.



That return value will be the direction the character moves.



(Example: adjoining = [[1, 0], [3, 2], [1, 1]]. Return returns the smallest both x and y coordinate which is [1, 0].)



My function should return the value with the lowest x coordinate. If there are multiple x coordinates that have the same low value, the one with the lowest y coordinate should be chosen.



How can I create a function that can figure out which x and y value is lowest?










share|improve this question















This is my problem:



I have a list of lists (adjoining) that is setup of x and y coordinates.



I'm trying to create a function that can figure out which way to go, that should return a single element from that list.



That return value will be the direction the character moves.



(Example: adjoining = [[1, 0], [3, 2], [1, 1]]. Return returns the smallest both x and y coordinate which is [1, 0].)



My function should return the value with the lowest x coordinate. If there are multiple x coordinates that have the same low value, the one with the lowest y coordinate should be chosen.



How can I create a function that can figure out which x and y value is lowest?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 17:30









Vasilis G.

2,6042621




2,6042621










asked Nov 10 at 17:26









Sigurd Setså

11




11








  • 3




    You could use min
    – Daniel Mesejo
    Nov 10 at 17:29
















  • 3




    You could use min
    – Daniel Mesejo
    Nov 10 at 17:29










3




3




You could use min
– Daniel Mesejo
Nov 10 at 17:29






You could use min
– Daniel Mesejo
Nov 10 at 17:29














3 Answers
3






active

oldest

votes

















up vote
0
down vote













Just use min() like so:



>>> adjoining = [[3,2],[1,1],[1,0]]
>>> min(adjoining)
[1, 0]





share|improve this answer

















  • 1




    @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
    – jpp
    Nov 10 at 18:15








  • 1




    @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
    – Sigurd Setså
    Nov 10 at 18:24


















up vote
0
down vote













You can do this:



minimum = min(adjoining, key=lambda elem: (elem[0], elem[1]))


Result:



[1, 0]


Use the key parameter to define the fields which will affect the sorting process. First, you will get the minimum element based on x value and if there are many of them, they will be sorted again based on their y value.



An alternative is to use the itemgetter function from operator module:



import operator

adjoining = [[1, 0], [3, 2], [1, 1]]

minimum = min(adjoining, key=operator.itemgetter(0,1))





share|improve this answer






























    up vote
    0
    down vote













    adjoining.sort(key= lambda x:x[0])
    mins = [i for i in adjoining if i[0] == adjoining[0][0]]
    if len(mins)>1:
    mins.sort(key= lambda x:x[1])
    min = [i for i in mins if i[1] == mins[0][1]]
    else:
    min = mins
    answer = min[0]


    I used sort with the key attribute. This allows you to define the criteria by which to sort the list.
    The rest of the code is base on list comprehension.



    [ item for item in a list_of_items if item == wanted_item]


    This filters list_of_item based of some chosen criteria. Just need to make sure what follows if can be evaluated True or False.






    share|improve this answer























    • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
      – hellow
      Nov 11 at 7:21











    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',
    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%2f53241552%2ffinding-out-if-multiple-values-in-a-list-of-lists-are-the-same-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Just use min() like so:



    >>> adjoining = [[3,2],[1,1],[1,0]]
    >>> min(adjoining)
    [1, 0]





    share|improve this answer

















    • 1




      @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
      – jpp
      Nov 10 at 18:15








    • 1




      @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
      – Sigurd Setså
      Nov 10 at 18:24















    up vote
    0
    down vote













    Just use min() like so:



    >>> adjoining = [[3,2],[1,1],[1,0]]
    >>> min(adjoining)
    [1, 0]





    share|improve this answer

















    • 1




      @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
      – jpp
      Nov 10 at 18:15








    • 1




      @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
      – Sigurd Setså
      Nov 10 at 18:24













    up vote
    0
    down vote










    up vote
    0
    down vote









    Just use min() like so:



    >>> adjoining = [[3,2],[1,1],[1,0]]
    >>> min(adjoining)
    [1, 0]





    share|improve this answer












    Just use min() like so:



    >>> adjoining = [[3,2],[1,1],[1,0]]
    >>> min(adjoining)
    [1, 0]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 17:34









    Red Cricket

    3,90983381




    3,90983381








    • 1




      @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
      – jpp
      Nov 10 at 18:15








    • 1




      @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
      – Sigurd Setså
      Nov 10 at 18:24














    • 1




      @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
      – jpp
      Nov 10 at 18:15








    • 1




      @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
      – Sigurd Setså
      Nov 10 at 18:24








    1




    1




    @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
    – jpp
    Nov 10 at 18:15






    @SigurdSetså, Can you explain your logic more clearly as an edit to your question. Your question seems to imply [0, 1] comes before [1, 0], i.e. My function should return the value with the lowest x coordinate. Concrete examples (rather than descriptions) often help.
    – jpp
    Nov 10 at 18:15






    1




    1




    @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
    – Sigurd Setså
    Nov 10 at 18:24




    @jpp I realised that my comment got it backwards. [0, 1] would be correct. I meant to say it the other way
    – Sigurd Setså
    Nov 10 at 18:24












    up vote
    0
    down vote













    You can do this:



    minimum = min(adjoining, key=lambda elem: (elem[0], elem[1]))


    Result:



    [1, 0]


    Use the key parameter to define the fields which will affect the sorting process. First, you will get the minimum element based on x value and if there are many of them, they will be sorted again based on their y value.



    An alternative is to use the itemgetter function from operator module:



    import operator

    adjoining = [[1, 0], [3, 2], [1, 1]]

    minimum = min(adjoining, key=operator.itemgetter(0,1))





    share|improve this answer



























      up vote
      0
      down vote













      You can do this:



      minimum = min(adjoining, key=lambda elem: (elem[0], elem[1]))


      Result:



      [1, 0]


      Use the key parameter to define the fields which will affect the sorting process. First, you will get the minimum element based on x value and if there are many of them, they will be sorted again based on their y value.



      An alternative is to use the itemgetter function from operator module:



      import operator

      adjoining = [[1, 0], [3, 2], [1, 1]]

      minimum = min(adjoining, key=operator.itemgetter(0,1))





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can do this:



        minimum = min(adjoining, key=lambda elem: (elem[0], elem[1]))


        Result:



        [1, 0]


        Use the key parameter to define the fields which will affect the sorting process. First, you will get the minimum element based on x value and if there are many of them, they will be sorted again based on their y value.



        An alternative is to use the itemgetter function from operator module:



        import operator

        adjoining = [[1, 0], [3, 2], [1, 1]]

        minimum = min(adjoining, key=operator.itemgetter(0,1))





        share|improve this answer














        You can do this:



        minimum = min(adjoining, key=lambda elem: (elem[0], elem[1]))


        Result:



        [1, 0]


        Use the key parameter to define the fields which will affect the sorting process. First, you will get the minimum element based on x value and if there are many of them, they will be sorted again based on their y value.



        An alternative is to use the itemgetter function from operator module:



        import operator

        adjoining = [[1, 0], [3, 2], [1, 1]]

        minimum = min(adjoining, key=operator.itemgetter(0,1))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 17:37

























        answered Nov 10 at 17:32









        Vasilis G.

        2,6042621




        2,6042621






















            up vote
            0
            down vote













            adjoining.sort(key= lambda x:x[0])
            mins = [i for i in adjoining if i[0] == adjoining[0][0]]
            if len(mins)>1:
            mins.sort(key= lambda x:x[1])
            min = [i for i in mins if i[1] == mins[0][1]]
            else:
            min = mins
            answer = min[0]


            I used sort with the key attribute. This allows you to define the criteria by which to sort the list.
            The rest of the code is base on list comprehension.



            [ item for item in a list_of_items if item == wanted_item]


            This filters list_of_item based of some chosen criteria. Just need to make sure what follows if can be evaluated True or False.






            share|improve this answer























            • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
              – hellow
              Nov 11 at 7:21















            up vote
            0
            down vote













            adjoining.sort(key= lambda x:x[0])
            mins = [i for i in adjoining if i[0] == adjoining[0][0]]
            if len(mins)>1:
            mins.sort(key= lambda x:x[1])
            min = [i for i in mins if i[1] == mins[0][1]]
            else:
            min = mins
            answer = min[0]


            I used sort with the key attribute. This allows you to define the criteria by which to sort the list.
            The rest of the code is base on list comprehension.



            [ item for item in a list_of_items if item == wanted_item]


            This filters list_of_item based of some chosen criteria. Just need to make sure what follows if can be evaluated True or False.






            share|improve this answer























            • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
              – hellow
              Nov 11 at 7:21













            up vote
            0
            down vote










            up vote
            0
            down vote









            adjoining.sort(key= lambda x:x[0])
            mins = [i for i in adjoining if i[0] == adjoining[0][0]]
            if len(mins)>1:
            mins.sort(key= lambda x:x[1])
            min = [i for i in mins if i[1] == mins[0][1]]
            else:
            min = mins
            answer = min[0]


            I used sort with the key attribute. This allows you to define the criteria by which to sort the list.
            The rest of the code is base on list comprehension.



            [ item for item in a list_of_items if item == wanted_item]


            This filters list_of_item based of some chosen criteria. Just need to make sure what follows if can be evaluated True or False.






            share|improve this answer














            adjoining.sort(key= lambda x:x[0])
            mins = [i for i in adjoining if i[0] == adjoining[0][0]]
            if len(mins)>1:
            mins.sort(key= lambda x:x[1])
            min = [i for i in mins if i[1] == mins[0][1]]
            else:
            min = mins
            answer = min[0]


            I used sort with the key attribute. This allows you to define the criteria by which to sort the list.
            The rest of the code is base on list comprehension.



            [ item for item in a list_of_items if item == wanted_item]


            This filters list_of_item based of some chosen criteria. Just need to make sure what follows if can be evaluated True or False.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 15:25

























            answered Nov 11 at 1:29









            qwerty asdf

            92




            92












            • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
              – hellow
              Nov 11 at 7:21


















            • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
              – hellow
              Nov 11 at 7:21
















            While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
            – hellow
            Nov 11 at 7:21




            While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
            – hellow
            Nov 11 at 7:21


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241552%2ffinding-out-if-multiple-values-in-a-list-of-lists-are-the-same-value%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma