pythonif循环-创新互联
x = 'spam'
while x:
print(x, end=' ')
x = x[1:] # spam pam am m
break 跳出最近所在的循环
continue 跳到所在循环的开头处
pass 啥都不做
def func():
pass
def func():
...
continue 跳到所在循环的开头处
x = 10
while x:
x = x-1
if x % 2 ==0:continue
print(x, end=' ') # 9 7 5 3 1
#判断是不是质数
#x = y //2
#while x > 1:
if y % x == 0:
print(y, 'has factor', x)
break
x -= 1
#else:
print(y, 'is prime')
for i in 'spam':
print(i, end='') # spam
sum = 0
for i in [1, 2, 3]:
sum += i
print(sum) # 6
sum = 1
for i in [1, 2, 3, 4]:sum *= i
print(sum) # 24
#for循环中的元组赋值和解包
for (i, j) in ((1, 2),(3, 4)): # 1 2
print(i, j) # 3 4
d = {'a':1,'b':2,'c':3} # 'a' 1
for i in d: # 'b' 2
print(i, d[i]) # 'c' 3
for (i, j) in d.items(): # 'a' 1
print(i, j) # 'b' 2
'c' 3
t = [(1, 2), (3, 4), (5, 6)]
for both in t: # 1, 2
a, b = both # 3, 4
print(a, b) # 5, 6
for ((a, b), c) in [((1, 2),3), ((4, 5), 6)]: # 1 2 3
print(a, b, c) # 4 5 6
for ((a, b), c) in [((1, 2),3), ('xy', 6)]: # 1 2 3
print(a, b, c) # x y 6
a, *b, c = (1, 2, 3, 4)
print(a, b, c) # 1 [2, 3] 4
for (a, *b, c) in [(1, 2, 3, 4), (5, 6, 7, 8)]:
print(a, b, c)
1 [2, 3] 4
5 [6, 7] 8
items = ['aaa', 111, (4, 5), 2.01]
tests = [(4, 5), 3.14]
for key in tests:
for item in items:
if item == key:
print(key, 'was found')
break
else:
print(key, 'not found')
(4, 5) was found
3.14 not found
for key in tests:
if key in items:
print(key, 'was found')
else:
print(key, 'not found')
(4, 5) was found
3.14 not found
#编写循环技巧
for比while容易些,执行也比较快。
#灵活使用range, range在py3 中是迭代器
print(range(5)) # range(0, 5)
print(list(range(5))) # [0, 1, 2, 3, 4]
l = [1, 2, 3, 4, 5]
for x in l:
x += 1
print(l) # [1, 2, 3, 4, 5]
这或许和你想象的不太一样,因为修改的是循环变量x, 而不是列表。
#每次经过循环时, x会引用从列表中取出来的下一个整数。
比如第一轮迭代 x是1, 下次一次迭代, 循环主体把x设为不同的对象,
#也就是2, 但是没更新1所来自的那个列表
l = [1, 2, 3, 4, 5]
for i in range(len(l)):
l[i] += 1
print(l) # [2, 3, 4, 5, 6]
更简便的便是用列表解析, 并且运行的更快
print([i+1 for i in [1, 2, 3, 4, 5]])
[2, 3, 4, 5, 6]
#并行遍历,zip 和 map
zip会取得一个或多个序列为参数,然后返回元组的列表。将这些序列中的并排的
#元素配成对
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
print(zip(l1, l2)) #
print(list(zip(l1, l2))) # [(1, 5), (2, 6), (3, 7), (4, 8)]
for (x, y) in zip(l1, l2):
print(x, y, '-->', x+y)
1 5 --> 6
2 6 --> 8
3 7 --> 10
4 8 --> 12
s1 = 'abc'
s2 = 'xyz123'
#print(list(map(None, s1, s2)))
python2 中运行将会为较短的序列用None来补齐
[('a', 'x'), ('b', 'y'), ('c', 'z'), (None, '1'), (None, '2'), (None, '3')]
print(list(map(ord, 'spam'))) # [115, 112, 97, 109]
#用zip构造字典
k = ['spam', 'eggs', 'toast']
v = [1, 3, 5]
print(list(zip(k, v)))
d = {}
[('spam', 1), ('eggs', 3), ('toast', 5)]
for i, j in zip(k, v):
d[i] = j
print(d)
{'spam': 1, 'eggs': 3, 'toast': 5}
其实也可以用zip来创建字典
k = ['spam', 'eggs', 'toast']
v = [1, 3, 5]
d = dict(zip(k, v))
print(d)
{'spam': 1, 'eggs': 3, 'toast': 5}
用enumerate来产生偏移和元素
s = 'spam'
for i, j in enumerate(s):
print(i,'-->',j)
0 --> s
1 --> p
2 --> a
3 --> m
enumerate函数返回一个生成器对象,可以通过next调用。
e = enumerate('spam')
print(e)
#这个对象有个next方法,可以在每次迭代的时候返回一个元组,
#可以在for中循环通过元组赋值将运算解包
print(next(e))
print(next(e))
print(next(e))
(0, 's')
(1, 'p')
(2, 'a')
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文名称:pythonif循环-创新互联
文章位置:http://ybzwz.com/article/dpoegp.html