python中a.any()以及&&,||

OO~ posted @ 2013年5月26日 20:26 in python , 16018 阅读

  首先说明一下,在python中是没有&&及||这两个运算符的,取而代之的是英文and和or。其他运算符没有变动。

  接着重点要说明的是python中的a.any(),我之所以会涉及到这个函数,是因为我在设计某个分段数值函数时由于不细心犯的错误引起的。a.any(),根据官方解释:

  Test whether any elements of an array evaluate to True along an axis.

即针对的是一个list,判断list中的某个元素是否为真,此时只要有一个元素为真,list元素全部输出。下面一个例子更直观的做了解释:

#!/usr/bin/python
#coding=utf8

import numpy as np

def f(data):
    for val in data:
        if val <= 0.6:
            r1 = val*1
            print "this is <= 0.6 numbers", r1
        if val > 4:
            r1 = val*1
            print "this is > 4 numbers ", r1

def main():
    data = np.arange(0.1, 5.1, 0.5)
    if np.any(data > 0.3) and np.any(data <= 1):
        r1 = data * 1
    print r1
    f(data)

if __name__=='__main__':
    main()

  对应的输出是:

[ 0.1  0.6  1.1  1.6  2.1  2.6  3.1  3.6  4.1  4.6]
this is <= 0.6 numbers 0.1
this is <= 0.6 numbers 0.6
this is > 4 numbers  4.1
this is > 4 numbers  4.6

  在a.any()中,只要满足条件,整个list就是true的。而我在分段函数计算时,恰恰使用到了a.any()来判断list的范围,在后面的计算中,其实是整个list同时运算然后赋值了,也就将分段函数结果简化成了某一段的所有值。正确的计算分段函数有两中策略,要么像上面的函数f(),要么将list分段带入运算。

  这个问题我纠结了一个晚上,最后才发现是a.any()的问题。最开始使用的时候,其真正的用法没有具体熟悉,同时我也该反省我自己,对python语言掌握的实在太菜了!

  路漫漫其修远兮,吾将上下而求索~

Avatar_small
OO~ 说:
2013年5月26日 20:32

跪求推荐牛人博客,学习python,敦促自己,得到提高

Avatar_small
依云 说:
2013年5月27日 00:18

这个不应该属于 numpy 的使用么?

Avatar_small
OO~ 说:
2013年5月27日 18:10

是啊,range貌似不太支持小数吧!PS:这个问题就是我在用matplotlib绘图时,遇到的!

Avatar_small
carpet cleaning duba 说:
2019年9月14日 22:03

Expert cleaners are been trained in using cleaning items, some of which may be very bad for your health insurance and well-being in the event that used incorrectly. Using a skilled office cleansing service may ensure the correct use associated with cleaning chemical substances around your company environment, in addition to remove the necessity to store all of them on website.

Avatar_small
nanny services dubai 说:
2020年7月06日 21:23

Getting some sort of nanny can certainly appreciably reduce the pressure connected with moms and dads, exactly who also have to give the perfect time to the task. Doing the job moms and dads can certainly make use of nannies to address the little ones in the instance of disaster gatherings on the practices, or maybe in the event extensive company time come to be important. Moms and dads may correctly take care of the time period seeing that the nanny may perhaps handle the youngster's regular regime like arranging dinner with regards to little ones, having these individuals intended for classes, taking the meal, appealing these individuals with fruitful things to do, for example..

Avatar_small
nanny services in du 说:
2021年9月02日 16:18

Octoclean is known as a commercial maintaining franchise which is in business in the past 18 years. During the last five quite a few years, it contains experienced an important 30% grow in yucky, giving the application brand-name recognition in the uk. This store-bought cleaning operation offers maintaining and protection services to help you businesses in all sizes. These have a pretty low early cash investment for one commercial maintaining franchise. Being a Octoclean franchisee, you'll get training in running this sort of type in business plus marketing and even financial services.

Avatar_small
AP 10th Civics Model 说:
2022年9月18日 02:05

Department of Education and Secondary Education Board has designed the AP SSC Civics Model Paper 2023 Pdf with answers for Telugu Medium, English Medium & Urdu Medium Students of the State Board. Every year there are a huge number of teaching staff and educational portals of the state have suggested the practice question bank with revision questions for both medium students of the board. AP 10th Civics Model Paper In civics, students learn to contribute to public processes and discussions of real issues. Students can also learn civic practices such as voting, volunteering, jury service, and joining with others to improve society. Civics enables students not only to study how others participate but also to practice participating and taking informed action themselves.

Avatar_small
Alyssa 说:
2023年1月21日 19:34

Understanding the difference between && and || and the a.any() function in python is essential for avoiding careless mistakes. The a.any() function tests whether any element of an array evaluates to True along cbd oil extraction methods an axis. This means that, for a list, it checks if any element is true. If one element is true, the entire list is output. This is an incredibly useful function that can save a lot of time and make code much more efficient.

Avatar_small
monthly cleaning ser 说:
2023年8月16日 18:57

For you to win additional estimates, establish as part of your client's minds that they'll receive less expensive from anyone. The approach to sell more should be to educate customers. Selling can be education, and educating customers will ensure you get more buyers. Does you believe you give them approximately when one does work for the children.

Avatar_small
seo service london 说:
2024年2月22日 16:29

Excellent blog here! Also your site a lot up fast! What web host are you the usage of? Can I get your affiliate link in your host? I wish my site loaded up as quickly as yours lo


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter