Accessing lists a referenced element is inside of

Holsterbau

Let's say I got a list of lists, like so:

x = [
["spam", "bacon", "eggs", "tomatoes"]
]

Then I make a reference to an element inside of the first element (list) in the list.

y = x[0][1]  # which should be "bacon"

Now I wish to access, having only reference y at disposal, other elements that are with "bacon", and maybe even lists next to the list "bacon" is part of. Here is more specifically what I would need that for:

y = x[0][1]
z = x[0][0]

class Thing:
    def __init__(self, stuff)
        self.stuff = stuff
    def checkstuff()
        # if stuff from different instance of class is member of the same list, things happen

spam = Thing(stuff=y)
bacon = Thing(stuff=z)
pjmaracs

Question is a bit vague, would add comment normally but rep too low. Here's my guess at what you want...

x = [
["spam", "bacon", "eggs", "tomatoes"],
["spam", "bacon", "eggs", "tomatoes"],
["spam", "notthisone", "eggs", "tomatoes"],
["spam", "bacon", "eggs", "tomatoes"],
["spam", "orthisone", "eggs", "tomatoes"]
]

y = x[0][1]
print(y)
indicesContaining = []
for i in range(len(x)):
    for string in x[i]:
        if string == y and i not in indicesContaining:
            indicesContaining.append(i)
print (indicesContaining)

This gets you the indices of the outer list in which the element y is contained. Edit: answer is now outdated, question changed

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Accessing list of lists inside a for

Accessing an element inside a Vuejs component

Accessing an element inside an Array of an Array

Accessing lists inside a dictionary with Django Template tags

Accessing an element inside another element JavaScript data

Sorting and accessing tuples in lists by the element at a given index

Accessing Element in Array Inside Struct Dynamically Allocated

Accessing an element inside an array with multiple objects

Accessing a div inside of a Polymer element template

accessing an element in <html> inside <html> with selenium python

Time complexity for accessing element inside dictionary

how to count element inside lists of a dictionary in python?

How to merge two element inside a list of lists?

AngularJS Accessing DOM element by id inside ng-repeat

Accessing Element inside Listview gives java.lang.NullPointerException

Accessing an array element inside a struct (which is part of an array of structs) (in C)

Angular: Accessing a template reference variable located inside `async` pipe element

Accessing child Components inside the parent component's html element

Accessing cshtml in referenced webapi project

Accessing dictionary values that are lists of lists

Distance from smallest to the largest element in lists (inside a list)

Split list into lists based on a character occurring inside of an element

How append li element inside a ul lists with the same class name?

Prolog - Working with a list of lists and an element inside one of the sublists

Indices of cross-referenced lists

React image element referenced

Creating referenced table element

Accessing list of list of lists

accessing nested lists in R