Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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例程转换为函数_Python_Python 3.x - Fatal编程技术网

有没有一种方法可以;概括;将此python例程转换为函数

有没有一种方法可以;概括;将此python例程转换为函数,python,python-3.x,Python,Python 3.x,本质上,我希望根据列表中某个项的值的存在来执行switch语句,然后对该项(如果存在)执行计算。请注意do\u calculations\u on()和do\u something\u other\u on方法的区别 l=['no','maybe','yes'] item0=next(过滤器(lambda x:x='no',l),“默认值”) 如果项目0: 打印(第0项)#“否” 进行计算(第0项) 项=下一个(过滤器(λx:x==0,l),“-1”) 如果不是项目名称: 打印(项目名称)#-1

本质上,我希望根据列表中某个项的值的存在来执行switch语句,然后对该项(如果存在)执行计算。请注意
do\u calculations\u on()
do\u something\u other\u on
方法的区别

l=['no','maybe','yes']
item0=next(过滤器(lambda x:x='no',l),“默认值”)
如果项目0:
打印(第0项)#“否”
进行计算(第0项)
项=下一个(过滤器(λx:x==0,l),“-1”)
如果不是项目名称:
打印(项目名称)#-1
第2项=下一个(过滤器(λx:x='yes',l),“dft”)
如果是第2项:
打印(项目2)#“是”
做点别的(第2项)
看起来像(伪代码)的东西

编辑: 我想我已经把这个例子弄得比它本应该更复杂了

我给出了一个字典元素列表,这些元素的形式如下

d={'foo':foo_值,'bar':bar_值}

我得到了多个
d
元素作为列表,因此

[d1,d2,…]=。[{'foo':foo\u value1,'bar':bar\u value1},{'foo':foo\u value2,'bar':bar\u value2},…]

我想知道该列表中的字典中是否存在该值,如果存在,请使用对应于该值的dictionary元素

我尝试在列表上执行“第一个或默认值”,如:

found_d = next(filter(lambda x: x['bar'] == 'this_bar_value',[d1,d2,..] ), {})`

if found_d:
    do_something_to(found_d)
但是,当列表中的字典中存在其他“bar_值”时,我想做其他事情,并使用该字典执行其他计算。所以本质上我想概括如下:

found_d1 = next(filter(lambda x: x['bar'] == 'this_bar_value',[d1,d2,..] ), {})`

if found_d1:
    do_something_to(found_d)


another_d = next(filter(lambda x: x['bar'] == 'new_bar_value',[d1,d2,..] ), {})`

if another_d:
    do_another_thing(another_d)
差不多

def new_method(bar_value, list_of_dictionariess):
    if bar_value == 'this_bar_value':
         do_something_to(found_d)  # having trouble thinking about how to compare bar_value and using its corresponding dictionary
    elif bar_value == 'another_bar_value':
         do_another_thing(another_d)
下面的映射函数已关闭,因此看起来如下所示:

map(func, [d1,d2,...]) 

def func(d):
bar_value = d['bar']
    if bar_value == 'this_bar_value':
         do_something_to(found_d)  
    elif bar_value == 'another_bar_value':
         do_another_thing(another_d)
但问题是我想传递其他参数。我在这个函数中构建了(不是列表),所以看看local_var1和local_var2

local_var1 = 10
local_var2 = 12

map(func, [d1,d2,...]) 

def func(d):
    bar_value = d['bar']
    
    if bar_value == 'this_bar_value':
         do_something_to(d, local_var1)  
    elif bar_value == 'another_bar_value':
         do_another_thing(d, local_var2)

仍然不确定您想要什么,但您可以将伪代码包装到函数中,并在
map
中使用它:

l=['no','maybe','yes']
def func(x):
如果x==‘是’:
#做点什么
返回1
elif x==‘否’:
#做点什么
返回2
其他:
#做点什么
返回3
打印(列表(映射(函数,l)))
# -> [2, 3, 1]

根据问题更新进行编辑:

我认为字典列表的这种结构有点棘手。但是如果需要使用它,并且您只想从dict检索信息:

def新方法(目标值,字典列表):
#字典的子集列表到与输出匹配的单个字典
尝试:
d=[{k:v代表k,d.items()中的v,如果v==target_value},如果d.values()中的target_value,则d代表d字典列表中的d][0]
除:
打印(f'目标值“{target\u value}”不在dict值中。“)
一无所获
#条件句
如果目标值='bar\u value1':
#做点什么口述`
返回d
elif目标值=='条形图值2':
#做点什么口述`
返回d
其他:
返回“默认输出”
或者,如果您需要就地更新字典值

