代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。