比较Python中列表的匹配元素 [英] Comparing matching elements of a list in Python

查看:229
本文介绍了比较Python中列表的匹配元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如何使一个列表中的第5个项目与另一个列表中的第5个项目匹配,然后在匹配时发送该结果?这是json的示例:

For example, how can I get the 5th item in one list to match with the 5th item of the other, and then send that result if it matches? Here is an example of what the json will look like:

{
  "List1": [
    "name1",
    "name2",
    "name3",
    "name4",
    "name5",
  ],
  "List2": [
    "emoji1",
    "emoji2",
    "emoji3",
    "emoji4",
    "emoji5",
  ]
}

字符在第二个列表中找到项目,我需要使其与第一个列表中的专有名称。它们已经按照正确的顺序排列,因此第5个名称就是第5个表情符号的匹配项。如果相关的话,我正在使用这种方法来查找玩家拥有的表情符号:

The characters find items in the second list and I need to make it correspond with the proper names in the first list. They are already in the correct order, so the 5th name is the match to the 5th emoji. If it's relevant, I'm using this method of finding the emojis that the player owns:

        scan = f"privatelink"
        async with aiohttp.ClientSession() as cs:
            async with cs.get(scan) as r:
                try: Bag = ast.literal_eval(await r.text())
                except: Bag = json.loads(await r.text())

而json看起来像这样:

And that json looks like this:

[{"emojis":"emoji10,emoji20,emoji11,emoji14,emoji30,,emoji9,emoji44,emoji53,emoji16,emoji48"}]

这看起来确实很奇怪,我认为这可能实际上是我的问题。我想我以前没有看过像这样的json和Idk如何解析它。

It looks really weird and I think this might actually be my problem. I don't think I've seen a json like this before and Idk how to parse it.

因此,假设我拥有 emoji5从第二个列表。我想将其与 name5匹配从第一个列表开始。如何匹配这些结果?

So let's say I own "emoji5" from the second list. I want to match that to "name5" from the first list. How do I match these results?

推荐答案

我能够做到这一点:

        bag = bag[0]['items']
        emj = c['smiley']
        nam = c['words']


        for x in emj:
            if x in bag:
                for x in [emj.index(x)]:
                    J = c['words'][x]

我现在的问题是, J 当分配给Discord嵌入字段时,名称或描述只是向每个垃圾邮件发送一次具有不同嵌入的垃圾邮件。答案是正确的,但是我如何将每个单词放入其自己的字段中,或拆分为描述的自己部分,而不是为每个单词发送新的嵌入内容?我已经试过了:

My problem now is that J when assigned to a Discord embed field.name or description are just spamming each one with different embeds one at a time. The answer is correct but how do I make each word into it's own field, or split into it's own part of the description instead of sending a new embed for each word? I have tried this:

                    e = discord.Embed(title=f"Test", color=discord.Colour(value=), 
                    description=f'')

                    e.add_field(name=J, value=f"")
                    await ctx.send(embed=e)

但是它只是将第一个结果添加到单个字段并发送嵌入,然后再将下一个结果发送到另一个嵌入,依此类推上。不用介意btw的颜色值,我仍在尝试决定笑声。

But it just adds a single field with the first result and sends the embed, then sends another embed with the next result, and so on. Don't mind the color value btw, I'm still trying to decide lol.

这篇关于比较Python中列表的匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