Python​ 重解零基础100题(9)

简介: Python​ 重解零基础100题(9)

第81题


问题:请写一个程序来随机打印一个7到15之间的整数(包括15)。

提示:使用random.randrange()到一个给定范围内的随机整数。

1. import random
2. print(random.randrange(7,16))



我的答案:

>>> round(r.random()*(15-7))+7
11
>>> r.randint(7,15)
13
>>> r.randrange(7,16)
11
>>> r.choice([*range(7,16)])
8
>>> r.sample([*range(7,16)],1)[0]
12
>>> r.choices([*range(7,16)])[0]
13



第82题


问题:请编写一个程序来压缩和解压字符串"hello world!hello world!hello world!hello world!"。

提示:使用zlib.compress()和zlib.decompress()来压缩和解压缩字符串。

import zlib
s = b'hello world!hello world!hello world!hello world!'
t = zlib.compress(s)
print(t)
print(zlib.decompress(t))

暂无



第83题

问题:请编写一个程序打印100次“1+1”执行的运行时间。

提示:使用timeit()函数测量运行时间。

1. from timeit import Timer
2. t = Timer("for i in range(100):1+1")
3. print(t.timeit())


我的答案:尝试一下装饰器

def timer(func):
  def wrapper(*args,**kwargs):
    from time import time
    time_start = time()
    result = func(*args,**kwargs)
    time_spent = time() - time_start
    print('\nSeconds:',round(time_spent,2))
    return result
  return wrapper
>>> @timer
def test():
  for i in range(100):
    print('1+1',end='\t')
>>> test()
1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 
1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 
1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 
1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 
1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 1+1 
Seconds: 4.91
>>> 



第84题


问题:请编写一个程序洗牌和打印列表[3,6,7,8]。

提示:使用shuffle()函数洗牌列表。

1. from random import shuffle
2. li = [3,6,7,8]
3. shuffle(li)
4. print(li)

同上




第85题


问题:请编写一个程序洗牌和打印列表[3,6,7,8]。

提示:使用shuffle()函数洗牌列表。

1. from random import shuffle
2. li = [3,6,7,8]
3. shuffle(li)
4. print(li)

同上(为毛一样)




第86题


问题:请编写一个程序,生成主语在 ["I", "You"],动词在["Play", "Love"]中,对象在["Hockey","Football"]中的所有句子.

提示:使用list[index]表示法从列表中获取元素。

subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
for i in range(len(subjects)):
    for j in range(len(verbs)):
        for k in range(len(objects)):
            sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
            print(sentence)



我的答案:

>>> s,v,b=["I","You"],["Play","Love"],["Hockey","Football"]
>>> print('\n'.join(([' '.join([i,j,k]) for i in s for j in v for k in b])))
I Play Hockey
I Play Football
I Love Hockey
I Love Football
You Play Hockey
You Play Football
You Love Hockey
You Love Football
>>> 




第87题


问题:请写一个程序打印列表,删除偶数后打印[5,6,77,45,22,12,24]。

提示使用列表理解从列表中删除一组元素。

1. li = [5,6,77,45,22,12,24]
2. li = [x for x in li if x%2!=0]
3. print(li)

同上




第88题


问题:使用列表理解,请编写程序,删除[12,24,35,70,88,120,155]中可被5和7整除的删除数后,打印列表。

提示:使用列表理解从列表中删除一组元素。

1. li = [12,24,35,70,88,120,155]
2. li = [x for x in li if x%5!=0 and x%7!=0]
3. print(li)

我的答案: 5,7互质,可简化为: [x for x in li if x%5*7!=0]



第89题

问题:使用列表理解法,请编写一个程序,去掉[12,24,35,70,88,120,155]中的第0,2,4,6位置上的元素后打印列表。

提示:使用列表理解从列表中删除一组元素。使用enumerate()来获取(索引,值)元组。


li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print(li)

我的答案:用切片最简单: li[1::2]




第90题

问题:使用列表理解,编写一个程序生成一个358三维数组,每个元素为0。

提示:使用列表理解来创建数组。

 

array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
print(array)
目录
相关文章
|
存储 索引 Python
Python​ 重解零基础100题(10-2)
Python​ 重解零基础100题(10-2)
74 0
|
存储 Python
Python​ 重解零基础100题(10)
Python​ 重解零基础100题(10)
83 0
|
索引 Python
Python​ 重解零基础100题(8)
Python​ 重解零基础100题(8)
92 0
|
Python
Python​ 重解零基础100题(7)
Python​ 重解零基础100题(7)
127 0
|
Python
Python​ 重解零基础100题(6)
Python​ 重解零基础100题(6)
74 0
|
Python
Python​ 重解零基础100题(5)
Python​ 重解零基础100题(5)
71 0
|
Python
Python​ 重解零基础100题(4)
Python​ 重解零基础100题(4)
93 0
|
机器人 Python
Python​ 重解零基础100题(3)
Python​ 重解零基础100题(3)
123 0
|
JSON 数据安全/隐私保护 数据格式
Python​ 重解零基础100题(2)
Python​ 重解零基础100题(2)
235 0
|
Python 容器
Python​ 重解零基础100题(1)
Python​ 重解零基础100题(1)
142 0