我的目标是用与2D数组中的值模式完全匹配的1D数组中的值填充2D数组。例如:
array_a =
([[nan,nan,0],
[0,nan,0],
[nan,0,0],
[0,0,nan]])
array_b =
([0.324,0.254,0.204,
0.469,0.381,0.292,
0.550])
我想得到这个:
array_c =
([[nan,nan,0.324],
[0.254,nan,0.204],
[nan,0.469,0.381],
[0.292,0.550,nan]])
需要在array_a中填充的值的数量将与array_b中的值的数量完全匹配。主要问题是我想在整个数组中按适当的顺序使用nan值,但我不确定如何做到这一点。
答案 0 :(得分:3)
布尔索引可以很好地完成这项工作:
找到const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.set('trust proxy')
const puppeteer = require('puppeteer')
const http = require('http')
app.post('/puppeteer', async (req, res) => {
const LOGIN_URL = `https://twitter.com/login`
const ACCOUNT_URL = `https://twitter.com/settings/account`
const CREDENTIALS = req.body
// Setting up Puppeteer
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
await page.setViewport({ width: 1920, height: 926 })
// Go to login page
await page.goto(LOGIN_URL)
// Let's wait until submit button is available, without this you won't be able to login
await page.waitForSelector('.submit')
// Let's fill in the form and submit it
await page.$eval('.js-username-field', (el, payload) => el.value = payload, CREDENTIALS.username)
await page.$eval('.js-password-field', (el, payload) => el.value = payload, CREDENTIALS.password)
await page.click('button.submit')
// Wait for navigation in case there's some redirect
await page.waitForNavigation()
// We are logged in, now navigate to the page you want to get data from, i.e. account settings
await page.goto(ACCOUNT_URL)
// Wait for #user_lang select list
await page.waitForSelector('#user_lang')
// Find out what's user's selected language (there might be a better way to do this, I rarely need to do DOM manipulations)
const userLanguage = await page.$eval('#user_lang', languages => [].map.call(languages, lang => { return { lang: lang.textContent, selected: lang.selected } }).find(el => el.selected).lang)
// This scenario assumes we succeed, so I return a successful response, but you might want to return different error codes based on results
return res.status(200).json(userLanguage)
})
const server = http.createServer(app)
server.listen(8000)
:
nan
应用于数组的布尔掩码将生成一维数组:
In [229]: mask = np.isnan(array_a)
In [230]: mask
Out[230]:
array([[ True, True, False],
[False, True, False],
[ True, False, False],
[False, False, True]])
在In [231]: array_a[~mask]
Out[231]: array([0., 0., 0., 0., 0., 0., 0.])
上下文中使用相同的数组:
set
答案 1 :(得分:2)
您也可以这样做:
np.place(array_a, array_a == 0, array_b)
array_a
array([[ nan, nan, 0.324],
[0.254, nan, 0.204],
[ nan, 0.469, 0.381],
[0.292, 0.55 , nan]])
答案 2 :(得分:1)
这应该可以解决问题,尽管可能会有预先写好的解决方案或列表理解器。
import numpy as np
b_index = 0
array_c = np.zeros(np.array(array_a).shape)
for row_index, row in enumerate(array_a):
for col_index, col in enumerate(row):
if not np.isnan(col):
array_c[row_index, col_index] = array_b[b_index]
b_index += 1
else:
array_c[row_index, col_index] = np.nan
>>> print(array_c)
[[ nan nan 0.324]
[0.254 nan 0.204]
[ nan 0.469 0.381]
[0.292 0.55 nan]]