0

I have a list of lists like this:

MyList = [[[[11, 12], [13, 14]], [[12, 13], [22, 23]], [[24, 34], [53, 54]], [[43, 44], [54, 55]]],
          [[[12, 13], [22, 23]], [[11, 12], [13, 14]], [[15, 25], [44, 54]], [[24, 34], [53, 54]]],
          [[[13, 14], [21, 31]], [[15, 25], [44, 54]], [[52, 53], [54, 55]]],
          [[[15, 25], [44, 54]], [[12, 13], [22, 23]], [[13, 14], [21, 31]]],
          [[[24, 34], [53, 54]], [[11, 12], [13, 14]], [[12, 13], [22, 23]]],
          [[[34, 35], [45, 55]], [[52, 53], [54, 55]]],
          [[[43, 44], [54, 55]], [[11, 12], [13, 14]]],
          [[[52, 53], [54, 55]], [[13, 14], [21, 31]], [[34, 35], [45, 55]]]]

In this list of lists, I have 8 items. I want to rename these items like this:

[[11, 12], [13, 14]] = 1
[[12, 13], [22, 23]] = 2
[[13, 14], [21, 31]] = 3
[[15, 25], [44, 54]] = 4
[[24, 34], [53, 54]] = 5
[[34, 35], [45, 55]] = 6
[[43, 44], [54, 55]] = 7
[[52, 53], [54, 55]] = 8

Finally, renamed list will look like this:

MyListRename = [[1, 2, 5, 7],
                [2, 1, 4, 5],
                [3, 4, 8],
                [4, 2, 3],
                [5, 1, 2],
                [6, 8],
                [7, 1],
                [8, 3, 6]]

What is the best way to do this in python?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • The items in a list—regardless of type—don't have names. Typically individual element are referenced with an integer index with `[]` brackets. – martineau Jun 06 '18 at 20:30

4 Answers4

2

(This solution can definitely be improved)

mydict = {
    ((11, 12), (13, 14)): 1,
    ((12, 13), (22, 23)): 2,
    ((13, 14), (21, 31)): 3,
    ((15, 25), (44, 54)): 4,
    ((24, 34), (53, 54)): 5,
    ((34, 35), (45, 55)): 6,
    ((43, 44), (54, 55)): 7,
    ((52, 53), (54, 55)): 8
}

newList = []
counter = 0

for outlist in myList:
    newList.append([]);

    for inList in outlist:
        obj = (tuple(inList[0]), tuple(inList[1]))
        newList[counter].append(mydict[obj])

    counter += 1

I'm using a dictionary where the keys are the tuples representing the lists you want to replace. Remember that lists are mutable, therefore not hashable.

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
0

you can use a function such as:

def replace_list (sub_list):
    if sub_list == [[11, 12], [13, 14]]: return 1
    if sub_list == [[12, 13], [22, 23]]: return 2
    if sub_list == [[13, 14], [21, 31]]: return 3
    if sub_list == [[15, 25], [44, 54]]: return 4
    if sub_list == [[24, 34], [53, 54]]: return 5
    if sub_list == [[34, 35], [45, 55]]: return 6
    if sub_list == [[43, 44], [54, 55]]: return 7
    if sub_list == [[52, 53], [54, 55]]: return 8
    return 0 #or any default value

and then do a double loop for to change the value in MyList:

for i in range(len(MyList)):
    for j in range(len(MyList[i])):
        MyList[i][j] = replace_list (MyList[i][j])

or even use map with list comprehension

MyList = [map(replace_list, subList) for subList in MyList]
Ben.T
  • 29,160
  • 6
  • 32
  • 54
0

I would do this programmatically, taking advantage of the fact that the first element of every list is the list of possible elements:

lookup_dict = {}
reverse_dict = {}
index = 0
for ele in MyList:
    lookup_dict[index] = ele[0]
    reverse_dict[repr(ele[0])] = index
    index += 1

Note: lists are normally not able to be used for a dictionary key. In this case, we use repr(...) to get the representation in string form to use as a key

which then lets us compress our list via:

result = [[reverse_dict[repr(subele)] + 1 for subele in ele] for ele in MyList]

[[1, 2, 5, 7], 
 [2, 1, 4, 5], 
 [3, 4, 8], 
 [4, 2, 3], 
 [5, 1, 2], 
 [6, 8], 
 [7, 1], 
 [8, 3, 6]]

and reconstitute the original via:

re_list = [[lookup_dict[subele - 1] for subele in ele] for ele in result]

>>> re_list == MyList
True

If you can also use a zero indexing, you can remove the +/- 1s to slightly reduce complexity.

TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
0

code

MyList = [
      [[[11, 12], [13, 14]], [[12, 13], [22, 23]], [[24, 34], [53, 54]], [[43, 44], [54, 55]]],
      [[[12, 13], [22, 23]], [[11, 12], [13, 14]], [[15, 25], [44, 54]], [[24, 34], [53, 54]]],
      [[[13, 14], [21, 31]], [[15, 25], [44, 54]], [[52, 53], [54, 55]]],
      [[[15, 25], [44, 54]], [[12, 13], [22, 23]], [[13, 14], [21, 31]]],
      [[[24, 34], [53, 54]], [[11, 12], [13, 14]], [[12, 13], [22, 23]]],
      [[[34, 35], [45, 55]], [[52, 53], [54, 55]]],
      [[[43, 44], [54, 55]], [[11, 12], [13, 14]]],
      [[[52, 53], [54, 55]], [[13, 14], [21, 31]], [[34, 35], [45, 55]]]
      ]

OtherList = [
    [[11, 12], [13, 14]],
    [[12, 13], [22, 23]],
    [[13, 14], [21, 31]],
    [[15, 25], [44, 54]],
    [[24, 34], [53, 54]],
    [[34, 35], [45, 55]],
    [[43, 44], [54, 55]],
    [[52, 53], [54, 55]]
    ]

NewList = [[OtherList.index(j) + 1 for j in i] for i in MyList]

print(NewList)

output

[[1, 2, 5, 7],
 [2, 1, 4, 5],
 [3, 4, 8],
 [4, 2, 3],
 [5, 1, 2],
 [6, 8],
 [7, 1],
 [8, 3, 6]]
S1M0N38
  • 131
  • 2
  • 10