def新方法(目标值,字典列表):
#获取目标字典的列表索引和dict键
尝试:
idx,key=[(i,k)表示枚举中的i,d(字典列表)中的目标值,如果d中的目标值()表示k,则d中的v.items()表示v==目标值][0]
除:
打印(f'目标值“{target\u value}”不在dict值中。“)
一无所获
#条件句
如果目标值='bar\u value1':
#在适当的地方做点什么来记录字典的子集列表[idx][key]`
字典列表[idx][key]=“计算结果”
elif目标值=='条形图值2':
#在适当的地方做点什么来记录字典的子集列表[idx][key]`
字典列表[idx][key]=“计算结果”
其他:
返回“默认输出”
示例:

词典列表=[
{'foo':'foo_value1','bar':'bar_value1'},
{'foo':'foo_value2','bar':'bar_value2'}
]
新的\u方法(target\u value='bar\u value2',词典列表=词典列表)
#->{'bar':'bar_value2'}
新方法(target_value='foo_value1',词典列表=词典列表)
#->“默认输出”
新的\u方法(target\u value='inexistent\u value',字典列表=字典列表)
#->目标值“不存在”不在dict值中。
新的\u方法\u就位(目标\u值='bar\u值1',词典列表=词典列表)
词典列表
#->[{'foo':'foo_value1','bar':'result_of_calculation'},
#->{'foo':'foo_value2','bar':'bar_value2'}]

仍然不确定您想要什么,但您可以将伪代码包装到函数中,并在
映射中使用它:

l=['no','maybe','yes']
def func(x):
如果x==‘是’:
#做点什么
返回1
elif x==‘否’:
#做点什么
返回2
其他:
#做点什么
返回3
打印(列表(映射(函数,l)))
# -> [2, 3, 1]

根据问题更新进行编辑:

我认为字典列表的这种结构有点棘手。但是如果需要使用它,并且您只想从dict检索信息:

def新方法(目标值,字典列表):
#字典的子集列表到与输出匹配的单个字典
尝试:
d=[{k:v代表k,d.items()中的v,如果v==target_value},如果d.values()中的target_value,则d代表d字典列表中的d][0]
除:
打印(f'目标值“{target\u value}”不在dict值中。“)
一无所获
#条件句
如果目标值='bar\u value1':
#做点什么口述`
返回d
elif目标值=='条形图值2':
#做点什么口述`
返回d
其他:
返回“默认输出”
local_var1 = 10
local_var2 = 12

map(func, [d1,d2,...]) 

def func(d):
    bar_value = d['bar']
    
    if bar_value == 'this_bar_value':
         do_something_to(d, local_var1)  
    elif bar_value == 'another_bar_value':
         do_another_thing(d, local_var2)
def myswitch(item,target1,target2,fun1,fun2,default=None,key=None):
    key_item = key(item) if key else item
    if key_item == target1:
        return fun1(item)
    elif key_item == target2:
        return fun2(item)
    else:
        return default
>>> def do_something_to(x):
        print("doing something to",x )
        return 1

>>> def do_another_thing(x):
        print("doing another thing to",x)
        return 2

>>> list_of_dictionaries = [
    {'foo': 'foo_value1', 'bar': 'bar_value1'},
    {'foo': 'foo_value2', 'bar': 'bar_value2'},
    {'foo': 'foo_value3', 'bar': 'bar_value3'},
]
>>> [ myswitch(x,'bar_value1','bar_value2',do_something_to,do_another_thing, default=23, key=lambda y:y["bar"])
         for x in list_of_dictionaries]
doing something to {'foo': 'foo_value1', 'bar': 'bar_value1'}
doing another thing to {'foo': 'foo_value2', 'bar': 'bar_value2'}
[1, 2, 23]
>>> 
>>> myswitch_dict={'bar_value1':do_something_to, 'bar_value2':do_another_thing}
>>> [ myswitch_dict.get(x["bar"],lambda y:23)(x) for x in list_of_dictionaries]
doing something to {'foo': 'foo_value1', 'bar': 'bar_value1'}
doing another thing to {'foo': 'foo_value2', 'bar': 'bar_value2'}
[1, 2, 23]
>>>