相关疑难解决方法(0)

为什么“any”和“all”在这里似乎没有使用短路求值?

all()在序列中找到 False 后是否立即返回 False?
尝试运行这段代码:

def return_true():
    print('I have just been printed')
    return True

print(all((False, return_true())))
Run Code Online (Sandbox Code Playgroud)

如您所见,I have just been printed即使前面有 False,也会打印出来。

另一个例子:

def return_false():
    print('I have just been printed')
    return False

print(any((True, return_false())))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,I have just been printed即使之前有 True,也会在此代码中打印。

python

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

如何在python中检查字符串是否仅包含数字和/?

我正在尝试检查一个字符串是否只包含 / 和数字,以用作一种验证形式,但是我找不到同时执行这两项操作的方法。ATM 我有这个:

if Variable.isdigit() == False:
Run Code Online (Sandbox Code Playgroud)

这适用于数字,但我还没有找到一种方法来检查斜线。

python string if-statement

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

Pandas 删除值小于给定值的行

我想删除仅包含小于 10 且大于 25 的值的行。我的示例数据框将如下所示:

a   b   c  
1   2   3  
4   5   16  
11  24  22  
26  50  65  
Run Code Online (Sandbox Code Playgroud)

预期输出:

a   b   c  
1   2   3  
4   5   16   
26  50  65  
Run Code Online (Sandbox Code Playgroud)

因此,如果该行包含任何小于 10 或大于 25 的值,则该行将保留在数据帧中,否则,需要将其删除。

有什么方法可以用 Pandas 实现这一点,而不是遍历所有行?

python pandas

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

Python内置函数`all`和`any`的Perl pendant

是否有工作像Python函数Perl函数allanyJobin的答案是对这两种功能如何运作的简短解释.

我想确定(没有循环)是否所有error-msg都defined and ne ""在以下结构中:

$VAR1 = [{
  'row' => [{
      err_msg => "msg1",
      a => "a1",
      b => "b1"
    },
    {
      err_msg => "msg2",
      a => "a2",
      b => "b2"
    }]
},
{
  'row' => [{
      err_msg => "msg3",
      a => "a3",
      b => "b3"
    },
    {
      err_msg => "msg4",
      a => "a4",
      b => "b4"
    }]
}]
Run Code Online (Sandbox Code Playgroud)

python perl perl-data-structures

3
推荐指数
1
解决办法
86
查看次数

Python 2.7 如果没有任何语法?

我刚刚在删除包含某些字符串问题的行上阅读了Sloth的精彩回复,同时寻找一种方法来过滤掉 txt/csv 文件中的垃圾行。要点是“从输入文件中获取 xyz 单词/字符串/任何内容,然后过滤每一行,只写未过滤的行。”

他贴出的代码是:

bad_words = ['bad', 'naughty']

with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
    if not any(bad_word in line for bad_word in bad_words):
        newfile.write(line)
Run Code Online (Sandbox Code Playgroud)

我的问题是:有人会解释这条线if not any(bad_word in line for bad_word in bad_words):吗?

我试过只输入,if not any(bad_word in line):但它给了我一个错误。

我试图理解为什么。在 python 文档网页上粗略搜索并没有帮助我(我是 Python/编程的新手,可能不太聪明,无法启动 :-))。

感谢我阅读的任何参考资料。

谢谢!

syntax if-statement list python-2.7

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

针对一个字符串测试多个子字符串

如果我有一个字符串列表:

matches = [ 'string1', 'anotherstring', 'astringystring' ]
Run Code Online (Sandbox Code Playgroud)

我还有另一个要测试的字符串:

teststring = 'thestring1'
Run Code Online (Sandbox Code Playgroud)

我想测试每个字符串,如果有匹配,就做一些事情。我有:

match = 0
for matchstring in matches:
  if matchstring in teststring:
    match = 1
if !match:
  continue
Run Code Online (Sandbox Code Playgroud)

这是一个循环,所以如果我们没有得到匹配,我们就再次循环(当然我可以反转这个逻辑,如果匹配则做一些事情),但是代码看起来很笨拙,而不是Pythonic,如果很容易理解的话。

我认为有更好的方法可以做到这一点,但我对 python 的理解并不如我所愿。有更好的方法吗?

请注意,“重复”是相反的问题(尽管相同的答案方法是相同的)。

python

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

向“or”语句添加参数的更简单方法?

有没有一种更简单的方法可以执行以下操作而无需我写出来outputbodypixel == everytime?理想情况下,我想从列表中提取数据,我可以在其中添加#ebebeb, #ececec, #212121

if(outputbodypixel == "#356044" or outputbodypixel == "#22402b" or
   outputbodypixel == "#213f2c" or outputbodypixel == "#356043" or
   outputbodypixel == "#22402c" or outputbodypixel == "#346044"):
    output_body = "green"
elif(outputbodypixel == "#7c3d15" or outputbodypixel == "#493613" or
     outputbodypixel == "#4a3612" or outputbodypixel == "#6a553e" or
     outputbodypixel == "#785735" or outputbodypixel == "#5e4b37" or
     outputbodypixel == "#6a553e" or outputbodypixel == "#86623c" or
     outputbodypixel == "#8b4f0d" or outputbodypixel == "#7c3d14" or
     outputbodypixel == "#6a553d" …
Run Code Online (Sandbox Code Playgroud)

python multiple-conditions conditional-statements python-3.x

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

如何检查列表是否包含字符串

我需要遍历列表并检查值是字符串还是int.有没有简单的方法在python中执行此操作?

例如:

[1,2,3] 是真的.

["a",2,3] 会是假的.

python list

-3
推荐指数
1
解决办法
566
查看次数