英文:
Create a dictionary (array) from CSV data
问题
function getDaysUnderTempDictionary(float $targetTemp): array {
$file = fopen("data/temperatures-filtered.csv", "r");
$result = []; // Initialize an empty array to store results
while ($data = fgetcsv($file)) {
$year = $data[0];
$yearDays = getDaysUnderTemp((int)$year, $targetTemp); // Utilize the existing function to get days under the temperature for each year
if (array_key_exists($year, $result)) {
$result[$year] += $yearDays; // If the year exists in the result array, add to its days count
} else {
$result[$year] = $yearDays; // If the year is not in the result array, set its days count
}
}
fclose($file);
return $result;
}
英文:
I need to write a function that takes temperature as an input and returns a dictionary with years as keys and number of days as values.
CSV file looks like this (year, month, day, hour, temperature):
2019,1,1,0,0.1
2019,1,1,1,0.4
2019,1,1,2,0.8
2019,1,1,3,1.3
2019,1,1,4,1.8
...
2020,1,1,0,-3.9
The number of days is calculated by another function which I already have. It takes a year and a temperature and returns how many days in a given year the temperature was equal to or below the given temperature. Since the data is about hours, not days, the number of hours is found and then divided by 24.
The function:
function getDaysUnderTemp(int $targetYear, float $targetTemp): float {
$file = fopen("data/temperatures-filtered.csv", "r");
$hours = 0;
while ($data = fgetcsv($file)) {
if ($data[0] == $targetYear and $data[4] <= $targetTemp) {
$hours ++;
}
}
fclose($file);
return $hours / 24;
}
So as an example getDaysUnderTemp(2019, -10)
returns 13.92
.
This is a function I am asking about as I'm not sure how it might be done:
function getDaysUnderTempDictionary(float $targetTemp): array {
$file = fopen("data/temperatures-filtered.csv", "r");
while ($data = fgetcsv($file)) {
???
}
fclose($file);
return [];
}
The problem is I don't understand how an already written function could be implemented in this new one, and then create a required dictionary from all this data. Without using classes.
Desired output of getDaysUnderTempDictionary(-10)
:
Array
(
[2019] => 3.88
[2020] => 0.21
[2021] => 13.92
)
答案1
得分: 1
将小时不再只作为单个变量存储,而是存储在由年份索引的数组中。在处理完 CSV 后,循环遍历数组,并将每个值除以 24,就像之前你所做的那样。
这段代码是用于演示目的的模拟 CSV 文件,但除此之外,与你的代码相同:
function getDaysUnderTempDictionary(float $targetTemp): array {
// 以下仅用于模拟 CSV 文件
$dataString = <<<EOT
2019,1,1,0,0.1
2019,1,1,1,0.4
2019,1,1,2,0.8
2019,1,1,3,1.3
2019,1,1,4,1.8
2020,1,1,0,-3.9
EOT;
$stream = fopen('php://memory', 'r+');
fwrite($stream, $dataString);
rewind($stream);
$years = [];
while ($data = fgetcsv($stream)) {
$year = $data[0];
if ($data[4] <= $targetTemp) {
if(!isset($years[$year])){
$years[$year] = 0;
}
$years[$year]++;
}
}
foreach($years as $year => $hours){
$years[$year] = $hours / 24;
}
return $years;
}
演示链接:https://3v4l.org/RWFPK
英文:
Instead of just storing hours as a single variable, store it in an array indexed by year. Once done with the CSV, loop over the array and divide each by 24, same as you did before.
This code has a mock CSV file for demo purposes, but is otherwise the same as yours:
function getDaysUnderTempDictionary(float $targetTemp): array {
//This is just for mocking a CSV file
$dataString = <<<EOT
2019,1,1,0,0.1
2019,1,1,1,0.4
2019,1,1,2,0.8
2019,1,1,3,1.3
2019,1,1,4,1.8
2020,1,1,0,-3.9
EOT;
$stream = fopen('php://memory', 'r+');
fwrite($stream, $dataString);
rewind($stream);
$years = [];
while ($data = fgetcsv($stream)) {
$year = $data[0];
if ($data[4] <= $targetTemp) {
if(!isset($years[$year])){
$years[$year] = 0;
}
$years[$year]++;
}
}
foreach($years as $year => $hours){
$years[$year] = $hours / 24;
}
return $years;
}
Demo: https://3v4l.org/RWFPK
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论