0

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",
  ]
}

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())

And that json looks like this:

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

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.

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?

3 Answers 3

1

You can use index() on a list to find get the index of the item you are searching for. However, this is really expensive if you are doing it often or the list is really long. Instead, you should use a dict:

x = {
    'emoji1': 'name1',
    'emoji2': 'name2',
    # etc.
}

Now you can look up the data quickly with x['emoji1'].

Sign up to request clarification or add additional context in comments.

2 Comments

@Brenda No, you don't "have" to. I suggest building it the same way you built your list of lists.
@Brenda So it looks like your list of lists comes from some JSON. How is that JSON built? Does it come from an API call or is it from a file you control? If it is from some source outside of your control, then I suggest transforming the list of lists into a dict. I suggest looking at the zip() function, too.
1

not sure I understand your question...but if you are just seeking to match corresponding elements of two lists of equal length, use zip

import json
data = json.loads("""
{
  "List1": [
    "name1",
    "name2",
    "name3",
    "name4",
    "name5"
  ],
  "List2": [
    "emoji1",
    "emoji2",
    "emoji3",
    "emoji4",
    "emoji5"
  ]
}
""")

print(dict(zip(data['List2'], data['List1'])))

prints:

{'emoji1': 'name1', 'emoji2': 'name2', 'emoji3': 'name3', 'emoji4': 'name4', 'emoji5': 'name5'}

1 Comment

I think I might have thought I was having a different error than I actually was, I think my problem is with parsing a json that looks like this: [{"emojis":"emoji10,emoji20,emoji11,emoji14,emoji30,,emoji9,emoji44,emoji53,emoji16,emoji48"}] So if I own emoji48 from Bag json, which would correspond with List2, I want that to give the result of the 48th item in List1. Sorry if I'm not explaining it well. It might be important to note that it's not actually called emoji48, so I can't base the code by the actual names.
0

I was able to do it with this:

        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]

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)

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.