Related
This question already has answers here:
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 1 year ago.
So I have this list comprehension line I need to convert to a for loop in python.
pred and exp are lists that have many different values in them. They are part of a data set of numbers and with machine learning the code is supposed to guess the number(but that is just background)
correct = [(p, e) for (p, e) in zip(pred, exp) if p != e]
Output: of correct
[(8, 2),
(7, 2),
(9, 8),
(4, 0),
(2, 8),
(9, 7),
(8, 9),
(8, 9),
(2, 8),
(1, 8),
(4, 9),
(9, 4),
(3, 8),
(1, 8),
(3, 9),
(9, 5),
(9, 5),
(9, 7),
(3, 8),
(1, 8),
(9, 7),
(9, 7),
(2, 8),
(9, 7),
(3, 9),
(8, 2),
(1, 8),
(7, 3),
(1, 8),
(3, 8),
(9, 7),
(5, 9),
(3, 8),
(2, 8),
(8, 1),
(9, 1),
(1, 2),
(9, 5),
(8, 2),
(4, 5),
(9, 5),
(4, 6),
(2, 3),
(1, 8),
(8, 2),
(9, 3),
(8, 2),
(5, 3),
(9, 5),
(1, 8),
(1, 8),
(3, 9),
(8, 2),
(1, 9),
(1, 9),
(8, 1),
(3, 8),
(2, 7)]
I tried this
correct = []
for (p, e) in zip(predicted, expected):
if p != e:
correct.append(p)
correct.append(e)
print(correct)
But the output looks like this
[5, 3, 6, 8, 1, 4, 7, 9, 7, 3, 7, 8, 7, 9, 7, 4, 7, 9, 9, 8, 9, 8, 6, 8, 8, 2, 8, 2, 3, 8, 1, 8, 1, 2, 1, 8, 1, 8, 9, 5, 7, 9, 7, 9, 1, 8, 9, 5, 1, 8, 8, 2, 7, 2, 8, 9, 3, 8, 9, 5, 8, 9, 6, 3, 7, 9, 8, 6, 0, 2, 2, 8, 9, 5, 9, 5, 7, 4, 9, 5, 1, 8, 1, 8, 5, 9, 1, 2, 7, 4, 8, 2]
Any help would be appreciated, thanks
You want a list of tuples, but you get a list of numbers. The reason is that you are appending the numbers just as numbers, not as tuples.
Instead, do
if p != e:
correct.append((p,e))
Inside the for loop you're appending p and e to the list as single elements, despite the fact that on each iteration you get them inside a tuple.
To fix it just append them to the list as a tuple element.
if p != e:
correct.append((p,e))
I have an array as below.
testgrid = [
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6],
[9, 1, 2, 4, 3, 8, 7, 5, 6]]
On passing the testgrid into the following function it should return all indexes that do not contain a value of zero currently it returns (0, 0). I would expect all indexes to be returned on this grid. It seems to be checking the indexes rather than the value stored at that index. I am a noob so probably am missing something obvious here.
def not_empty_location(grid):
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
return (i, j)
return None
Use yield instead of return:
def not_empty_location(grid):
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
yield (i, j)
return None
This would return all the values as a generator which can then simply be converted into a list.
Output:
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]
try by storing the index tuples in a list and return
def not_empty_location(grid):
result = [] # list to store the index
# checks if current location is empty and assign location not empty
for i in range(9):
for j in range(9):
if grid[i][j] != 0:
result.append( (i, j) ) # In your code you are returning at your very first success of getting != 0
return result
I have a list as follows:
list1 = [((1, 4, 2),(0, 5, 8), (3, 6, 7)), ((1, 5, 2),(0, 4, 8), (3, 6, 7))]
I want my dictionary to have each element of list as key and a tuple value as follows:
dict[((1, 4, 2),(0, 5, 8), (3, 6, 7))] = (1,'None')
How can I do this?
IIUC I think you need:
d = {l:(1,'None') for l in list1}
d
{((1, 4, 2), (0, 5, 8), (3, 6, 7)): (1, 'None'),
((1, 5, 2), (0, 4, 8), (3, 6, 7)): (1, 'None')}
For 0th element:
d = {l:(1,'None') for l in list_val[0]}
d
{(1, 4, 2): (1, 'None'), (0, 5, 8): (1, 'None'), (3, 6, 7): (1, 'None')}
or
d = {v2:(1,'None') for v1 in list1 for v2 in v1}
d
{(1, 4, 2): (1, 'None'),
(0, 5, 8): (1, 'None'),
(3, 6, 7): (1, 'None'),
(1, 5, 2): (1, 'None'),
(0, 4, 8): (1, 'None')}
Well, it works fine. you just create a dictionary and assign key value pairs. Thas it!!
Look at my conversation with python interpreter below. ( i am using ipython! )
In [3]: d=dict()
In [4]: d['d']='4'
In [5]: d
Out[5]: {'d': '4'}
In [6]: d[(1,2,3)]=90
In [7]: d
Out[7]: {'d': '4', (1, 2, 3): 90}
In [9]: d[((1, 4, 2),(0, 5, 8), (3, 6, 7))] = (1,'None')
In [10]: d
Out[10]: {'d': '4', (1, 2, 3): 90, ((1, 4, 2), (0, 5, 8), (3, 6, 7)): (1, 'None')}
let's say I have a list of numbers:
my_list = range(0,1001,50)
I want to make every single possible combination of threes (threes = [a,b,c]) based on my_list, for example: [0,50,100], [0,50,150], ..., [0,50,1000], [0,100,150], ...
Threes don't have to be stored in a list, it's just an example of data structure.
And after that I want to put every single threes' values (a,b,c) to some sort of formula.
How could I make that? I'm kinda beginner in Python and haven't experienced with more complicated mathematical structures yet. Or is it possible to do on some kind of loop...?
Any help would be appreciated.
You can use combinations :
my_list = range(0,1000,50)
from itertools import combinations
combinations(my_list,3)
From the doc :
combinations() p, r r-length tuples, in sorted order, no repeated
elements
It creates an iterable. Converted to a list, it looks like :
[(0, 50, 100), (0, 50, 150), (0, 50, 200), (0, 50, 250), (0, 50, 300), ...
What you want is the wonderful itertools module, that has combinations() in it:
>>> import itertools
>>> list(itertools.combinations(range(10), 3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 1, 5), (0, 1, 6), (0, 1, 7), (0, 1, 8), (0
, 1, 9), (0, 2, 3), (0, 2, 4), (0, 2, 5), (0, 2, 6), (0, 2, 7), (0, 2, 8), (0, 2
, 9), (0, 3, 4), (0, 3, 5), (0, 3, 6), (0, 3, 7), (0, 3, 8), (0, 3, 9), (0, 4, 5
), (0, 4, 6), (0, 4, 7), (0, 4, 8), (0, 4, 9), (0, 5, 6), (0, 5, 7), (0, 5, 8),
(0, 5, 9), (0, 6, 7), (0, 6, 8), (0, 6, 9), (0, 7, 8), (0, 7, 9), (0, 8, 9), (1,
2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3,
4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 5), (1, 4, 6)
, (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 6), (1, 5, 7), (1, 5, 8), (1, 5, 9), (
1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7, 8), (1, 7, 9), (1, 8, 9), (2, 3, 4), (2,
3, 5), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 4, 5), (2, 4, 6), (2, 4,
7), (2, 4, 8), (2, 4, 9), (2, 5, 6), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7),
(2, 6, 8), (2, 6, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 4, 5), (3, 4, 6), (3
, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 6), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6
, 7), (3, 6, 8), (3, 6, 9), (3, 7, 8), (3, 7, 9), (3, 8, 9), (4, 5, 6), (4, 5, 7
), (4, 5, 8), (4, 5, 9), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 7, 8), (4, 7, 9),
(4, 8, 9), (5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 7, 8), (5, 7, 9), (5, 8, 9), (6,
7, 8), (6, 7, 9), (6, 8, 9), (7, 8, 9)]
Then you can feed that to your function:
def f(a,b,c):
return a * b * c
print [f(*x) for x in itertools.combinations(range(10), 3)]
Imagine I have a list of tuples in this format:
(1, 2, 3)
(1, 0, 2)
(3, 9 , 11)
(0, 2, 8)
(2, 3, 4)
(2, 4, 5)
(2, 7, 8)
....
How could I sort the list by the first element of the tuples, and then by the second? I'd like to get to this list:
(0, 2, 8)
(1, 0, 2)
(1, 2, 3)
(2, 3, 4)
(2, 4, 5)
(2, 7, 8)
(3, 9 , 11)
I was thinking to do a sort for the first element, and then go through the list, and build a hash with subarrays. I will probably overcomplicate things :), and this is why I asked for other ways of doing this sort.
Why not simply let python sort the list for you ?
my_list = [
(1, 2, 3),
(1, 0, 2),
(3, 9 , 11),
(0, 2, 8),
(2, 3, 4),
(2, 4, 5),
(2, 7, 8),
]
print sorted(my_list)
>>>[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
Python automagically does the right thing:
>>> a = [(1, 2, 3), (1, 0, 2), (3, 9, 11), (0, 2, 8), (2, 3, 4), (2, 4, 5), (2, 7, 8)]
>>> a.sort()
>>> a
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
Tuples are already sorted that way.
Try this:
#!/usr/bin/python2
l = [
(1, 2, 3),
(1, 0, 2),
(3, 9 , 11),
(0, 2, 8),
(2, 3, 4),
(2, 4, 5),
(2, 7, 8),
]
l.sort()
print l
If you don't mind sorting by all three elements, this is really trivial:
>>> l = [(1, 2, 3), (1, 0, 2), (3, 9, 11), (0, 2, 8), (2, 3, 4), (2, 4, 5), (2, 7, 8)]
>>> l.sort()
>>> l
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
>>> x = [
... (1, 2, 3),
... (1, 0, 2),
... (3, 9 , 11),
... (0, 2, 8),
... (2, 3, 4),
... (2, 4, 5),
... (2, 7, 8),
... ]
>>> x.sort()
>>> x
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]