相关疑难解决方法(0)

比较Python中连续元组列表的第一个元素

我有一个元组列表,每个元组包含两个元素.少数子列表的第一个元素很常见.我想比较这些子列表的第一个元素,并将第二个元素添加到一个列表中.这是我的清单:

myList=[(1,2),(1,3),(1,4),(1,5),(2,6),(2,7),(2,8),(3,9),(3,10)]
Run Code Online (Sandbox Code Playgroud)

我想列出一个列表,看起来像这样:`

NewList=[(2,3,4,5),(6,7,8),(9,10)]
Run Code Online (Sandbox Code Playgroud)

我希望如果有任何有效的方法.

python compare list append python-2.7

7
推荐指数
1
解决办法
1928
查看次数

检查Python字典中的特定键

有几种不同的方法可以检查Python字典是否包含特定的键,即

d = {}

if key in d:

if d.contains(key):

if d.has_key(key):
Run Code Online (Sandbox Code Playgroud)

一种语言允许你以几种不同的方式做同样的事情是愚蠢的,除非每种方法都做了完全不同的事情.有人可以请对比上面的三种技术,它们有何不同?

python

6
推荐指数
3
解决办法
4024
查看次数

如何替换python3中的has_key?

我尝试安装Auto-SelfControl并在执行此命令时卡住了:

sudo /usr/bin/python auto-selfcontrol.py
Run Code Online (Sandbox Code Playgroud)

它显示错误:

AttributeError:“dict”对象没有属性“has_key”

我正在寻找解决方案,最终将has_key替换为in运算符,但由于我只了解 python 的基础知识,所以代码对我来说相当复杂。

有 3 个地方使用了has_key,你能帮我更改它以便我可以使用 python3 运行吗?

1.

def check_if_running(username):
""" checks if self-control is already running. """
defaults = get_selfcontrol_settings(username)
return defaults.has_key("BlockStartedDate") and not NSDate.distantFuture().isEqualToDate_(defaults["BlockStartedDate"])
Run Code Online (Sandbox Code Playgroud)

2-4.

def check_config(config):
""" checks whether the config file is correct """
if not config.has_key("username"):
    exit_with_error("No username specified in config.")
if config["username"] not in get_osx_usernames():
    exit_with_error(
            "Username '{username}' unknown.\nPlease use your OSX username instead.\n" \
            "If you have trouble …
Run Code Online (Sandbox Code Playgroud)

python macos python-3.x

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

Python字典检查key是否存在

@commands.command(aliases=['lookup'])
    async def define(self, message, *, arg):
        dictionary=PyDictionary()
        Define = dictionary.meaning(arg)
        length = len(arg.split())
        if length == 1:
            embed = discord.Embed(description="**Noun:** " + Define["Noun"][0] + "\n\n**Verb:** " + Define["Verb"][0], color=0x00ff00)
            embed.set_author(name = ('Defenition of the word: ' + arg),
            icon_url=message.author.avatar_url)
            await message.send(embed=embed)
        else:
            CommandError = discord.Embed(description= "A Term must be only a single word" , color=0xfc0328)
            await message.channel.send(embed=CommandError)
Run Code Online (Sandbox Code Playgroud)

我想检查 和 是否NounVerb字典中Define,因为当一个单词在其定义中只包含一个名词时,它会抛出一个错误,因为我试图通过机器人输出名词和动词,看看我得到了什么。我是字典新手,非常感谢任何帮助

python dictionary python-3.x discord.py

3
推荐指数
1
解决办法
3万
查看次数

为什么'键入d.keys()'在O(n)时完成,而'key in d'在O(1)中完成?

我对这个问题有一个后续问题.原始问题的一位评论者提到他过去曾经误认为人们使用如下语法:

key in d.keys()
Run Code Online (Sandbox Code Playgroud)

在O(n)时间内完成,而不是

key in d
Run Code Online (Sandbox Code Playgroud)

在O(1)时间内完成,没有意识到差异.直到今天(当我在试图理解为什么我的代码运行如此缓慢之后偶然发现原始问题时),我才是其中之一.我尝试使用Python 2.7.5验证注释的准确性,果然,这里是timeit的结果:

$ python -m timeit -s 'd=dict.fromkeys(range(100))' '1000 in d.keys()'
100000 loops, best of 3: 2.18 usec per loop
$ python -m timeit -s 'd=dict.fromkeys(range(100))' '1000 in d'
10000000 loops, best of 3: 0.0449 usec per loop
$ python -m timeit -s 'd=dict.fromkeys(range(1000))' '10000 in d.keys()'
100000 loops, best of 3: 17.9 usec per loop
$ python -m timeit -s 'd=dict.fromkeys(range(1000))' '10000 in d'
10000000 loops, best of 3: …
Run Code Online (Sandbox Code Playgroud)

python dictionary

2
推荐指数
1
解决办法
551
查看次数

如何检查字典是否有不需要的密钥?

我有两个清单:

  • 必须在dict中的键
  • dict可选的键

    1. 如何检查词典中必须存在的键是否存在?
    2. 如何检查字典中是否有任何可选键?

python

0
推荐指数
1
解决办法
117
查看次数

为什么 dict.has_key() 在 Python 中不起作用?

例子:

Dict={"Name":"Sai","Age":23}
"Age" in Dict
Run Code Online (Sandbox Code Playgroud)

产量TRUE,但是

Dict.has_key("Age")
Run Code Online (Sandbox Code Playgroud)

给出错误。这是为什么??

dictionary python-2.x python-3.x

-2
推荐指数
1
解决办法
2万
查看次数