我有一个问题,我需要(至少可以肯定)通过整个列表来解决.问题是找出列表中连续数字的最大数量,该数字加起来列表中的另一个(更大)元素.如果没有,那么我们只需将列表中的最大值作为候选求和,将1作为最大连续元素数.
我的通用代码有效,但对于大型列表(> 500,000个元素)则不太好.我只是在寻找有关如何以不同方式解决问题的提示.我目前的做法:
L = [1,2,3,4,5,6,7,8,9,10]
candidate_sum = L[-1]
largest_count = 1
N = len(L)
i = 0
while i < N - 1:
s = L[i]
j = 0
while s <= (N - L[i + j + 1]):
j += 1
s += L[i+j]
if s in L and (j+1) > largest_count:
largest_count = j+1
candidate_sum = s
i+=1
Run Code Online (Sandbox Code Playgroud)
在这种情况下,答案是[1,2,3,4],因为它们加起来为10,长度为4(显然这个例子L是一个非常简单的例子).
然后我通过将初始while循环条件更改为:
while i < (N-1)/largest_count
Run Code Online (Sandbox Code Playgroud)
这不是一个很好的假设,但基本认为数字的分布有些统一,因此列表后半部分的两个数字平均大于列表中的最终数字,因此被取消资格.
我只是在寻找:
有没有办法在融合中突出显示 R 语法?我已经尝试过常规配置 > 配置代码宏 > 添加新语言,但我不知道如何为 R 上传自定义画笔语法。
我试图获得连续行之间的差异,例如:
dt_in <- data.table(
Game = c(1,1,2,2,3,3,4,4),
ID = c(1,2,3,4,5,6,7,8),
weight = c(150,120,151,160,190,170,170,170)
)
Run Code Online (Sandbox Code Playgroud)
我想计算每个ID,他们有多少或多少重量比他们的其他ID Game
.到目前为止我所做的是:
dt_in[, d1 := diff(weight), by = list(Game)]
Run Code Online (Sandbox Code Playgroud)
这使:
> dt_in
Game ID weight d1
1: 1 1 150 -30
2: 1 2 120 -30
3: 2 3 151 9
4: 2 4 160 9
5: 3 5 190 -20
6: 3 6 170 -20
7: 4 7 170 0
8: 4 8 170 0
Run Code Online (Sandbox Code Playgroud)
但我想要的是:
> dt_in
Game ID weight d1 want
1: …
Run Code Online (Sandbox Code Playgroud) 在以下代码中:
import tkinter as tk
class CardShuffling(tk.Tk):
background_colour = '#D3D3D3'
deck_size = 52
def __init__(self):
tk.Tk.__init__(self) # What does this do?
Run Code Online (Sandbox Code Playgroud)
我对最后一行的目的感到困惑.CardShuffling的一个实例继承自来tk.Tk
,不是最后一行只是重复已经由...处理过的事情了CardShuffling(tk.Tk)
?
我有一个循环遍历所有点并将它们与其他点进行比较的函数(是的,我知道这不需要在plpgsql中完成 - 这是一个玩具MWE).该函数返回具有最大x坐标的点:
create type point as (x integer, y integer);
create or replace function test() returns set of Point as
$$
declare
p1 point;
p2 point;
bool integer;
begin
for p1 in select * from table loop
bool := 0;
for p2 in select * from table loop
if p2.x > p1.x then bool :=1;
exit;
end if;
end loop;
if bool = 0 then return next p1;
end if;
end loop;
end;
$$ language 'plpgsql';
Run Code Online (Sandbox Code Playgroud)
哪个有效.我想要做的是能够将表名作为函数的参数,我对于放置execute
语句的位置感到困惑.
我有一个三维的numpy数组形状(x,y,R)
.对于每一(x,y)
对,我有一个R值的1D numpy数组.我想将整个数组设置为nan
任何R值是否为nan
或zero
.我试过类似的东西
# 3d np array is called: data
mask1 = (data==0).any(axis=2)
mask2 = (data==np.nan).any(axis=2)
data[np.logical_or(mask1, mask2)] = np.nan
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,我认为问题是我试图numpy array
用较低维度的布尔区域进行子集化的方式,但不太确定如何解决这个问题.
一些示例数据:
y = np.random.random(size=(2,2,3))
y[0,0,2] = np.nan
y[0,1,0] = np.nan
y[0,0,1] = np.nan
y[1,1,2] = 0.
Run Code Online (Sandbox Code Playgroud)
以便:
y[0,0,:]
array([0.092718, nan, nan])
y[0,1,:]
array([ nan, 0.00243745, nan])
y[1,0,:]
array([0.5282173 , 0.7548559 , 0.08869139])
y[1,1,:]
array([0.19612415, 0.16969036, 0.0])
Run Code Online (Sandbox Code Playgroud)
和期望的结果:
y[0,0,:]
array([nan, nan, nan])
y[0,1,:]
array([nan, nan, nan]) …
Run Code Online (Sandbox Code Playgroud) 要从数据框中删除行,请使用以下命令:
data <- data[-1, ]
Run Code Online (Sandbox Code Playgroud)
例如,删除第一行.我需要删除前6行,所以我使用了以下内容:
data <- data[-c(1,2,3,4,5,6), ]
Run Code Online (Sandbox Code Playgroud)
要么
data <- data[-(1:6), ]
Run Code Online (Sandbox Code Playgroud)
这可以删除行名称,但引入了一个名为" row.names
我无法摆脱"的新列,除非我使用该命令:
row.names(data) <- NULL
Run Code Online (Sandbox Code Playgroud)
这是什么原因?有没有更好的方法可以用一个命令删除多个行/列?
例:
在以下代码之后:
tquery <- tquery[-(1:6), ]
Run Code Online (Sandbox Code Playgroud)
这是数据:
我正在尝试获取累积总和的向量,也就是说,我有:
# 500 Samples from the U(0,1) Distribution
U<-runif(500,0,1)
# Empty Vector of length 500
F<-rep(0,500)
# Fill the vector with f(U(k))
for ( i in 1:500 ){
F[i] <- sqrt(1-U[i]^2)
}
# Another Empty Vector of length 500
I<-rep(0,500)
# Fill the second empty vector with the sums of F
for ( i in 1:500 ){
I[i]<-cumsum(F[1]:F[i])
}
Run Code Online (Sandbox Code Playgroud)
代码的最后一行是问题,我希望“ I”成为一个向量,使得I [1] = F [1],I [n] = F [1] + F [2] + ..... + F [n]。出于某些原因,cumsum函数无法正常工作。尝试这样做有什么问题?
我在R中有以下'list':
[[1]]
[1] 17336 5246 8597 5246 17878 19701
[[2]]
[1] 19701 37748 18155 5246 8597
[[3]]
[1] 12297 19701 17878 5246 17336 8597 17878
[[4]]
[1] 17878 37748 19701 37748 12297 8597
[[5]]
[1] 19701 37748 19701 37748 19701 5246
[[6]]
[1] 19701 6254 17336 18155 19701 12297
[[7]]
[1] 19701 17878 18155 17878 18155 19701 8597
[[8]]
[1] 8597 18155
[[9]]
[1] 12450 18155 5246 8597 5246 8597
[[10]]
[1] 18155 4105 6254 17878 12297 5246
[[11]]
[1] …
Run Code Online (Sandbox Code Playgroud) 我的数据看起来像:
player event diff
A x NA
A y 2
A z 240
A w 3
A x 9
B x NA
B y 3
B z 120
C x NA
C x 8
Run Code Online (Sandbox Code Playgroud)
我做的是通过玩家列分组并获取时间事件之间的差异,因此每当新玩家有事件时,差异列的NA都是.
我想要做的是将数据划分为彼此在几分钟内的玩家特定交互(比如diff = 20的截止值).我最终想要的是:
player event diff interaction
A x NA 1
A y 2 1
A z 240 2
A w 3 2
A x 9 2
B x NA 1
B y 3 1
B z 120 2
C x NA 1
C x …
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的数据框:
Game Team Value
1 A 0.6
1 B 0.5
2 C 1.2
2 D 0.3
Run Code Online (Sandbox Code Playgroud)
我想创建一个新列来计算给定游戏的"值"差异,因此它将是:
difference
0.1
-0.1
0.9
-0.9
Run Code Online (Sandbox Code Playgroud)
换句话说,我想按"游戏"分组,但我不太清楚如何做到这一点
r ×7
python ×3
list ×2
python-3.x ×2
confluence ×1
data.table ×1
dplyr ×1
numpy ×1
oop ×1
plpgsql ×1
postgresql ×1
sql ×1
tkinter ×1
while-loop ×1