Haskell Iterate over 2d list, filter, output 1d list
Date : March 29 2020, 07:55 AM
wish of those help I thought I was smooth sailing in my Haskell studies, until... , This is a place where list comprehensions shine. blackBox tiles =
[Coord x y -- generate a Coord pair
| (y, row) <- enumerate tiles -- for each row with its coordinate
, (x, tile) <- enumerate row -- for each tile in the row (with coordinate)
, tile == 1] -- if the tile is 1
blackBox tiles = do
(y, row) <- enumerate tiles -- for each row with its coordinate
(x, tile) <- enumerate row -- for each tile in the row (with coordinate)
guard $ tile == 1 -- as long as the tile is 1
return $ Coord x y -- return a coord pair
def black_box(tiles):
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
if tile == 1:
yield Coord(x, y)
enumerate = zip [0..]
|
Pandas -- how to iterate through a list of dates which filter a DataFrame
Date : March 29 2020, 07:55 AM
this one helps. I'm not 100% sure I understand what you want but I think you want to a sub dataframe (taken from the number dataframe) for each date in datelist. So in your example you want 7 dataframes created? If so this is what I would do: print df
date group number
0 2013-02-01 group1 -0.098765
1 2013-02-02 group2 0.519878
2 2013-02-03 group1 -0.098765
3 2013-02-04 group3 1.960784
4 2013-02-05 group3 2.859412
5 2013-02-06 group2 1.960784
6 2013-02-07 group1 -0.696594
parse = lambda x: datetime(int(x[0]),int(x[1]),int(x[2]))
datelist['end'] = datelist['date'].str.split(',').apply(parse)
print datelist
date end
0 2013, 2,3 2013-02-03
1 2013, 2,6 2013-02-06
2 2013, 3,6 2013-03-06
3 2013, 3,8 2013-03-08
pieces = []
for idx,rows in datelist[['end']].iterrows():
x = df[df['date'] <= rows['end']]
x['end'] = rows['end']
pieces.append(x)
print pd.concat(pieces,ignore_index=True)
date group number end
0 2013-02-01 group1 -0.098765 2013-02-03
1 2013-02-02 group2 0.519878 2013-02-03
2 2013-02-03 group1 -0.098765 2013-02-03
3 2013-02-01 group1 -0.098765 2013-02-06
4 2013-02-02 group2 0.519878 2013-02-06
5 2013-02-03 group1 -0.098765 2013-02-06
6 2013-02-04 group3 1.960784 2013-02-06
7 2013-02-05 group3 2.859412 2013-02-06
8 2013-02-06 group2 1.960784 2013-02-06
9 2013-02-01 group1 -0.098765 2013-03-06
10 2013-02-02 group2 0.519878 2013-03-06
11 2013-02-03 group1 -0.098765 2013-03-06
12 2013-02-04 group3 1.960784 2013-03-06
13 2013-02-05 group3 2.859412 2013-03-06
14 2013-02-06 group2 1.960784 2013-03-06
15 2013-02-07 group1 -0.696594 2013-03-06
16 2013-02-01 group1 -0.098765 2013-03-08
17 2013-02-02 group2 0.519878 2013-03-08
18 2013-02-03 group1 -0.098765 2013-03-08
19 2013-02-04 group3 1.960784 2013-03-08
20 2013-02-05 group3 2.859412 2013-03-08
21 2013-02-06 group2 1.960784 2013-03-08
22 2013-02-07 group1 -0.696594 2013-03-08
|
Filter / Iterate a List by values inside List C#
Date : March 29 2020, 07:55 AM
wish of those help In that case you will have all students without parsebar rolnumber on the top, if you do want to have them in the bottom, change Int32.MinValue to Int32.MaxValue var sorted = obj.OrderBy(x =>
{
int res;
bool parsed = Int32.TryParse(x.rolenumber, out res);
return parsed? res: Int32.MinValue;
});
|
Can't iterate list after filter
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The problem is not that filter is not iterable, but rather the fact that you do iterable unpacking in the head of the for loop: for index, item in arr:
# do something
for index, item in enumerate(arr):
# do something
for item in arr:
# do something
|
Can't iterate and create a list from Django filter Query
Tag : python , By : Peter Leung
Date : March 29 2020, 07:55 AM
I wish this helpful for you In my project I have a Transaction model with a one to many relationship with the Coin model. Each transaction has a buy_price and unit_number (number of units). With the nested for loop in views.py below I am expecting to generate a list of multiple buy_prices within each coin dictionary. , I think the problem is here: for t in transactions:
buy_prices = [] # <-- here
buy_prices.append(t.buy_price)
print(buy_prices)
coin_dict['transactions'] = {
'buy_prices': buy_prices
}
buy_prices = []
for t in transactions:
buy_prices.append(t.buy_price)
coin_dict['transactions'] = {
'buy_prices': buy_prices
}
coin_dict['transactions'] = { 'buy_prices': list(transactions.values_list('buy_prices', flat=True))}
# No need for Transaction for loop
|