假设我有3个char变量a,b和c.
每一个都可以'0',这是一个特例,意味着它匹配每个字符.
所以,如果是'0',我只需要检查是否b == c.
我想检查是否a == b == c,但发现C#中的实现变得混乱和冗长.
您可以提供任何创意或漂亮的解决方案吗?
更新
对于绩效驱动,采取Erik A. Brandstadmoen的方法.为简单起见,使用M4N的apprach,我也做了一些修改:!(query.Any() && query.Distinct().Skip(1).Any())
在Shiny教程中,有一个例子:
fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2))
shinyServer(function(input, output) {
currentFib <- reactive({ fib(as.numeric(input$n)) })
output$nthValue <- renderText({ currentFib() })
output$nthValueInv <- renderText({ 1 / currentFib() })
})
Run Code Online (Sandbox Code Playgroud)
我不知道如何reactive缓存这些值.它内部是否做了类似的事情return(function() cachedValue)?现在我想知道我能不能这样做?
fib <- function(n) ifelse(n<3, 1, fib(n-1)+fib(n-2))
shinyServer(function(input, output) {
currentFib <- reactiveValues({ fib(as.numeric(input$n)) })
output$nthValue <- renderText({ currentFib })
output$nthValueInv <- renderText({ 1 / currentFib })
})
Run Code Online (Sandbox Code Playgroud) 例如:
1.
foreach (var item in myDic)
{
if (item.value == 42)
myDic.remove(item.key);
}
Run Code Online (Sandbox Code Playgroud)
无论内括号中的语句如何影响,迭代器都能正常工作myDic吗?
2.
var newDic = myDic.where(x=>x.value!=42).ToDictionary(x=>x.key,x=>x.value);
Run Code Online (Sandbox Code Playgroud)
第二种方法是一种好的做法吗?函数式编程和不可变?
如果是这样,我们为什么需要sapply?
x <- list(a=1, b=1)
y <- list(a=1)
JSON <- rep(list(x,y),10000)
microbenchmark(sapply(JSON, function(x) x$a),
unlist(lapply(JSON, function(x) x$a)),
sapply(JSON, "[[", "a"),
unlist(lapply(JSON, "[[", "a"))
)
Unit: milliseconds
expr min lq median uq max neval
sapply(JSON, function(x) x$a) 25.22623 28.55634 29.71373 31.76492 88.26514 100
unlist(lapply(JSON, function(x) x$a)) 17.85278 20.25889 21.61575 22.67390 78.54801 100
sapply(JSON, "[[", "a") 18.85529 20.06115 21.53790 23.42480 38.56610 100
unlist(lapply(JSON, "[[", "a")) 11.33859 11.69198 12.25329 13.37008 27.81361 100
Run Code Online (Sandbox Code Playgroud) 说,我有一个datetime:
given_time = datetime(2013, 10, 8, 0, 0, 33, 945109,
tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=60,
name=None))
Run Code Online (Sandbox Code Playgroud)
我想将其转换为np.datetime64:
np.datetime64(given_time)
> numpy.datetime64('2013-10-08T00:00:33.945109+0100')
Run Code Online (Sandbox Code Playgroud)
它运作良好.但是,如果我有一个数组given_time:
given_times = np.array([given_time]*3) # dtype is object
Run Code Online (Sandbox Code Playgroud)
双方given_times.astype('datetime64')并given_times = np.array([given_time] * 3, dtype=np.datetime64)会引发TypeError: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'
所以,我必须指定单位:
given_times.astype('datetime64[us]')
# or
given_times = np.array([given_time]*3, dtype='datetime64[us]')
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么我必须在这里指定单位?它不需要np.datatime64构造函数中的单元.
我想捕获第一场比赛,NA如果没有比赛则返回.
regexpr("a+", c("abc", "def", "cba a", "aa"), perl=TRUE)
# [1] 1 -1 3 1
# attr(,"match.length")
# [1] 1 -1 1 2
x <- c("abc", "def", "cba a", "aa")
m <- regexpr("a+", x, perl=TRUE)
regmatches(x, m)
# [1] "a" "a" "aa"
Run Code Online (Sandbox Code Playgroud)
所以我期待"a",NA,"a","aa"
鉴于x, y是张量,我知道我能做到
with tf.name_scope("abc"):
z = tf.add(x, y, name="z")
Run Code Online (Sandbox Code Playgroud)
这z就是命名"abc/z".
我想知道f在以下情况下是否存在直接分配名称的函数:
with tf.name_scope("abc"):
z = x + y
f(z, name="z")
Run Code Online (Sandbox Code Playgroud)
f我现在使用的愚蠢是z = tf.add(0, z, name="z")
我感到惊讶的是,它abs适用于numpy数组,但不适用于列表.这是为什么?
import numpy as np
abs(np.array((1,-2)))
array([1, 2])
abs([1,-1])
TypeError: bad operand type for abs(): 'list'
Run Code Online (Sandbox Code Playgroud)
此外,内置函数sum也适用于numpy数组.我想这是因为numpy数组支持__getitem__?但是abs,如果它依赖于__getitem__它应该也适用于列表,但它没有.
看看这2个循环
const int arrayLength = ...
Run Code Online (Sandbox Code Playgroud)
版本0
public void RunTestFrom0()
{
int sum = 0;
for (int i = 0; i < arrayLength; i++)
for (int j = 0; j < arrayLength; j++)
for (int k = 0; k < arrayLength; k++)
for (int l = 0; l < arrayLength; l++)
for (int m = 0; m < arrayLength; m++)
{
sum += myArray[i][j][k][l][m];
}
}
Run Code Online (Sandbox Code Playgroud)
版本1
public void RunTestFrom1()
{
int sum = 0;
for (int i = 1; …Run Code Online (Sandbox Code Playgroud) test <- data.table(x=sample.int(10, 1000000, replace=TRUE))
y <- test$x
test[,.N, by=x] # fast
test[,.N, by=y] # extremely slow
Run Code Online (Sandbox Code Playgroud)
为什么在第二种情况下它很慢?
这样做更快:
test[,y:=y]
test[,.N, by=y]
test[,y:=NULL]
Run Code Online (Sandbox Code Playgroud)
它看起来好像很难优化?