python缩进和二维列表的解包
这两天博主遇见了一道题目 如下:现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和。
输入
第一行给出整数M(0<M<10)代表多少组测试数据
每组测试数据第一行给你N,代表该组测试数据的数量。
接下来的N个数为要测试的数据,每个数小于1000
输出
每组测试数据结果占一行,输出给出的测试数据的所有素数和
输入
3
5
1 2 3 4 5
8
11 12 13 14 15 16 17 18
10
21 22 23 24 25 26 27 28 29 30
输出
10
41
52
博主在写的过程中是这么想的 既然要素数求和 那就要先求素数 用一些代码求素数(1不是素数)
然后进行第二步 规范输入输出
这里的多组输入我选择用map直接给列表a传参 这个方法我是从学长那学到的 。传参,筛选出素数后求和 把最后结果当成一个列表存进大列表中 。这里博主想用一个二维列表 ,这样就可以存储上多组数据并一起输出, 最后用解包的办法把二维列表里的一维列表遍历出来 。
(我对于解包了解不是太全面 欢迎大神补充 )
这是我第一次的代码
from math import sqrt
m = 0
allnum = []
M = int(input())
a = []# 进来的所有数
while M > 0:
M -= 1
N = int(input())
a = map(int, input().split())
b = []# 每一组求出的素数
for r in a:
if r == 1:
<span class="token keyword">continue</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token function">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token function">int</span><span class="token punctuation">(</span><span class="token function">sqrt</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> r <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">break</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
b<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>allnum.append()for r in allnum: print(r) 但是我在这里遇见了一个问题 我在测试过程中 先一组一组测试 发现没有问题 当我两组一起测试时发现 二维列表里只有一组数据 就是最后一组数据 第一组数据被自动覆盖了 我以为是存储这些数据的b列表位置的错误 但我更改b列表以后还是无法达到题目格式输出 。最后我做了如图调整
from math import sqrt
m = 0
allnum = []
M = int(input())
a = []# 进来的所有数
while M > 0:
M -= 1
N = int(input())
a = map(int, input().split())
b = []# 每一组求出的素数
for r in a:
<span class="token keyword">if</span> r <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
<span class="token keyword">continue</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token function">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token function">int</span><span class="token punctuation">(</span><span class="token function">sqrt</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> r <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">break</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
b<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>
allnum<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token function">sum</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span>for r in allnum: print(r) 再测试时 已经可以格式化输出了 。
这里,在素数求和时我对代码进行了一个优化,引入了数学开平方根的方法
总结 :
1.之前只听python缩进严格 这是我第一次亲身经历 python缩进不同 结果不同 这是第一次让我切身体会到python缩进的严格 以后一定会多加注意
2.其实这里可以不用多组输出直接计算完毕后输出也可以 我这么做有些多此一举了 当时以为必须最后输入完毕才允许输出 其实改成如下也可以通过
from math import sqrt
m = 0
allnum = []
M = int(input())
a = []# 进来的所有数
while M > 0:
M -= 1
N = int(input())
a = map(int, input().split())
b = []# 每一组求出的素数
for r in a:
<span class="token keyword">if</span> r <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
<span class="token keyword">continue</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token function">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token function">int</span><span class="token punctuation">(</span><span class="token function">sqrt</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> r <span class="token operator">%</span> i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token keyword">break</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
b<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span>
sumnum<span class="token operator">=</span><span class="token function">sum</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span>sumnum<span class="token punctuation">)</span>
但是很尴尬 优化代码的目的就是减少时间或者空间或者两者 但是这样改了之后 时间空间都增多了一丢丢 不知道为什么 希望有大神来解答一下。
页:
[1]