标签: any

在 R 中,如果某个值位于同一数据帧的任何特定列中,如何计数?

我有这个数据框,我想计算 VAL1-VAL3 中的一个值,如果在 T1-T5 中则对其求和

VAL1 <- c(1,3,2,4,9)
VAL2 <- c(5,6,3,1,2)
VAL3 <- c(7,5,5,9,5)
T1   <- c(4,2,6,4,8)
T2   <- c(10,1,2,9,9)
T3   <- c(6,8,6,3,2)
T4   <- c(5,4,2,4,1)
T5   <- c(9,7,8,4,5)

df<- data.frame(VAL1,VAL2,VAL3,T1,T2,T3,T4,T5)`
Run Code Online (Sandbox Code Playgroud)
值1 值2 值3 T1 T2 T3 T4 T5
1 5 7 4 10 6 5 9
3 6 5 2 1 8 4 7
2 3 5 6 2 6 2 8
4 1 9 4 9 3 4 4
9 2 5 8 9 2 1 5

所需的输出,新列RESULT1-RESULT3(相同的df): …

r count any

5
推荐指数
1
解决办法
96
查看次数

Python NameError:未定义全局名称"any"

我在生产服务器上收到以下错误:

Traceback (most recent call last):

 File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
 File "myproject/middleware.py", line 31, in process_request
if not any(m.match(path) for m in EXEMPT_URLS):

NameError: global name 'any' is not defined
Run Code Online (Sandbox Code Playgroud)

服务器正在运行python 2.6,并且在开发过程中没有引发此错误.违规代码在middleware.py:

...
if not request.user.is_authenticated():
        path = request.path_info.lstrip('/')
        if not any(m.match(path) for m in EXEMPT_URLS):
            return HttpResponseRedirect(settings.LOGIN_URL)
Run Code Online (Sandbox Code Playgroud)

我应该重写这个any函数来解决这个问题吗?

python any

4
推荐指数
1
解决办法
3248
查看次数

在异常情况下,C++错误C2228('.''的左边必须有class/struct/union)

在C++中,我试图any用C++ 实现我自己的类.然而,在我能够测试它之前(所以如果我的实现很糟糕,请随意纠正我),我得到了错误:error C2228: left of '.val' must have class/struct/union两次使用该value()函数两次,这在其他地方工作时看起来很奇怪.我唯一能想到的是decltype函数的前面是导致错误,但它不应该:

编辑:我已经更新了更改template<class T> any(T V){...}构造函数变量的方法

class any{
protected:
    template<class T> struct variable{
    public:
        T val;
        variable(){}
        variable(T t) : val(t){}
    };
    variable<int> v;
public:
    any(){
        v.val = 0;
    }
    template<class T> any(T V){
        variable<T> nV(V);
        v = nV;
    }
    ~any(){
        delete &v;
    }
    decltype(v.val) value(){ // Error still here
        return v.val;
    }
    template<class T> static any create(T V){
        return any(V);
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ struct decltype any c++11

4
推荐指数
1
解决办法
1030
查看次数

Scala数组具有不同的数据类型

*我们知道scala数组包含相同类型的数据.但当我宣布数组为

var a = new Array[Any](3)
Run Code Online (Sandbox Code Playgroud)

我能够存储不同的数据类型.

a(0)=5
a(1)="hello"
a(2)=1.5
Run Code Online (Sandbox Code Playgroud)

这怎么可能?如果它是错的那么我们在scala中有什么选项来存储不同的数据类型?*

arrays scala any

4
推荐指数
1
解决办法
3352
查看次数

Haskell/GHC表现的`any` /`all`

我编写了量化函数exists,forall以及noneHaskell的内置[]列表数据类型.在很多场合,这些似乎比Prelude/ Data.Lists anyall.我天真地怀疑这种表现是由于anyall使用Θ(n)折叠实现的.由于我对Haskell相对较新,我认为我必须弄错,或者说这种现象有充分的理由.

来自Data.Foldable:

-- | Determines whether any element of the structure satisfies the predicate.
any :: Foldable t => (a -> Bool) -> t a -> Bool
any p = getAny #. foldMap (Any #. p)

-- | Determines whether all elements of the structure satisfy the predicate.
all :: Foldable t => (a -> Bool) -> t …
Run Code Online (Sandbox Code Playgroud)

performance haskell linked-list fold any

4
推荐指数
1
解决办法
506
查看次数

postgresql 将文本搜索到文本数组中

我有一张桌子 t1

id  |  names
----|-------------------------
1   |  {jully , alex , sarah}
2   |  {bety , cate , jenifer}
3   |  {adam , pit , joee}
4   |  {piter , mat , andy}
Run Code Online (Sandbox Code Playgroud)

所以,我需要行至少有一个以“a”开头的名称,我需要的结果在下面

第 1 行:亚历克斯

第 3 行:亚当

第 4 行:安迪

id   |   names
-----|-------------------------
1    |  {jully , alex , sarah}
3    |  {adam , pit , joee}
4    |  {piter , mat , andy}
Run Code Online (Sandbox Code Playgroud)

像这样的查询

select * from t1 where 'a' like% any t1.name
Run Code Online (Sandbox Code Playgroud)

arrays postgresql any sql-like

4
推荐指数
1
解决办法
2597
查看次数

python:如果列表中没有以'p ='开头的元素,则返回False

有一个像这样的清单

lst = ['hello', 'stack', 'overflow', 'friends']
Run Code Online (Sandbox Code Playgroud)

我该怎么做:

if there is not an element in lst starting with 'p=' return False else return True
Run Code Online (Sandbox Code Playgroud)

我在想的是:

for i in lst:
   if i.startswith('p=')
       return True
Run Code Online (Sandbox Code Playgroud)

但是我不能在循环中添加返回False或者它在第一个元素处出现.

python loops list any

4
推荐指数
3
解决办法
252
查看次数

使用R中的any()实现检测数字是否为素数的函数

正在探索避免for循环的方法,而是使用any()函数来实现一个函数,当传递的参数为prime和false时,它给出true.

这是我有的:

prime2 <- function(n) {
  rangeOfNumbers <- range(2:(n-1))
  if(any(n%%rangeOfNumbers == 0)){
    return(FALSE)
  }
  else return(TRUE)
}
Run Code Online (Sandbox Code Playgroud)

看起来很直接,但prime(55)给出TRUE而不是假.

我究竟做错了什么?

primes r data-analysis any

4
推荐指数
1
解决办法
481
查看次数

如何使用 jq 数组值返回 true

如果列表中的任何一个条件为 true,我尝试使用以下 jq 命令返回 true 输出。

.Tags[] as $t| "aws:cloudformation:stack-name"| IN($t[])   
Run Code Online (Sandbox Code Playgroud)

输入

 {
    "Tags": [{
            "Value": "INF-D-XX-SEC-OPNV-UW1",
            "Key": "Name"
        },
        {
            "Value": "INF-D-XX-CFS-StandardInfrastructure-UW1",
            "Key": "aws:cloudformation:stack-name"
        },
        {
            "Value": "sgOpenVPNAccess",
            "Key": "aws:cloudformation:logical-id"
        },
        {
            "Value": "UW1",
            "Key": "Location"
        },
        {
            "Value": "INF",
            "Key": "Application"
        },
        {
            "Value": "D",
            "Key": "Parent Environment"
        },
        {
            "Value": "arn:aws:cloudformation:us-west-1:111111:stack/INF-D-XX-CFS-StandardInfrastructure-UW1/1111-11-11e8-96fe-11",
            "Key": "aws:cloudformation:stack-id"
        },
        {
            "Value": "OPNV",
            "Key": "ResourceType"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

这给了我一个返回布尔值的列表,如下所示,

- 输出 -

true
false
false
false
false
false
false
Run Code Online (Sandbox Code Playgroud)

true如果其中之一,我想返回一个值

Key="aws:cloudformation:stack-name" 
Run Code Online (Sandbox Code Playgroud)

被检测到并且没有给我返回值列表。

arrays json any jq

4
推荐指数
1
解决办法
6547
查看次数

检查列表是否为空的最快方法是什么?在什么情况下会优先选择一种方法?

我想知道检查列表是否为空的最佳方法是什么。

我知道这个问题看起来很愚蠢,但我意识到有时当你与其他人一起工作并且其他人阅读你的代码时,使用某些函数可能看起来比其他函数更好,并且对于来自不同编程语言的人来说更容易理解。

假设我有一个清单:

names = ["Bob", "Billy", "Samuel", "Adam", "Rob"]
Run Code Online (Sandbox Code Playgroud)

这是我检查列表是否为空的一种方法:

is_empty = bool(names)
Run Code Online (Sandbox Code Playgroud)

这是另一个:

is_empty = any(names)
Run Code Online (Sandbox Code Playgroud)

我确实意识到 any 函数会检查列表中是否至少有 1 个真值。检查列表是否为空的最佳和最快的方法是什么?哪个看起来最好,在什么场景下?有没有我不知道的更快的方法?

python boolean list any

4
推荐指数
1
解决办法
1万
查看次数