PHP- Get each value of an associative array in a foreach loop, then apply a function and echo the result
Tag : php , By : Alex Bartzas
Date : March 29 2020, 07:55 AM
will be helpful for those in need In this context, foreach($list_links as $link), $link is not the url, it is an array of urls. Try a nested foreach instead: foreach ($list_link as $link_array) {
foreach ($link_array as $link) {
DoWork($link, $db_connect);
}
}
foreach ($list_link as $key=>$link_array) {
foreach ($link_array as $link) {
DoWork($link, $db_connect);
echo 'this url is from the '.$key.' list';
}
}
$list_links = array(
0 => array
(
"http://test.com/1/",
"http://test.com/2/"
),
1 => array
(
"http://test.com/3/",
"http://test.com/4/"
)
);
$array = array ('key' => 'value')
$list_links = array(
array("http://test.com/1/", "http://test.com/2/"),
array("http://test.com/3/", "http://test.com/4/")
);
|
Apply Function foreach Pixel in Numpy Array
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I have a function like: , Code: import math
import numpy as np
np.random.seed(1)
# FAKE-DATA
img = np.random.randint(0,255,size=(4,4,3))
print(img)
# LOOP APPROACH
def calcChromaFromPixel(red, green, blue):
r = int(red)
g = int(green)
b = int(blue)
return math.sqrt(math.pow(r - g, 2) +
math.pow(r - b, 2) +
math.pow(g - b, 2))
bla = np.zeros(img.shape[:2])
for a in range(img.shape[0]):
for b in range(img.shape[1]):
bla[a,b] = calcChromaFromPixel(*img[a,b])
print('loop')
print(bla)
# VECTORIZED APPROACH
print('vectorized')
res = np.linalg.norm(np.stack(
(img[:,:,0] - img[:,:,1],
img[:,:,0] - img[:,:,2],
img[:,:,1] - img[:,:,2])), axis=0)
print(res)
[[[ 37 235 140]
[ 72 137 203]
[133 79 192]
[144 129 204]]
[[ 71 237 252]
[134 25 178]
[ 20 254 101]
[146 212 139]]
[[252 234 156]
[157 142 50]
[ 68 215 215]
[233 241 247]]
[[222 96 86]
[141 233 137]
[ 7 63 61]
[ 22 57 1]]]
loop
[[ 242.56545508 160.44313634 138.44132331 97.21111048]
[ 246.05283985 192.94040531 291.07730932 98.66103588]
[ 124.99599994 141.90842117 207.88939367 17.20465053]
[ 185.66636744 133.02631319 77.82030583 69.29646456]]
vectorized
[[ 242.56545508 160.44313634 138.44132331 97.21111048]
[ 246.05283985 192.94040531 291.07730932 98.66103588]
[ 124.99599994 141.90842117 207.88939367 17.20465053]
[ 185.66636744 133.02631319 77.82030583 69.29646456]]
|
How can I apply the Result.withDefault function to a function that produces a Result
Tag : json , By : Adam May
Date : March 29 2020, 07:55 AM
should help you out Given that you have a function which returns an empty Item, for example emptyItem, you just need to perform the steps you've described: Wrap the output of decodeItem with a Result.withDefault The empty Item would be the first argument to Result.withDefault decodeItem : D.Value -> Item
decodeItem value =
Result.withDefault emptyItem (D.decodeValue itemDecoder value)
emptyItem : Item
emptyItem = Item "" "" 0 ""
|
Apply forEach function for array like object
Date : March 29 2020, 07:55 AM
it fixes the issue files is of type FileList and it doesn't have a forEach extension method. You could spread it within an [] to convert it to an array of files: [...files].forEach(file => { /* do something */ })
const filesToStore = [...files]
|
How can I iterate through all rows of a dataframe to apply a look up function to a string value and apply the result to
Tag : python , By : Alan Little
Date : March 29 2020, 07:55 AM
seems to work fine First off, apply and iterrows are slow, so try not to use them, ever. What I usually do in this situation is to create a pair of forward and backward dicts: forward = {'east': east,
'west': west,
'south': south}
backward = {x:k for k,v in forward.items() for x in v}
df1['Region'] = (df1['State'].map(backward)
.fillna(df1['City'].map(backward))
.fillna('other')
)
Last Name First Name Code Deparment City State Region
0 SMITH TOM 12 Research NEW YORK NY east
1 JONES DICK 34 Management BOSTON MA east
2 WILSON HARRY 56 Maintenance SAN FRANCISCO CA west
3 DOYLE MICHAEL 78 Marketing DALLAS TX south
4 ANDERSON KEVIN 90 IT DETROIT MI other
|