【问题标题】:Slicing a dictionary's value isn't working properly切片字典的值无法正常工作
【发布时间】:2020-09-20 20:50:48
【问题描述】:

所以我有一个 json 文件,其中包含角色 id(一个名为角色的字典),带有一个键(这是角色的名称),每个键都有角色的 id 作为它的值

看起来像这样:

{"Ban": ["694175790639743076"], "Muted": ["692863646543380590"]}

我主要只需要来自 json 的 id

使用 roles['Muted']roles.get('Muted') 之类的东西会给我['692863646543380590'] Muted=roles.get('Muted') print(Muted)

函数采用整数,所以我必须从输出中删除 [' 并得到:692863646543380590

无论我使用什么切片,切片都会给我[]work=boost1[2:20]*or any other slice* print(work) 发出"[]"

为什么切片在这里不起作用?我该如何解决这个问题??

【问题讨论】:

  • 您是否要分割列表中的数字[]

标签: python json dictionary slice discord.py


【解决方案1】:

试试这个 -

work = int(boost1[0]) #fetching the first element of list and converting it into int

【讨论】:

    【解决方案2】:

    首先roles['Muted'] 是一个列表,如果您只想要第一个元素,则使用0 索引获取它,然后尝试使用int() 将其转换为整数:

    Muted=int(roles.get('Muted')[0]) # or Muted=int(roles['Muted'][0])
    

    静音将是:

    692863646543380590
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-23
      • 2021-11-06
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多