【发布时间】:2020-10-10 07:15:16
【问题描述】:
这显然是一个流行的面试问题。
有 2 个包含恐龙数据的 CSV 文件。我们需要查询它们以返回满足特定条件的恐龙。
注意 - 我们不能使用其他模块,如 q、fsql、csvkit 等。
file1.csv:
NAME,LEG_LENGTH,DIET
Hadrosaurus,1.2,herbivore
Struthiomimus,0.92,omnivore
Velociraptor,1.0,carnivore
Triceratops,0.87,herbivore
Euoplocephalus,1.6,herbivore
Stegosaurus,1.40,herbivore
Tyrannosaurus Rex,2.5,carnivore
file2.csv
NAME,STRIDE_LENGTH,STANCE
Euoplocephalus,1.87,quadrupedal
Stegosaurus,1.90,quadrupedal
Tyrannosaurus Rex,5.76,bipedal
Hadrosaurus,1.4,bipedal
Deinonychus,1.21,bipedal
Struthiomimus,1.34,bipedal
Velociraptor,2.72,bipedal
使用论坛: 速度 = ((STRIDE_LENGTH / LEG_LENGTH) - 1) * SQRT(LEG_LENGTH * g),其中 g = 9.8 m/s^2
编写一个程序来读取 csv 文件,并仅打印双足恐龙的名称,按速度从快到慢排序。
在 SQL 中,这很简单:
select f2.name from
file1 f1 join file2 f2 on f1.name = f2.name
where f1.stance = 'bipedal'
order by (f2.stride_length/f1.leg_length - 1)*pow(f1.leg_length*9.8,0.5) desc
这在 python 中如何实现?
【问题讨论】:
-
你有什么想法?我不认为知道我们中的一个人能通过面试是你真正想要的。
-
@IgorRivin,我知道用sql怎么做,想知道怎么用python解决
-
你可以使用熊猫吗?
-
您需要编写完成所有这些事情的代码。这对于 StackOverflow 来说太宽泛了
-
@juanpa.arrivillaga,不要求提供成熟的部署就绪代码,即使是关于从哪里开始的指针或想法也将不胜感激