python - 对于相同的代码,在 CodeSkulptor 上得到错误的答案

标签 python

在在线类(class)中,在与 Python 中的字典相关的作业中,我的一位同事遇到了输出错误,但奇怪的是,相同的代码在 IDLE 上运行良好并打印了正确的代码。 Autograger 软件支持 Python 2.7,并且基于 Skulpt 和 CodeMirror,因此为了测试此代码,我使用了 CodeSkulptor Viz 模式,该模式会打印错误的答案。为什么会发生这种情况?

handle = [ "From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="daa9aebfaab2bfb4f4b7bba8abafbba8be9aafb9aef4bbb9f4a0bb" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed818298849ead808889848cc38f889f8688818894c3888998" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f88289919996b88d95919b90d69d9c8d" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5d454340584a6f465a5f5a46014a4b5a" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c9c2dad2ddf3c6dedad0db9dd6d7c6" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25574f494a5240654c5055504c0b404150" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d3c7d5def0d9c5c0c5d99ed5d4c5" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a697d6f644a637f7a7f63246f6e7f" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5235213b3e24372012273f3b313a7c373627" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d6c2d8ddc7d4c3f1c4dcd8d2d99fd4d5c4" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="542e253d353a1421393d373c7a313021" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c4d0cacfd5c6d1e3d6cecac0cb8dc6c7d6" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbccdadcd5dec9d6c9fbd2cecbced295dedfce" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="344e455d555a7441595d575c1a515041" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65040b1117040b0c022506041700114b0604084b04064b100e" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee89819e8f82c09c8f838f9d8f8383978d818185ae89838f8782c08d8183" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3e3b2c333e743235282d332e201a2f392e743b3974203b" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284c495e414c0640475a5f415c52685d4b5c06494b065249" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c2c7d0cfc288cec9d4d1cfd2dce6d3c5d288c7c588dcc7" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d797c6b74793375726f6a7469675d687e69337c7e33677c" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f08384958098959ede9d91828185918294b0859384de9193de8a91" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="630f0c160a10230e06070a024d01061108060f061a4d060716" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bed2d1cbd7cdfed3dbdad7df90dcdbccd5dbd2dbc790dbdacb" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfadbea69fb2babbb6bef1bdbaadb4bab3baa6f1babbaa" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c1d5c7cce2cbd7d2d7cb8cc7c6d7" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197a6e7c7759706c696c70377c7d6c" rel="noreferrer noopener nofollow">[email protected]</a>",
"From <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bbafbdb698b1ada8adb1f6bdbcad" rel="noreferrer noopener nofollow">[email protected]</a>",
    ]

answer_key = 0

dct = dict()
for line in handle:
    if line.startswith('From '):
        email = line.strip().split()[1]
        if not email in dct:
            dct[email] = 1
        else:
            dct[email] += 1

answer_key = max(dct, key=dct.get)
print answer_key, dct[answer_key]  

正确的输出是 [email protected] 5

但是 CodeSculptor 打印:
[email protected] 4

最佳答案

问题是 Skulpt 不是 Python 的完整实现,并且您正在使用它不支持的功能:max 上的 key 参数 - 或者,事实上,任何函数上的任何关键字参数(请参阅 the docs )。

如果您在稳定版本上尝试它,您会收到错误:

Line 41: ValueError: Keyword arguments are not supported by this function

测试版显然增加了对此功能的支持,但它显然仍然存在错误,这可能就是您得到错误答案的原因。我假设他们有一个错误报告系统,所以您可能应该就此提交一个错误。

但是,除非您想等到下一个版本的 Skulpt 完成,并且新版本的 CodeSkulptor 推出,否则这对您没有帮助;您需要解决缺失/有缺陷的功能。 (并确保留下评论,提醒您自己或其他读者,为什么您需要解决方法。)

作为Sorting HOWTO解释说,在与排序相关的函数(包括 max,即使这并不明显)具有 key 参数之前,Python 程序员必须做一些称为“装饰排序(或搜索)-undecorate”:创建一个已修改(以某种可逆方式)的新序列,以按所需顺序排序,然后调用您的函数,然后反转修改。

在您的情况下,您希望按值顺序对字典进行排序。可逆装饰的最简单方法是创建一个(value, key) 对列表。其中的 max 将是最高值的 valuekey,然后您要获取 key >。所以:

# Skulpt can't handle key arguments on max, so we have to
# decorate-search-undecorate instead.
deco = [(v, k) for (k, v) in dct.items()]
_, answer_key = max(deco)

关于python - 对于相同的代码,在 CodeSkulptor 上得到错误的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25229868/

相关文章:

Python - 如何使用多个分隔符拆分列值

python - 将点积应用于 3D 数组中的所有列

python - 将 Excel 数据导入 Python 以生成热图

python - Windows 上没有适用于 Python 3.5 的 cx_Oracle 吗?

python - 优化性能的 tf.data 管道设计

python - 将新字符串插入列表并保存信息

python - 如何使用 python 下载需要登录的 .csv 文件?

python - 如何从一堆图像中裁剪具有不同位置的相同大小的图像 block ?

python 鲭鲨 : Adding a Default Namespace

python - 用字符串和附加到它的数字的长度替换字符串中的数字