How to reorder list

Hi everyone!

I have list text original = [‘3A’, ‘2B’, ‘3A’, ‘2B’, ‘3A’,“4A”]

How to get new_list and reoder:
get = [‘3A’, ‘2A’, ‘3B’, ‘2B’, ‘3C’, “4A”]
Thank advance !!!

import collections
original = ['3A', '2B', '3A', '2B', '3A',"4A"]
duplicate = [item for item, count in collections.Counter(original).items() if count > 1]

#get duplicate index
def find_indices(list_check, item_find):
    return [idx for idx, value in enumerate(list_check) if value == item_find]

index_duplicate = []
for i in duplicate:
	index_duplicate.append(find_indices(original, i))
print(index_duplicate)
print("\n")
#how to get list of index

list_by_index = [['3A', '3A', '3A'], ['2B', '2B']]
print(list_by_index)
print("\n")

# and how to get new list
new_list = ['3A', '3B', '3C', '2A', '2B']
print(new_list)
print("\n")

#and get list sorted by index of original
get = ['3A', '2A', '3B', '2B', '3C', "4A"]
print(get)

Could you explain a bit more what you are trying to do? I find it hard to follow what you have, what you want and which rules you want to get there.

1 Like

Dear Sir
Thank for reply

I want update Family Type and resort it based on new type name and old type name.

example:
Family Type is unique so “3A” and “3A” is Not Good.
If new type duplicate, I want change by alphabet. A~Z

Hi,
a solution with a dictionary

import sys

original = ['3A', '2B', '3A', '2B', '3A',"4A"]

out = []
# create a dict to store letter (start letter is 'A')
dict_suffix = {i[0] : "A"  for i in original}

for i in original:
    current_suffix = dict_suffix[i[0]]
    out.append(i[0] + current_suffix)
    # update the dict and increment the letter
    dict_suffix[i[0]] = chr(ord(current_suffix)+ 1)
OUT = out
1 Like

I still confuse with what you want, if you want sort by alphabet with A=>Z can consider :

a = ["1A", "2X", "3R", "4D", "5B", "6F", "7G", "8H"]
## sort a by the last character of each string
a.sort(key=lambda x: x[-1])
print(a)

Result : ['1A', '5B', '4D', '6F', '7G', '8H', '3R', '2X']

1 Like

HI @manhgt214 ,

I think this is the workflow you’re looking for:


2023-05-12 Unique Combinations From Numbers.dyn (16.9 KB)

PS: I realise now that the List.SortByKey is a bit overkill, List.Sort would work too.

2 Likes

Hi Mr @c.poupin Thank you so much!! <3

@chuongmep, @Daan thank for answer I think Mr @c.poupin 's solution is best for me

1 Like

Hi change a litle bit, I can get new list by add prefix and same index of old list.

import sys
import re

original = ['B-3A',"G-65BA",'250B', '30A', '250B', '30A',"450A","B-700AB"]

out = []
# create a dict to store letter (start letter is 'A')
dict_suffix = {i[:4] : "A"  for i in original}

for i in original:
    current_suffix = dict_suffix[i[:4]]
    out.append("JW-" + re.findall(r'\d+', i)[0] + current_suffix)
    # update the dict and increment the letter
    dict_suffix[i[:4]] = chr(ord(current_suffix)+ 1)
print(out)

#result: ['JW-3A', 'JW-65A', 'JW-250A', 'JW-30A', 'JW-250B', 'JW-30B', 'JW-450A', 'JW-700A']