RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
python表存放函数,python列表存储对象

python现定义一个函数,使传入列表a中每个元素加三,将计算结果存放到新的列表中,并返回新的列表

def function(l):

成都创新互联始终坚持【策划先行,效果至上】的经营理念,通过多达十载累计超上千家客户的网站建设总结了一套系统有效的营销解决方案,现已广泛运用于各行各业的客户,其中包括:成都广告制作等企业,备受客户赞扬。

return [a + 3 for a in l]

思路就是使用一个列表推导来实现循环,让每一个元素都加3.

python 字典可以储存函数吗

Python中是没有switch的, 所以有时我们需要用switch的用法, 就只能通过if else来实现了. 但if else写起来比较冗长,

这时就可以使用Python中的dict来实现, 比switch还要简洁. 用法如下:

如果是key1的情况就执行func1, 如果是key2的情况就执行func2...(func1, func2...所有的函数的参数形式需要相同),

假设各个函数参数均为(arg1, arg2):

dictName = {"key1":func1, "key2":func2, "key3":func3"...}#字典的值直接是函数的名字,不能加引号dictName[key](arg1, arg2)

示例代码如下:

#!/usr/bin/python#File: switchDict.py#Author: lxw#Time: 2014/10/05import redef add(x, y): return x + ydef sub(x, y): return x - ydef mul(x, y): return x * ydef div(x, y): return x / ydef main():

inStr = raw_input("Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.\n")

inList = re.split("(\W+)", inStr)

inList[1] = inList[1].strip() print("-------------------------") print(inList) print("-------------------------") #Method 1:

if inList[1] == "+": print(add(int(inList[0]), int(inList[2]))) elif inList[1] == "-": print(sub(int(inList[0]), int(inList[2]))) elif inList[1] == "*": print(mul(int(inList[0]), int(inList[2]))) elif inList[1] == "/": print(div(int(inList[0]), int(inList[2]))) else: pass

#Method 2:

try:

operator = {"+":add, "-":sub, "*":mul, "/":div} print(operator[inList[1]](int(inList[0]), int(inList[2]))) except KeyError: passif __name__ == '__main__':

main()

Output:

PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 + 2

-------------------------['1', '+', '2']-------------------------

3

3PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.4 - 9

-------------------------['4', '-', '9']-------------------------

-5

-5PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.6 / 5

-------------------------['6', '/', '5']-------------------------

1

1PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 9 9

-------------------------['1', '', '9', ' ', '9']-------------------------PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 ( 9

-------------------------['1', '(', '9']-------------------------PS J:\

个人感觉, 如果想用switch来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有

相同的参数), 就可以用这种方法来实现.

python常用列表函数

1

len(list)

列表元素个数

2

max(list)

返回列表元素最大值

3

min(list)

返回列表元素最小值

4

list(seq)

将元组转换为列表

序号

方法

1

list.append(obj)

在列表末尾添加新的对象

2

list.count(obj)

统计某个元素在列表中出现的次数

3

list.extend(seq)

在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

4

list.index(obj)

从列表中找出某个值第一个匹配项的索引位置

5

list.insert(index, obj)

将对象插入列表

6

list.pop([index=-1])

移除列表中的一个元素(默认最后一个元素),并且返回该元素的值

7

list.remove(obj)

移除列表中某个值的第一个匹配项

8

list.reverse()

反向列表中元素

9

list.sort( key=None, reverse=False)

对原列表进行排序

10

list.clear()

清空列表

11

list.copy()

复制列表


当前文章:python表存放函数,python列表存储对象
新闻来源:http://scyingshan.cn/article/hspcjj.html