小编Sea*_*ean的帖子

订购柜台配方如何运作

阅读如何super()工作,我遇到了关于如何创建有序计数器的这个方法:

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first seen'
     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__,
                            OrderedDict(self))
     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)
Run Code Online (Sandbox Code Playgroud)

例如:

oc = OrderedCounter('adddddbracadabra')

print(oc)

OrderedCounter(OrderedDict([('a', 5), ('d', 6), ('b', 2), ('r', 2), ('c', 1)]))
Run Code Online (Sandbox Code Playgroud)

有人能够解释这是如何神奇地起作用的吗?

这也出现在Python文档中.

python counter dictionary ordereddictionary python-3.x

20
推荐指数
1
解决办法
7457
查看次数

字典生成器中有多个'for'循环

此Python文档中,以下内容用作生成器表达式的示例:

dict((fn(i+1), code)
    for i, code in enumerate('FGHJKMNQUVXZ')
    for fn in (int, str))

>> {1: 'F', '1': 'F', 2: 'G', '2': 'G', 3: 'H', '3': 'H', 4: 'J',...}
Run Code Online (Sandbox Code Playgroud)

我不明白第二个for循环如何for fn in (int, str)将int值转换为字符串并向字典添加一个额外的条目.

我找到了这个Stack Overflow问题,但是我仍然无法知道第二个for循环在这种情况下是如何工作的.

python generator python-3.x

13
推荐指数
2
解决办法
1215
查看次数

在 39 颗宝石中找不到“minitest”(>= 5.1)

情况:

我正在尝试设置 Cocoapods。

我使用的是 OSX 12.2。我已经用 Homebrew 安装了 Ruby。当我跑步时gem list minitest (5.15.0)列出。

在终端中运行pod setup失败,因为它无法minitest (>=5.1)在 39 个宝石中找到。

我注意到搜索到的目录看起来是 OSX 系统 ruby​​ 安装位置Library/Ruby/Site/2.6.0;而不是我用 Homebrew 安装的ruby 3.1.0(at )。/usr/local/opt/ruby/bin/ruby

我假设我需要做的是将终端指向ruby 3.1.0; 但是,我仔细检查了我的 .zhsrc 文件,它看起来是有序的(我是通过阅读 SO 问题以及 Ruby 和 Cocoapods 设置指南了解到这一点的 - 所以我对此可能是非常错误的)。

这是以下内容.zhsrc

export PATH="/usr/local/sbin:$PATH"

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/sean/Desktop/google-cloud-sdk/path.zsh.inc' ]; then . '/Users/sean/Desktop/google-cloud-sdk/path.zsh.inc'; fi

# The next …
Run Code Online (Sandbox Code Playgroud)

ruby rubygems path cocoapods

4
推荐指数
1
解决办法
7048
查看次数