Using Lists as Queues in Python----在Python中把链表当作队列使用

    It is also possible to use a list as a queue, where the first element added is the first element retrieved (``first-in, first-out''); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).
    你也可以把链表当做队列使用,队列作为特定的数据结构,最先进入的元素最先 释放(先进先出)。不过,列表这样用效率不高。相对来说从列表末尾添加和弹 出很快;在头部插入和弹出很慢(因为,为了一个元素,要移动整个列表中的所 有元素)。
    To implement a queue, use collections.deque which was designed to have fast appends and pops from both ends. 
    要实现队列,使用 collections.deque ,它为在首尾两端快速插入和删除而设计。

For example:

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")
# Terry arrives
>>> queue.append("Graham")
# Graham arrives
>>> queue.popleft()
# The first to arrive now leaves
'Eric'
>>> queue.popleft()
# The second to arrive now leaves
'John'
>>> queue
# Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])


`python-lists.sources.list` 文件是用来定义额外的 Python 包来源的配置文件。当你想从非默认仓库(比如私人仓库、测试版本等)获取 Python 库时,你需要创建这样一个文件并将它放置在适当的目录下(通常是 `/etc/pip.conf`, `/home/<username>/.pip/pip.conf` 或者 `/etc/pymodule/pymodule.conf`),然后通过 pip 工具应用这些配置。 下面是如何创建并使用这样的文件的一般步骤: ### 创建文件 首先,打开文本编辑器(如 Vim, Emacs, 或者 Notepad++ 等),然后创建一个新的文件,并将其命名为 `python-lists.sources.list`。 ### 编写文件内容 在这个文件里,你应该包括以下几部分内容: 1. **仓库地址**:指定你要从哪些仓库下载库。例如,如果你有一个名为 `myrepo` 的私人仓库,你可以添加以下行: ``` [myrepo] trusted-host = example.com url = https://example.com/myrepo/ ``` 其,`trusted-host` 表示你信任的主机域名,确保能安全地从这个域加载资源;`url` 则是仓库的 URL 地址。 2. **额外选项**:如果你需要设置其他的选项,可以使用 `[options]` 来定义,例如: ``` [options] index-url = https://pypi.python.org/simple extra-index-url = http://myextra.repo.com/simple ``` 在这里,`index-url` 定义了默认的 PyPI 仓库地址,而 `extra-index-url` 则是附加的仓库地址。 3. **认证信息**:如果仓库需要认证,可以在这里提供用户名和密码: ```ini [options] username = your_username password = your_password ``` 使用这些信息时要注意安全,避免在文本文件直接存储敏感信息。 4. **其他配置项**:还可以根据需要添加更多的配置选项,比如自动安装依赖等。 ### 应用配置 保存文件后,你需要将它应用于你的 `pip` 客户端。你可以在命令行上运行: ```bash pip config set global.file <path_to_your_file> ``` 或者如果你是在项目级设置: ```bash pip config set project_name.file <path_to_your_file> ``` 其 `<path_to_your_file>` 是你刚才创建的 `python-lists.sources.list` 文件的路径。 ### 更新和验证 之后,你可以通过运行 `pip list` 或者 `pip show package-name` 来检查是否已经正确加载了仓库的库。同时也可以通过访问仓库页面或使用 `curl` 查看仓库是否正常工作。 这就是如何创建并使用 `python-lists.sources.list` 类型的文件的基本过程,以定制你使用Python 库来源。注意每个实际的应用场景可能会有所不同,所以请根据具体的使用需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值