我有一个元组列表,每个元组包含两个元素.少数子列表的第一个元素很常见.我想比较这些子列表的第一个元素,并将第二个元素添加到一个列表中.这是我的清单:
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字典是否包含特定的键,即
d = {}
if key in d:
if d.contains(key):
if d.has_key(key):
Run Code Online (Sandbox Code Playgroud)
一种语言允许你以几种不同的方式做同样的事情是愚蠢的,除非每种方法都做了完全不同的事情.有人可以请对比上面的三种技术,它们有何不同?
我尝试安装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) @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)
我想检查 和 是否Noun在Verb字典中Define,因为当一个单词在其定义中只包含一个名词时,它会抛出一个错误,因为我试图通过机器人输出名词和动词,看看我得到了什么。我是字典新手,非常感谢任何帮助
我对这个问题有一个后续问题.原始问题的一位评论者提到他过去曾经误认为人们使用如下语法:
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) 我有两个清单:
dict可选的键
例子:
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)
给出错误。这是为什么??
python ×6
dictionary ×3
python-3.x ×3
append ×1
compare ×1
discord.py ×1
list ×1
macos ×1
python-2.7 ×1
python-2.x ×1