来自Rust指南:
要取消引用(获取引用的值而不是引用本身)
y,我们使用星号(*)
所以我做到了:
fn main() {
let x = 1;
let ptr_y = &x;
println!("x: {}, ptr_y: {}", x, *ptr_y);
}
Run Code Online (Sandbox Code Playgroud)
即使没有明确的解引用,这也给了我相同的结果(x = 1; y = 1):
fn main() {
let x = 1;
let ptr_y = &x;
println!("x: {}, ptr_y: {}", x, ptr_y);
}
Run Code Online (Sandbox Code Playgroud)
为什么?不应该ptr_y打印内存地址并*ptr_y打印1?是否有某种自动解除引用或者我错过了什么?
FileWriter writer = new FileWriter(output_file);
int i = 0;
try (Stream<String> lines = Files.lines(Paths.get(input_file))) {
lines.forEach(line -> {
try {
writer.write(i + " # " + line + System.lineSeparator());
} catch (Exception e) {
e.printStackTrace();
}
}
);
writer.close();
}
Run Code Online (Sandbox Code Playgroud)
我需要用行号写行,所以我试着在.forEach()中添加一个计数器,但是我无法让它工作.我只是不知道把i ++放在哪里; 进入代码,随机搞砸到目前为止没有帮助.
我有这个(剥离代码示例的HTML标签)函数,用CSV构建HTML表格,但每次尝试运行它时都会出现运行时错误,我不知道为什么.谷歌说,编码的东西可能是错误的,但我不知道如何改变它.
我的CSV以ANSI编码,包含ä,Ä,Ü,Ö等字符,但我无法控制编码或将来是否会发生变化.
这里发生错误:
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at testgui.Csv2Html.start(Csv2Html.java:121)
Run Code Online (Sandbox Code Playgroud)
第121行是
lines.forEach(line -> {
Run Code Online (Sandbox Code Playgroud)
源代码:
protected void start() throws Exception {
Path path = Paths.get(inputFile);
FileOutputStream fos = new FileOutputStream(outputFile, true);
PrintStream ps = new PrintStream(fos);
boolean withTableHeader = (inputFile.length() != 0);
try {
Stream<String> lines = Files.lines(path);
lines.forEach(line -> {
try {
String[] columns = line.split(";");
for (int i=0; i<columns.length; i++) {
columns[i] = …Run Code Online (Sandbox Code Playgroud) 根据https://github.com/GoogleChrome/puppeteer/issues/628,我应该可以通过以下单行获取<a href ="xyz">的所有链接:
const hrefs = await page.$$eval('a', a => a.href);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试一个简单的
console.log(hrefs)
Run Code Online (Sandbox Code Playgroud)
我只能得到
http://example.de/index.html
Run Code Online (Sandbox Code Playgroud)
作为输出,这意味着它只能找到1个链接?但该页面在源代码/ DOM中肯定有12个链接.为什么找不到它们?
最小的例子:
'use strict';
const puppeteer = require('puppeteer');
crawlPage();
function crawlPage() {
(async () => {
const args = [
"--disable-setuid-sandbox",
"--no-sandbox",
"--blink-settings=imagesEnabled=false",
];
const options = {
args,
headless: true,
ignoreHTTPSErrors: true,
};
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
await page.goto("http://example.de", {
waitUntil: 'networkidle2',
timeout: 30000
});
const hrefs = await page.$eval('a', a => a.href); …Run Code Online (Sandbox Code Playgroud)我想创建一个 pandas dataframe df ,例如:
df = pd.DataFrame(
{
"group": ["A", "A", "A", "A", "A"],
"date": ["2020-01-02", "2020-01-13", "2020-02-01", "2020-02-23", "2020-03-05"],
"value": [10, 20, 16, 31, 56],
}
)
Run Code Online (Sandbox Code Playgroud)
我想在创建数据帧时将“日期”列指定为 dtype=datetime64[ns],而不是之后。
所以不是这样的:
df = pd.DataFrame(
{
"group": ["A", "A", "A", "A", "A"],
"date": ["2020-01-02", "2020-01-13", "2020-02-01", "2020-02-23", "2020-03-05"],
"value": [10, 20, 16, 31, 56],
}
)
df["date"] = pd.to_datetime(df["date"])
Run Code Online (Sandbox Code Playgroud)
但像这样:
df = pd.DataFrame(
{
"group": ["A", "A", "A", "A", "A"],
"date": pd.Series(
["2020-01-02", "2020-01-13", "2020-02-01", "2020-02-23", "2020-03-05"],
dtype=np.datetime64,
),
"value": …Run Code Online (Sandbox Code Playgroud) 我需要读取一个文件,获取每一行,遍历每一行并检查该行是否包含来自“aeiuo”的任何字符,以及它是否包含至少 2 个字符“äüö”。
这段代码是 Rust 惯用的吗?如何检查 a 中的多个字符String?
到目前为止,我尝试了一些 Google 和代码窃取:
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("foo.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open {}: {}", display, Error::to_string(&why)),
Ok(file) => …Run Code Online (Sandbox Code Playgroud) 我有一个数字列表:
[18, 22, 20]
Run Code Online (Sandbox Code Playgroud)
和一个数据框:
Id | node_id
UC5E9-r42JlymhLPnDv2wHuA | 20
UCFqcNI0NaAA21NS9W3ExCRg | 18
UCrb6U1FuOP5EZ7n7LfOJMMQ | 22
Run Code Online (Sandbox Code Playgroud)
列表编号映射到 node_id 编号。node_id 编号的顺序很重要,它们必须按列表编号的顺序排列。
所以数据帧的顺序是错误的。
我需要按列表值对数据框进行排序。
最终结果应该是:
Id | node_id
UCFqcNI0NaAA21NS9W3ExCRg | 18
UCrb6U1FuOP5EZ7n7LfOJMMQ | 22
UC5E9-r42JlymhLPnDv2wHuA | 20
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有一个数据框 df ,其中有 2 列,我想将其绘制在一起,并以天数作为索引:
| col1 | col2 | col3 | ...
2020-01-01 | 1 | 300000 | ...
2020-01-02 | 1000 | 600000 | ...
2020-01-03 | 3000 | 50000 | ...
Run Code Online (Sandbox Code Playgroud)
通过绘制 col1 + col2
df[["col1", "col2"]].plot()
Run Code Online (Sandbox Code Playgroud)
显示从 0 到 1.0 的值,并在顶部“1e6”,如下例所示: https: //i.stack.imgur.com/tJjgX.png
我想要 y 轴上的完整值范围,而不是科学记数法。我如何通过 pandas .plot() 或 matplotlib 来做到这一点?
我有一个清单:
["a", "b", "c", "d"]
Run Code Online (Sandbox Code Playgroud)
并列出 b1:
["a", "b", "x"]
Run Code Online (Sandbox Code Playgroud)
和b2:
["a", "z", "x"]
Run Code Online (Sandbox Code Playgroud)
如果 b1 至少有 2 个来自 a 的元素,则结果为 True。如果 b2 至少有 2 个来自 a 的元素,则结果为 True。
在此示例中,b1 == True 且 b2 == False。
我如何在 Python 中检查这一点?
得到一个带有“Id”列的数据框 df
Id
0 -KkJz3CoJNM
1 08QMXEQbEWw
2 0ANuuVrIWJw
3 0pPU8CtwXTo
4 1-wYH2LEcmk
Run Code Online (Sandbox Code Playgroud)
我需要将列“Id”转换为 set() 但是
set_id = set(df["Id"])
print(set_id)
Run Code Online (Sandbox Code Playgroud)
回报
{'Id'}
Run Code Online (Sandbox Code Playgroud)
而不是“Id”列中字符串的 set() ?