我有以下形式的数据
C1510438;;C0220832;;2
C0026030;;C0034693;;1
C1257960;;C0007452;;1
C0061461;;C0027922;;2
C0011744;;C0037494;;3
C0014180;;C0034493;;3
Run Code Online (Sandbox Code Playgroud)
当我尝试对第3个字段进行排序时,该命令返回错误
sort -t ';;' -k 3 -r -n -o output.txt input.txt
sort: multi-character tab `;;'
Run Code Online (Sandbox Code Playgroud)
我也尝试过
sort -t $';;' -k 3 -r -n -o output.txt input.txt
Run Code Online (Sandbox Code Playgroud)
但该命令返回相同的错误.
知道该怎么办?
我有一个映射,其中pair作为键,第三个整数作为值.我想知道如何迭代键.示例代码粘贴在下面.
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main ()
{
map <pair<int, int>, int> my_map;
pair <int, int> my_pair;
my_pair = make_pair(1, 2);
my_map[my_pair] = 3;
// My attempt on iteration
for(map<pair<int,int>,int>::iterator it = my_map.begin(); it != my_map.end(); ++it) {
cout << it->first << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我解析了一个巨大的书目记录数据库(大约 2000 万条记录)。每条记录都有唯一的 ID 字段、一组作者和一组描述书目记录主要内容的术语/关键字。例如,典型的书目记录如下所示:
ID: 001
Author: author1
Author: author2
Term: term1
Term: term2
Run Code Online (Sandbox Code Playgroud)
首先,我创建两个defaultdict来存储作者和术语:
d1 = defaultdict(lambda : defaultdict(list))
d2 = defaultdict(lambda : defaultdict(list))
Run Code Online (Sandbox Code Playgroud)
接下来,我填充作者:
d1['id001'] = ['author1', 'author2']
d1['id002'] = ['author3']
d1['id003'] = ['author1', 'author4']
Run Code Online (Sandbox Code Playgroud)
和关键词:
d2['id001'] = ['term1', 'term2']
d2['id002'] = ['term2', 'term3']
d2['id003'] = ['term4']
Run Code Online (Sandbox Code Playgroud)
问题是如何连接这两个字典以获得直接链接作者和术语之间的数据对象:
author1|term1,term2,term4
author2|term1,term2
author3|term2,term3
author4|term4
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
知道如何在ggplot2中绘制条形图吗?基础R中的示例粘贴在下面.
stripchart(iris$Sepal.Length, col = "red", method="stack")
Run Code Online (Sandbox Code Playgroud)

我编写了一个用于从 FTP 服务器下载文件的 Java 类。一切看起来都很好,但是当我检查文件的大小时,它们比原始文件小一点。
知道为什么会这样吗?
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.IOException;
public class Downloader {
public static void main(String[] args) {
String server = "ftp.example.gov";
int port = 21;
String user = "anonymous";
String pass = "anonymous";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.cwd("dir1/dir2/gz");
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
String downloadFile = "/home/andrej/Documents/" + file.getName();
OutputStream output = new …Run Code Online (Sandbox Code Playgroud) 我从Stroustrup的编程:使用C++的原理和实践开始,我遇到了第一个问题.长话短说,假设我们有以下代码:
int main() {
cout << "Enter your first name and age:\n";
string first_name = "???";
int age = -1;
cin >> first_name >> age;
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
在本书之后输入22 Carlos应该输出:
Hello, 22 (age -1)
Run Code Online (Sandbox Code Playgroud)
但我得到:
Hello, 22 (age 0)
Run Code Online (Sandbox Code Playgroud)
我不知道出了什么问题.任何想法或指针?
假设我们有一对元组,元组可以有不同的长度.我们称它们为元组,t1并且t2:
t1 = ('A', 'B', 'C')
t2 = ('d', 'e')
Run Code Online (Sandbox Code Playgroud)
现在我使用itertools计算来自两个元组的长度为2的所有组合:
import itertools
tuple(itertools.combinations(t1 + t2, 2))
Run Code Online (Sandbox Code Playgroud)
Itertools生成器产生所有可能的组合,但我只需要在元组之间发生的组合; 预期的产出是
(('A', 'd'), ('A', 'e'), ('B', 'd'), ('B', 'e'), ('C', 'd'), ('C', 'e'))
Run Code Online (Sandbox Code Playgroud)
我想知道删除不需要的组合的最佳方法是什么.
让我们来说明着名iris数据集上的问题.我需要按行应用所选函数,但仅适用于所选列.示例如下:
library(tidyverse)
iris %>%
mutate_at(.funs = scale, .vars = vars(-c(Species))) %>%
rowwise() %>%
mutate(my_mean=mean(c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)))
Run Code Online (Sandbox Code Playgroud)
因此,首先我缩放所有变量,排除Species然后在所有四个数值变量上计算平均行数.但是,在真实数据集中,我有100多个数字变量,我想知道如何说服R自动包含除了所选变量之外的所有变量(例如,Species在给定的例子中).我在SO上讨论解决方案(例如,这个),但所有示例都明确引用了列名.任何指针都非常受欢迎.
编辑:经过一些改变,这是我的解决方案:
iris %>%
as_tibble() %>%
mutate_at(.funs = scale, .vars = vars(-c(Species))) %>%
transmute(Species, row_mean = rowMeans(select(., -Species)))
Run Code Online (Sandbox Code Playgroud) 我正在努力与Lucene的BM25Similarity类(链接).Web上提供的所有示例都涉及较早的实现(链接).我恳请一个指针如何修改下面的标准玩具示例,以包括BM25相似性(创建索引和执行搜索).
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import java.io.IOException;
public class HelloLucene {
public static void main(String[] args) throws IOException, ParseException {
// Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);
// …Run Code Online (Sandbox Code Playgroud) 我(在其他SO用户的帮助下)“粘合”了一个小型C程序,该程序将字符串标签映射到边缘列表数据结构中的整数标签。例如,对于输入文件
Mike Andrew
Mike Jane
John Jane
Run Code Online (Sandbox Code Playgroud)
程序输出
1 2
1 3
4 3
Run Code Online (Sandbox Code Playgroud)
但是,我映射了巨大的边缘列表文件,不幸的是,与Python相比,该程序运行非常慢。下面,我用C和Python粘贴了这两个程序。请问如何提高C程序速度的指针。
#include <stdio.h>
#include <stdlib.h>
// Initial number of maximal lines in a file
enum { MAXL = 200};
typedef struct {
unsigned int first;
unsigned int second;
} edge;
typedef struct {
unsigned int hashed;
char **map;
} hash;
int insertInMap(hash *map, char *entry)
{
int i =0;
for (i=0;i<map->hashed;i++)
{
if (strcmp(map->map[i],entry) == 0)
return i+1;
}
/* Warning no boundary check is added …Run Code Online (Sandbox Code Playgroud) 我有一个单词列表,例如:
[man, walk, ball]
Run Code Online (Sandbox Code Playgroud)
我希望让它们同时出现;IE:
[('man', 'walk'), ('man', 'ball'), ('walk', 'ball')]
Run Code Online (Sandbox Code Playgroud)
我使用以下代码:
from itertools import product
my_list = [man, walk, ball]
list(product(my_list, my_list))
Run Code Online (Sandbox Code Playgroud)
这给了我:
[('man', 'man'), ('man', 'walk'), ('man', 'ball'), ('walk', 'man'), ('walk', 'walk'), ('walk', 'ball'), ('ball', 'man'), ('ball', 'walk'), ('ball', 'ball')]
Run Code Online (Sandbox Code Playgroud)
我想知道如何省略重复的对?
我有两个元组:
t1 = ('A', 'B')
t2 = ('C', 'D', 'E')
Run Code Online (Sandbox Code Playgroud)
我想知道如何在元组之间创建组合,因此结果应该是:
AC, AD, AE, BC, BD, BE
Run Code Online (Sandbox Code Playgroud)
编辑
运用
list(itertools.combinations('abcd',2))
Run Code Online (Sandbox Code Playgroud)
我可以为给定的字符串生成组合列表:
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
Run Code Online (Sandbox Code Playgroud)
如果我插入元组而不是字符串,则会发生以下错误:
TypeError: sequence item 0: expected string, tuple found
Run Code Online (Sandbox Code Playgroud)
有什么建议怎么办?
python ×5
r ×3
c++ ×2
dictionary ×2
bash ×1
c ×1
defaultdict ×1
dplyr ×1
ftp ×1
ftp-client ×1
ggplot2 ×1
graph-theory ×1
iterator ×1
java ×1
linux ×1
lucene ×1
nan ×1
output ×1
similarity ×1
sorting ×1
stdmap ×1
tidyverse ×1
tuples ×1