简体   繁体   中英

Find index of element in sublist that is same across all the sublists in a nested list

I have a nested list like this:

[[8.0, 16.275953626987484, 5.923962654520423],
   [8.0, 3.0076746636087575, 17.05737063904884]),
  ([8.0, 3.0076746636087575, 17.05737063904884],
   [8.0, -13.268278963378728, 11.133407984528427]),
  ([8.0, -13.268278963378728, 11.133407984528427],
   [8.0, -16.275953626987487, -5.923962654520416]),
  ([8.0, -16.275953626987487, -5.923962654520416],
   [8.0, -3.0076746636087694, -17.057370639048848]),
  ([8.0, -3.0076746636087694, -17.057370639048848],
   [8.0, 13.268278963378716, -11.133407984528437]),
  ([8.0, 13.268278963378716, -11.133407984528437],
   [8.0, 16.275953626987484, 5.923962654520423]]

In that list, I want to remove the elements in the sublists that have the same values in the same index, for instance, here I want to remove 8 from all the sublists since its the same across all the sublists at index 0.

How do I do it in a short and simple way without too many hard-coded conditions?

One thing to note here is that the sublists will always have 3 elements while the length of the outer nested list can be anything.

Your problem seems a bit odd, but this is a solution:

example = [
  ([8.0, 16.275953626987484, 5.923962654520423],
   [8.0, 3.0076746636087575, 17.05737063904884]),
  ([8.0, 3.0076746636087575, 17.05737063904884],
   [8.0, -13.268278963378728, 11.133407984528427]),
  ([8.0, -13.268278963378728, 11.133407984528427],
   [8.0, -16.275953626987487, -5.923962654520416]),
  ([8.0, -16.275953626987487, -5.923962654520416],
   [8.0, -3.0076746636087694, -17.057370639048848]),
  ([8.0, -3.0076746636087694, -17.057370639048848],
   [8.0, 13.268278963378716, -11.133407984528437]),
  ([8.0, 13.268278963378716, -11.133407984528437],
   [8.0, 16.275953626987484, 5.923962654520423])
]


def unchanging_mask(data):
    def all_inner_iterables(xss):
        if xss and isinstance(xss[0], (list, tuple)):
            for xs in xss:
                yield from all_inner_iterables(xs)
        else:
            yield xss

    return list(map(lambda xs: len(set(xs)) == 1, zip(*all_inner_iterables(data))))


def select_columns(data, mask):
    if data and isinstance(data[0], (list, tuple)):
        return type(data)(select_columns(xs, mask) for xs in data)
    else:
        return type(data)(x for i, x in enumerate(data) if not mask[i])


mask = unchanging_mask(example)
result = select_columns(example, mask)
print(result)

It works by determining a mask first, which tells it whether or not a column always has the same value for each element at the deepest level of the nesting.

It then applies that mask to all the values a the deepest level of the the nested structure, preserving type.

It does assume the elements at the deepest level all have the same size.

Here is a list comprehension approach

data = [
    [8.0, 16.275953626987484, 5.923962654520423],
    [8.0, 3.0076746636087575, 17.05737063904884],
    [8.0, 3.0076746636087575, 17.05737063904884],
    [8.0, -13.268278963378728, 11.133407984528427],
    [8.0, -13.268278963378728, 11.133407984528427],
    [8.0, -16.275953626987487, -5.923962654520416],
    [8.0, -16.275953626987487, -5.923962654520416],
    [8.0, -3.0076746636087694, -17.057370639048848],
    [8.0, -3.0076746636087694, -17.057370639048848],
    [8.0, 13.268278963378716, -11.133407984528437],
    [8.0, 13.268278963378716, -11.133407984528437],
    [8.0, 16.275953626987484, 5.923962654520423]
]


index_to_remove = [i for i in range((len(data[0]))) if len({_[i] for _ in data}) == 1]
print(index_to_remove)
# [0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM