所以我带了一个文件并通过我的代码运行它。每行显示在文件上的示例:
def counts(filename):
d={}
with open(filename) as f:
for line in f
for number in line:
return d
我的目标是让我的代码遍历文件中的数字,输出是一个以数字作为键的字典,以及它作为值出现在文件中的次数。例如:
{100:2200:2300:1400:1}
这是我到目前为止所做的。
Rack::Cors
另外,我可以使用.count()吗?那么我可以在文件中创建一个数字列表并将其设置为键,然后设置一个列表,列出每个数字出现的相应时间,并将其设置为键的值?
答案 0 :(得分:1)
$stmt = $conn->prepare("SELECT * FROM artist WHERE artID != ?");
O / P
<div class="row">
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'connection.php';
if(isset($_GET["album"]))
{
/* If album was passed in the URL then get current values
for that album */
$stmt = $conn->prepare("SELECT cd.artID, artName, cdTitle, cdPrice, cdGenre, cdTracks FROM cd INNER JOIN artist ON (cd.artID = artist.artID AND cdID = ?);");
if(!$stmt)
{
echo $conn->error;
exit;
}
$albumID = htmlspecialchars($_GET["album"]);
$stmt->bind_param('i', $albumID);
$stmt->execute();
$stmt->bind_result($albumArtID, $albumArtName, $albumTitle,
$albumPrice, $albumGenre, $numTracks);
$stmt->fetch();
/* Create input fields */
// Album Title
echo "<div class=\"row horizontal-center\">" .
"<input type=\"text\" value=\"" . htmlspecialchars($albumTitle) . "\" name=\"albumTitle\"/>" .
"</div>";
// Artist Name
echo "<div class=\"row horizontal-center\">" .
"<h6>By Artist:</h6>" .
"</div>";
echo "<div class=\"row horizontal-center\">" .
"<select name=\"artID\">";
/* Create option for current artist so it will be first in list */
echo "<option value=\"$albumArtID\">$albumArtName</option>\n";
/* Generate list of artists except artist currently associated with the album */
$stmt = $conn->prepare("SELECT * FROM artist WHERE artID != ?");
if(!$stmt)
{
echo $conn->error;
exit;
}
$stmt->bind_param('i', $albumArtID);
$stmt->execute();
$stmt->bind_result($artID, $artName);
/* Create options for artists that were found */
while($stmt->fetch())
{
echo "<option value=\"$artID\">$artName</option>\n";
}
echo "</select>" .
"</div>";
// Album Price
echo "<div class=\"row horizontal-center\">" .
"<input type=\"number\" step=\"0.01\" value=\"" . htmlspecialchars($albumPrice) . "\" name=\"albumPrice\"/>" .
"</div>";
// Album Genre
echo "<div class=\"row horizontal-center\">" .
"<input type=\"text\" value=\"" . htmlspecialchars($albumGenre) . "\" name=\"albumGenre\"/>" .
"</div>";
// Number of Tracks
echo "<div class=\"row horizontal-center\">" .
"<input type=\"number\" value=\"" . htmlspecialchars($numTracks) . "\" name=\"numTracks\"\n/>" .
"</div>";
// Delete checkbox
echo "<div class=\"row\">" .
"<div class=\"col-2\">" .
"<h6>Delete:</h6>" .
"</div>" .
"<div class=\"col-1\">" .
"<input type=\"checkbox\" name=\"delete\" value=\"Delete\"/>" .
"</div>" .
"</div>";
/* Create hidden field to submit the album ID with the form */
echo "<input type=\"hidden\" value=\"" . htmlspecialchars($albumID) . "\" name=\"albumID\"\n/>";
}
else
{
/* Send browser back to artists page if they somehow accessed
the edit page without going through the "Edit" link next
to an artist in the table. This would be the artName variable
would not be sent via the URL.*/
header("Location: artists.php");
}
?>
</div>
<div class="row">
<div class="col-2">
<h6>Delete:</h6>
</div>
<div class="col-1">
<input type="checkbox" name="delete" value="Delete"/>
</div>
</div>
<div class="row">
<input type="submit" name="submit" value="Update"/>
</div>
答案 1 :(得分:0)
你可以创建一个矩阵,其中每个位置可以表示一个数字,其内容代表它在文件中出现的数字。 此外,你可以创建一个比较器,与文件中的数字进行比较,然后增加计数器
答案 2 :(得分:0)
对于文件中的每个数字,如果它没有在词典中显示为键,则添加它(计数为0);无论如何,增加该数字的计数。
答案 3 :(得分:0)
这使用生成器读取所有行并将它们转换为整数。
from collections import Counter
from csv import reader
def counts(filename):
return Counter(int(line[0]) for line in reader(open(filename)) if line)
c = counts('my_file.csv')
>>> c
Counter({'100': 2, '200': 2, '300': 1, '400': 1})
>>> c.most_commont(5)
[('200', 2), ('100', 2), ('300', 1), ('400', 1)]
>>> dict(c)
{'100': 2, '200': 2, '300': 1, '400': 1}
答案 4 :(得分:0)
一种简单的方法是使用Counter
,一旦计数完成,就可轻松转换为from collections import Counter
def counts(filename):
with open(filename) as f:
return dict(Counter(int(line) for line in f))
# {200: 2, 100: 2, 300: 1, 400: 1}
。
friendBook[i]
答案 5 :(得分:0)
仅使用简单的python:
word_count = {}
with open('temp.txt') as file:
for line in file:
word_count[line[:-1]] = word_count.setdefault(line[:-1], 0) + 1
如果您想使用精美的图书馆,可以使用Counter
的@ alexander答案。