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?
python python-3.x
add a comment |
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?
python python-3.x
3
You could use min
– Daniel Mesejo
Nov 10 at 17:29
add a comment |
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?
python python-3.x
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
python python-3.x
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
add a comment |
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
add a comment |
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]
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
add a comment |
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))
add a comment |
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
.
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
add a comment |
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]
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
add a comment |
up vote
0
down vote
Just use min() like so:
>>> adjoining = [[3,2],[1,1],[1,0]]
>>> min(adjoining)
[1, 0]
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
add a comment |
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]
Just use min() like so:
>>> adjoining = [[3,2],[1,1],[1,0]]
>>> min(adjoining)
[1, 0]
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
add a comment |
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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
edited Nov 10 at 17:37
answered Nov 10 at 17:32
Vasilis G.
2,6042621
2,6042621
add a comment |
add a comment |
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
.
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
add a comment |
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
.
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
add a comment |
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
.
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
.
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
add a comment |
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
add a comment |
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%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
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
3
You could use min
– Daniel Mesejo
Nov 10 at 17:29