2 Star 1 Fork 2

NuLLLand ZHANG/python数学实验与建模

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
list-opration.py 1.95 KB
一键复制 编辑 原始数据 按行查看 历史
ZHANG HAOHAN 提交于 2021-03-27 11:10 +08:00 . chapter1 done
from functools import reduce
# list
example_list = list(range(0, 10))
# add multi elements to back of list
example_list.extend(list(range(10, 20)))
print(example_list)
# insert one element in target index
example_list.insert(0, -1) # insert(index: int, element: obj)
print(example_list)
# clear the whole list
example_list.clear()
print(example_list)
# tuple
example_tuple = (1, 2, 3, 4, 5,)
single_tuple = 1, # end with comma indicates it is a tuple
# operation is similar to list
# dict
example_dict = {'name': 'Zhou Kai', 'age': 45, 'University': 'ZJUT'}
# set
example_set = {1, 2, 2, 3, 4, 5}
print(example_set)
a = set('helloworld')
print(a) # remove the rapid element and unsorted(maybe sorted is okay)
# lambda function: single line function
funtion = lambda x, y: abs(x) + y ** 3
print(funtion(1, 2))
# map() function map(fun: function, Iterable) “map()方法会将 一个函数 映射到序列的每一个元素上,生成新序列,包含所有函数返回值。” from 知乎
a = list(range(0, 10))
print(list(map(str, a)))
print(list(map(lambda x: x ** 2, a)))
# function reduce() in functiontools
print(reduce(lambda x, y: x + y, a)) # return sum of list a
# function filter() "序列中的每个元素作为参数传递给函数进行判断,返回True或者False,最后将返回True的元素放到新列表中。" from CSDN
print(list(filter(lambda x: x % 2 == 0, a))) # print odd number in list a
# function zip() "zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。" from RUNOOB.COM
print(list(zip(a, [1, 2, 3])))
# function enumerate() "python中的enumerate()函数用于将一个可便利的数据对象(如列表、元组合字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。" from CSDN
for i, v in enumerate(a):
print(i, v)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nullland-zhang/python-mathematical-and-experiment-modeling.git
git@gitee.com:nullland-zhang/python-mathematical-and-experiment-modeling.git
nullland-zhang
python-mathematical-and-experiment-modeling
python数学实验与建模
master

搜索帮助