相关疑难解决方法(0)

如何知道python中的字典中是否存在某些内容

我正在使用API​​,其中的信息将是这样的:

"id": "17",
"address": "Av. Nossa Senhora de Copacabana",
"addressComplement": "A",
"number": "945",
"cityId": "2",
"cityName": "Rio de Janeiro",
"state": "Rio de Janeiro",
"uf": "RJ",
"neighborhood": "Copacabana",
"properties": {},
"telephones": [],
"geolocation": {
    "lat": -22.97625,
    "lng": -43.19002
},
Run Code Online (Sandbox Code Playgroud)

但是,在某些记录中,它不包含该geolocation字段,因此我必须检查是否geolocation存在于我的代码中.

我试图用hasattr这个技巧,但我认为我做错了.

这是我的代码的一部分:

if hasattr(i, 'geolocation'):
    address_lat = i['geolocation']['lat']
    address_lng = i['geolocation']['lng']
else:
    address_lat = 0.0
    address_lng = 0.0
Run Code Online (Sandbox Code Playgroud)

我的想法是,它将检查索引的位置是否i存在内部的东西geolocation.如果有东西,则返回true并进入条件,否则var将接收0.0.

那么,我做错了什么?这是正确的使用方式hasattr吗?

python

3
推荐指数
2
解决办法
227
查看次数

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万
查看次数

字典,从键调用值

我正在尝试获取输入,如果字典logins有一个与我的输入匹配的键,我想返回该键的值。

logins = {
    'admin':'admin',
    'turtle':'password123'
}

logging = input('username: ')

for logging in logins:
    print(logins.values())
Run Code Online (Sandbox Code Playgroud)

python

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

Python - 如果在字典中的值那么

这是我的代码

    if "value" not in dictionary():
        do something
    else:
        do something else
Run Code Online (Sandbox Code Playgroud)

我收到错误'TypeError:'dict'对象不可调用.

我已经尝试将第一行更改为

    if dictionary["value"]:
Run Code Online (Sandbox Code Playgroud)

但得到一个不同的错误.我在哪里错了?

python dictionary if-statement

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

Pythonic方法检查列表中的元素是否包含在密钥字典中

我有这样的映射关键字.

categories_mapping = {
        'comics': 'Comic Books',
        'cartoons': 'Comic Books',
        'manga': 'Comic Books',
        'video and computer games': 'Video Games',
        'role playing games': 'Video Games',
        'immigration': 'Immigration',
        'police': 'Police',
        'environmental': 'Environment',
        'celebrity fan and gossip': 'Celebrity',
        'space and technology': 'NASA / Space',
        'movies and tv': 'TV and Movies',
        'elections': 'Elections',
        'referendums': 'Elections',
        'sex': 'Sex',
        'music': 'Music',
        'technology and computing': 'Technology'}
Run Code Online (Sandbox Code Playgroud)

和这样的清单.

labels = ['technology and computing', 'arts and technology']
Run Code Online (Sandbox Code Playgroud)

如果列表中的任何单词位于字典的键中,我想返回字典的值.

这就是我想出来的,但我认为这不是非常pythonic.

cats = []
for k,v in categories_mapping.items():
    for l in …
Run Code Online (Sandbox Code Playgroud)

python dictionary

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

在字典中搜索键,并打印键及其值

我试图在歌词典中搜索关键词.它们的键是歌曲标题,值是歌曲的长度.我想在字典中搜索这首歌,然后打印出那首歌和它的时间.我已经想出要搜索这首歌,但是不记得如何突出它的价值.这是我现在拥有的.

def getSongTime(songDictionary):
    requestedSong=input("Enter song from playlist: ")
    for song in list(songDictionary.keys()):
        if requestedSong in songDictionary.keys():
            print(requestedSong,value)
Run Code Online (Sandbox Code Playgroud)

python search dictionary key

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

标签 统计

python ×6

dictionary ×4

discord.py ×1

if-statement ×1

key ×1

python-3.x ×1

search ×1