Great job making it this far into the course.
Let's take a look at the solution to this XORD
exercise. So here's our parse file function, and you
can see that what we've done here, is use this
column slicing trick. We've used the call values method
on sheet to pull out all of the values in
column one. Column one being the coast column within
that data set. Then, we're simply using max and min
here on this list that we've pulled out, in order to get the max val and min
val in that entire column. We're using the index
method on lists, to figure out where that max
val is in the CV list. Now, because the
data that we have actually begins on row one
of the spreadsheet rather than row zero, we've got
to add a one here. So that within our
spreadsheet, we end up with the right position for
that value, that is to say, the right row
number. Okay, then what we're going to do here is
for the position or the row on which the
max value appears, we're going to take the value
in column zero. That will give us the max
time. Okay? And then what we're going to do,
is turn that time, which we'll get as a floating
point number, into an actual time tuple. We'll
do that same process with the min value.
In generating our data dictionary that we'll end
up returning from this function, we can simply
plug in the realtime, maxval, realmintime, and minval,
and then to get the average, we're simply
going to calculate it right here, and make
it the value of this key, avgcoast. Okay,
let's run this. I just want to point out that I'm using the p print
module here, so that we get our data printed out in a nicely structured way.
And we can see that we get good values here.
And if you take a look in the spreadsheet itself
and do something like sort the spreadsheet, you'll see that
the maximum times and the maximum values are calculated correctly. One
thing I want to note is that, in our assertion here,
we're doing a rounding so that we don't have any
issues with floating point values being slightly off as we
get further and further to the right of the decimal point.
.عمل رائع حتى الآن في الدورة التدريبية
فلنلقِ نظرة على الحل لتمرين XORD
هذا. وإليك دالة parse file الخاصة بنا ويمكنك
رؤية أن ما قمنا به هنا هو استخدام
خدعة تقسيم العمود هذه. كما استخدمنا أسلوب استدعاء القيم
على ورقة لاستخراج جميع القيم الموجودة في
العمود واحد. العمود واحد هو العمود الطرفي
ضمن مجموعة البيانات. ثم، نستخدم بكل سهولة الحد الأقصى والأدنى
على هذه القائمة التي استخرجناها، للحصول على أقصى قيمة وأدنى
قيمة في هذا العمود ككل. نحن نستخدم
أسلوب المؤشر على القوائم، لاكتشاف موضع أقصى
قيمة في قائمة CV. الآن، نظرًا لأن
البيانات التي لدينا تبدأ بالفعل من الصف واحد
من جدول البيانات بدلاً من الصف صفر، كان لزامًا علينا
إضافة صف هنا. ومن ثم ضمن
جدول البيانات، توصلنا إلى الموضع الصحيح
لتلك القيمة، بمعنى، رقم
الصف الصحيح. حسنًا، ما سنقوم به بعدئذٍ هنا يتعلق
بالموضع أو الصف الذي تظهر عليه
أقصى قيمة، سنأخذ القيمة الموجودة في
العمود صفر. وسيمنحنا هذا أقصى
وقت. Okay? وبعدئذٍ ما سنقوم به
هو تحويل هذا الوقت، الذي سنحصل عليه على هيئة رقم بفاصلة
عائمة، إلى مجموعة أوقات فعلية. سنجري
.نفس هذه العملية مع أدنى قيمة
وخلال إنشاء قاموس البيانات الذي
سنحصل عليه من هذه الدالة في نهاية الأمر، يمكننا بسهولة
إضافة الوقت الفعلي وأقصى قيمة وأدنى وقت فعلي وأدنى قيمة
ثم الحصول على المتوسط بعدئذٍ، سنقوم بحسابه
بكل سهولة هنا، ونجعله
،قيمة هذا المفتاح، avgcoast. حسنًا
لنشغِّل هذا. أريد الإشارة إلى أنني أستخدم الوحدة النمطية للطباعة p
.هنا، لذا نحصل على البيانات مطبوعة بطريقة منظمة على نحوٍ رائع
.ويمكننا رؤية أننا حصلنا على قيم جيدة هنا
وإذا ألقيت نظرة في جدول البيانات نفسه
وقمت بشيء مثل فرز جدول البيانات، فسترى أن
أقصى الأوقات وأقصى القيم محسوبة على نحو صحيح. أحد الأشياء
،التي أريد التنويه إليها، في تأكيدنا هنا
أننا نقوم بالتقريب ومن ثم لا توجد لدينا
أي مشكلات تتعلق بكون قيم الفاصلة العائمة بعيدة عن موضعها قليلاً حيث
.نبتعد أكثر وأكثر إلى يمين النقطة العشرية
Até aqui, um excelente trabalho tem sido feito neste curso.
Vamos analisar a solução para este exercício
XLRD. Aqui está nossa função de parse file, e você
pode ver que o que fizemos foi usar este
truque de divisão de coluna. Usamos o método de valores de chamada
na folha para extrair todos os valores na
coluna um. A coluna um sendo a coluna coast dentro
desse conjunto de dados. Estamos simplesmente usando max e min
aqui nesta lista que extraímos, a fim de obtermos os valores máx. e mín.
nesta coluna inteira. Estamos usando o método
de índice nas listas para entender onde este
valor máx. está na lista CV. Por conta dos
dados terem começado na linha um
da planilha em vez de na linha zero, tivemos que
adicionar um aqui. Assim, em nossa
planilha, acabamos com a posição certa para
esse valor, ou seja, o número certo
da linha. O que vamos fazer aqui é,
para a posição ou a linha na qual o
valor máx. aparece, vamos pegar o valor
na coluna zero. Isso nos dará o tempo
máx. Certo? Depois o que vamos fazer é,
transformar esse tempo, que obteremos como um número
de ponto flutuante, em uma tupla de tempo. Faremos
esse mesmo processo com o valor mín.
Ao gerar nosso dicionário de dados que acabará
retornando desta função, podemos simplesmente
inserir realtime, maxval, realmintime e minval e,
em seguida, obter a média; vamos
simplesmente calculá-la aqui e torná-la
o valor desta chave, avgcoast. Vamos
executar isso. Só quero ressaltar que estou usando o módulo
p print aqui, de modo que nossos dados foram impressos de uma maneira estruturada bem legal.
Podemos ver que obtivemos bons valores aqui.
Se você analisar a planilha em si
e fizer algo como classificá-la, você verá que os tempos
máximos e os valores máximos são calculados corretamente. Preciso
dizer que, em nossa asserção aqui,
estamos fazendo um arredondamento para que não tenhamos nenhum
problema de ter valores de ponto flutuante ligeiramente de fora
à medida que avançamos à direita do ponto decimal.
恭喜你 你已经完成了这节课的这么多内容
我们看一下这个 XORD 练习的答案
这就是解析文件函数
可以看到 我们在这里使用了切片的方法
在表格中使用了 col.values 的方法
来获取第1列的所有值
第1列就是数据集里的 coast 列
然后 我们直接在所得的列表中
利用最大值和最小值函数
获取整个列的最大值和最小值
为找出最大值在 CV 列表中的位置
我们在表格中使用了索引的方法
由于数据始于电子表的第1行
而不是第0行
因此 我们需要在这里加上1
以使我们的值落于电子表内的正确位置
也就是说 获得正确的行数
然后 我们要针对
最大值出现的位置或行
20
获取第0列的值
这样就能得到最大时间了
然后 我们要将
用浮点数表示的日期
转换为真正的时间元组
对于最小值 我们也进行了同样的处理
在生成该函数最终返回的数据字典时
我们可以直接插入
实际时间、最大值、实际最小时间和最小值
然后 要计算平均值
我们可以直接在这里进行计算
把它当作这个关键字 avgcoast 的值
我们来运行这个代码 我要说明一下
我在这里用的是 pprint 模块 以使打出的数据结构更加美观
可以看到 我们得到的值是正确的
如果你查看电子表本身
并对其进行排序 你会发现
最大时间和最大值的计算结果都是正确的
我需要指出一点 在这里的声明中
我们要进行四舍五入
这样以来 即使小数点后面的数字越来越长
我们的浮点值也不会出现偏差