"或" Python中的条件

时间:2018-03-12 15:33:26

标签: python conditional-statements logical-operators

我想检查一个值是否为-1或小于另一个值。

为了做到这一点,我做了以下事情:

"ADFS-amtest-ro","pol-amtest-ro"
"adfs-host-role","pol-amtest-ro"
"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier"
"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkMulticontainerDocker"
"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWorkerTier"
"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth"
"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkService"
"AWSAccCorpAdmin","AdministratorAccess"
"AWScompanyCorpAdmin","AdministratorAccess"
"AWScompanyCorpPowerUser","PowerUserAccess"
"AWSServiceRoleForAutoScaling","AutoScalingServiceRolePolicy"
"AWSServiceRoleForElasticBeanstalk","AWSElasticBeanstalkServiceRolePolicy"
"AWSServiceRoleForElasticLoadBalancing","AWSElasticLoadBalancingServiceRolePolicy"
"AWSServiceRoleForOrganizations","AWSOrganizationsServiceTrustPolicy"
"AWSServiceRoleForRDS","AmazonRDSServiceRolePolicy"
"Cloudyn","ReadOnlyAccess"
"DatadogAWSIntegrationRole","DatadogAWSIntegrationPolicy"
"datadog_alert_metrics_role","AWSLambdaBasicExecutionRole-66abe1f2-cee8-4a90-a026-061b24db1b02"
"dev-instance-role","dev-instance-role-policy"
"ecsInstanceRole","AmazonEC2ContainerServiceforEC2Role"
"ecsServiceRole","AmazonEC2ContainerServiceRole"
"companySAMLUser","AdministratorAccess"
"irole-matlabscheduler","pol-marketdata-rw"
"lambda-ec2-ami-role","lambda-ec2-ami-policy"
"lambda_api_gateway_twilio_processor","AWSLambdaBasicExecutionRole-f47a6b57-b716-4740-b2c6-a02fa6480153"
"lambda_api_gateway_twilio_processor","AWSLambdaSNSPublishPolicyExecutionRole-d31a9f16-80e7-47c9-868a-f162396cccf6"
"OneLoginAdmin","AdministratorAccess"
"OneLoginDev","PowerUserAccess"
"rds-monitoring-role","AmazonRDSEnhancedMonitoringRole"
"role-amtest-ro","pol-amtest-ro"
...

要注意: def isValidWord(word, hand, wordList): """ Returns True if word is in the wordList and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or wordList. word: string hand: dictionary (string -> int) wordList: list of lowercase strings """ if word not in wordList: return False for k, v in getFrequencyDict(word).items(): if hand.get(k, -1) < v or hand.get(k, -1) == -1: return False return True 会返回字母字典及字母的频率。

剩下的问题是:我是否在下一行中犯了错误?

getFrequencyDict(word)

4 个答案:

答案 0 :(得分:1)

如果您知道dict中的所有值都是正数,则检查

if hand.get(k, -1) < v:

就够了。

在更通用的情况下,这似乎是使用or的正确方法(您的代码清楚地表明您检查了某些内容,作为默认值)。

答案 1 :(得分:0)

在此处使用.*作为默认设置似乎是多余的,并且不允许概括为-1包含负值的情况。

hand

或者,即使if hand.get(k) is None or hand.get(k) < v: ... 也可以被认为是多余的。要检查存在的密钥,您只需使用get

in

答案 2 :(得分:0)

只需使用设置即可。此处不需要字典设置支持差异操作。

def isValidWord(word, hand, wordList):
    """
    Returns True if word is in the wordList and is entirely
    composed of letters in the hand. Otherwise, returns False.

    Does not mutate hand or wordList.

    word: string
    hand: set of letters
          eg: set(["u", "s", "a"] or set("usa")
    wordList: set of strings
          eg: set(["zeus", "osiris", "thor"])
    """
    if word not in wordList:
        return False
    if len(set(word) - hand) != 0:
        return False
    return True

答案 3 :(得分:-2)

public Model SelectedModel
{
    get { return mSelectedModel; }
    set
    {
        if (mSelectedModel != null)
            mSelectedModel.PropertyChanged -= OnSelectedModelPropertyChanged;
        SetProperty(ref mSelectedModel, value);
        if (mSelectedModel != null)
            mSelectedModel.PropertyChanged += OnSelectedModelPropertyChanged;
        NotifyPropertyChanged(nameof(IsSelectedModelChecked));
    }
}

private void OnSelectedModelPropertyChanged( object sender, PropertyChangedEventArgs args )
{
    if (args.PropertyName == nameof(Model.IsChecked))
        NotifyPropertyChanged(nameof(IsSelectedModelChecked));
}

这可以用上面给出的方式编写。(注意它与你的问题不完全相同,只是想法)