list1 position = list 2? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list1 position = list 2? (/thread-29792.html) |
list1 position = list 2? - Nickd12 - Sep-20-2020 lets say I have two lists list1 = [1, 2, 3, 4, 5] list2 = [6, 7, 8, 9, 10] say list 1 position 0 is an input so the number 1 I want to return list 2 with that same position as an output so I would like to return the 6 basically what would be my return statement for this? RE: list1 position = list 2? - bowlofred - Sep-20-2020 Lists are indexed at 0, not at 1, so you'd have to subtract 1 if you want 1 to represent the first element. If you expected to return the first element of the second list, it would be list2[0] . So replace the 0 with (one less than) the result you had: list1[0] .Putting it together: list2[list1[0] - 1] >>> list1 = [1, 2, 3, 4, 5] >>> list2 = [6, 7, 8, 9, 10] >>> list2[list1[0] - 1] 6 RE: list1 position = list 2? - Nickd12 - Sep-20-2020 the items in the list are words not numbers I just used numbers as an example here is my code def cmd(text): CMD_INPUTS = ['pause'] CMD_OUTPUTS = ['input keyevent 85'] for word in text.split(): if word.lower() in CMD_INPUTS: returnthere will be more inputs and outputs but I only have one in there for now. What I want to do is if pause is in the text I want it go check the position of pause in the CMD_INPUTS and return the item that's in that same position but in CMD_OUTPUTS sorry I should've been more clear also note that its not like a one time return the return will always change based on the input RE: list1 position = list 2? - bowlofred - Sep-20-2020 list.index() gives you the position within a list.>>> INPUT=["the", "quick", "brown", "fox", "jumped"] >>> OUTPUT=["Now", "is", "the", "time", "for"] >>> word = "fox" >>> OUTPUT[INPUT.index(word)] 'time' RE: list1 position = list 2? - Nickd12 - Sep-20-2020 I think I got it idk if its sound though def test(text): list1 = ['1', '2', '3', '4', '5'] list2 = ['6', '7', '8', '9', '10'] for word in text.split(): if word.lower() in list1: x = list1.index(text) return list2[x] haha didn't see your reply yeah pretty much same thing but yours is a bit more clean so ill go with that but thank you. |