plain_list = []
arguments="asdfghjkl"
for i in arguments:
for j in i:
plain_list.append(j)
print(plain_list)
#['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']
L1= list(arguments)
print(L1)
#['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']
#squares = [x**2 for x in range(10)]
squares = []
for x in range(10):
squares.append(x**2)
print(squares)
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
str1 = list(map(lambda x: x**2, range(10)))
print(str1)
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
#[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
combs = []
for x in [1,2,3]:
for y in [3,1,4]:
if x != y:
combs.append((x, y))
print(combs)
#[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
a = [1,2,3]
b = [4,5,6]
#使用zip()函数来可以把列表合并,并创建一个元组对的列表。
print(list(zip(a,b))) #[(1, 4), (2, 5), (3, 6)]
#zip()方法用在for循环中,就会支持并行迭代:
for (x,y) in zip(a,b):
print(x,y,'--',x*y)
#python内置range()函数的作用是什么?它能返回一系列连续增加的整数,它的工作方式类似于分片,可以生成一个列表对象。
#range函数大多数时常出现在for循环中,在for循环中可做为索引使用。
#其实它也可以出现在任何需要整数列表的环境中,在python 3.0中range函数是一个迭代器。
#range()函数内可以填入三个参数,第三个参数是步进值(步进值默认为1):
for i in range(0,10,3):
print(i,end=' ') #输出: 0 3 6 9
还没有评论,来说两句吧...