Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python dict键作为语句_Python_Python 3.x_Dictionary_Conditional - Fatal编程技术网

Python dict键作为语句

Python dict键作为语句,python,python-3.x,dictionary,conditional,Python,Python 3.x,Dictionary,Conditional,我想知道是否可能发生以下情况: rarity = {>= 75: 'Common', <= 20 : 'Rare', >= 5: 'Legendary'} 稀有性={>=75:'Common',=5:'Legendary'} 在Python 2.7中,这将引发语法错误。这感觉像是滥用了字典(键值存储)的概念。也许您应该重新编写代码,并且可以使用'Common','ravel'作为键,将值作为范围,即范围(5,20),范围(20),等等。在Python 2.7中,这将引发语法错

我想知道是否可能发生以下情况:

rarity = {>= 75: 'Common', <= 20 : 'Rare', >= 5: 'Legendary'}
稀有性={>=75:'Common',=5:'Legendary'}

在Python 2.7中,这将引发语法错误。这感觉像是滥用了字典(键值存储)的概念。也许您应该重新编写代码,并且可以使用
'Common'
'ravel'
作为键,将值作为范围,即
范围(5,20)
范围(20)
,等等。在Python 2.7中,这将引发语法错误。这感觉像是滥用了字典(键值存储)的概念。也许你应该修改你的代码,你可以使用
'Common'
'ravel'
作为键,值作为范围,例如
范围(5,20)
范围(20)
,等等。

这不能用python中的
dict
来完成。您的任务可能需要一个普通函数:

def check(x):
    if x >= 75:
        return 'Common'
    if x <= 20:
        ...
def检查(x):
如果x>=75:
返回“普通”

如果x无法使用python中的
dict
完成此操作。您的任务可能需要一个普通函数:

def check(x):
    if x >= 75:
        return 'Common'
    if x <= 20:
        ...
def检查(x):
如果x>=75:
返回“普通”

如果x我找不到比O(k)性能更好的方法,其中
k
是您的dict类型中的键数

如果您不是在寻求
dict
的O(1)性能,而只是想要类似
dict
的语法,那么您可以自己实现映射对象,如下所示:

from collections.abc import Mapping

class CallDict(Mapping):
    def __init__(self, *pairs):
        self._pairs = pairs
    def __iter__(self):
        return iter(())
    def __len__(self):
        return len(self._pairs)
    def __getitem__(self, x):
        for func, value in self._pairs:
            if func(x):
                return value
        raise KeyError("{} satisfies no condition".format(x))

# Conditions copied directly from OP, but probably wrong.
cd = CallDict(
    ((lambda x: x >= 75), "Common"),
    ((lambda x: x <= 20), "Rare"),
    ((lambda x: x >= 5), "Legendary"),
)


assert cd[1] == 'Rare'
assert cd[10] == 'Rare'
assert cd[50] == 'Legendary'
assert cd[100] == 'Common'
from collections.abc导入映射
类CallDict(映射):
定义初始化(自,*对):
self.\u pairs=对
定义(自我):
返回iter(())
定义(自我):
回程透镜(自对)
定义获取项目(self,x):
对于func,以self.\u对表示的值:
如果func(x):
返回值
raise KeyError(“{}不满足任何条件”。格式(x))
#直接从OP复制的条件,但可能是错误的。
cd=CallDict(
((λx:x>=75),“普通”),
((λx:x=5),“传奇”),
)
断言cd[1]=“稀有”
断言cd[10]=“稀有”
断言cd[50]=“传奇”
断言cd[100]=“通用”

我找不到比O(k)性能更好的方法,其中
k
是您的指令中的键数

如果您不是在寻求
dict
的O(1)性能,而只是想要类似
dict
的语法,那么您可以自己实现映射对象,如下所示:

from collections.abc import Mapping

class CallDict(Mapping):
    def __init__(self, *pairs):
        self._pairs = pairs
    def __iter__(self):
        return iter(())
    def __len__(self):
        return len(self._pairs)
    def __getitem__(self, x):
        for func, value in self._pairs:
            if func(x):
                return value
        raise KeyError("{} satisfies no condition".format(x))

# Conditions copied directly from OP, but probably wrong.
cd = CallDict(
    ((lambda x: x >= 75), "Common"),
    ((lambda x: x <= 20), "Rare"),
    ((lambda x: x >= 5), "Legendary"),
)


assert cd[1] == 'Rare'
assert cd[10] == 'Rare'
assert cd[50] == 'Legendary'
assert cd[100] == 'Common'
from collections.abc导入映射
类CallDict(映射):
定义初始化(自,*对):
self.\u pairs=对
定义(自我):
返回iter(())
定义(自我):
回程透镜(自对)
定义获取项目(self,x):
对于func,以self.\u对表示的值:
如果func(x):
返回值
raise KeyError(“{}不满足任何条件”。格式(x))
#直接从OP复制的条件,但可能是错误的。
cd=CallDict(
((λx:x>=75),“普通”),
((λx:x=5),“传奇”),
)
断言cd[1]=“稀有”
断言cd[10]=“稀有”
断言cd[50]=“传奇”
断言cd[100]=“通用”

我想你可能在找一个枚举我可以猜出你想在这里做什么,但请再详细解释一下。关于数字>=5,这里的问题是什么?可能是重复的我想你可能在找一个枚举我可以猜出你想在这里做什么,但是请再详细解释一下。关于数字>=5,这里的问题是什么?问题的可能重复部分非常不清楚,但有一些方法可以使用这样的dict,例如
x=6;{x>=75:'Common',x=5:'Legendary'}[True]
@Chris_Rands除了OP犯了一个错误外,它应该是
x>=20
,如果你在代码中使用它,你只会看到返回最后一个True条件。因此,您的代码将因
x=25
@anishtain4而失败。我们只能猜测问题是关于什么的,但假设一个整数输入为一个输出(在Ivan的代码中也是假设的),这可以在我展示的dict中完成above@Chris_Rands是的,这是一个写得很差的问题。然而很明显,OP正在寻找一种简短的切换形式。一般来说,我看不出有任何理由使用switch命令,
如果。。。elif
也做同样的工作。这个问题很不清楚,但有一些方法可以使用这样的dict,例如
x=6;{x>=75:'Common',x=5:'Legendary'}[True]
@Chris_Rands除了OP犯了一个错误外,它应该是
x>=20
,如果你在代码中使用它,你只会看到返回最后一个True条件。因此,您的代码将因
x=25
@anishtain4而失败。我们只能猜测问题是关于什么的,但假设一个整数输入为一个输出(在Ivan的代码中也是假设的),这可以在我展示的dict中完成above@Chris_Rands是的,这是一个写得很差的问题。然而很明显,OP正在寻找一种简短的切换形式。一般来说,我看不出有任何理由使用switch命令,
如果。。。elif
也做同样的工作。请注意,我并不主张为这种特殊情况创建
映射
对象。对于这样一个简单的例子,我更愿意看到
rarity=HowRareIsIt(10)
。但是知道可以创建映射对象对于其他类似的情况可能很有用。请注意,我并不主张为这个特殊情况创建
mapping
对象。对于这样一个简单的例子,我更愿意看到
rarity=HowRareIsIt(10)
。但是知道可以创建映射对象可能对其他类似情况有用。