How to group a list of tuples based on the strings they contain?

david_10001

I have a sorted list of two-string tuples. I also have another list of 4 character strings that are in some of those tuples. I would like to group from one of these 4 character strings to the next in the list. It is a bit hard to explain so I will demonstrate.

original_list = [('1321', 01), ('MessageXZY', 02), ('DescriptionSKS', 03), ('S7_6', 04), ('S7_3', 05), ('0A3B', 06), ('MessageZYA', 07),
 ('DescriptionKAM', 08), ('9K44', 09), ('MessageYAL', 10),
 ('DescriptionAUS', 11), ('S7_2', 12)]

I have the terms 1321, OA3B and 9K44 saved in another list. I would like to group everything between (and including) these terms into a tuple, like so:

grouped_list = [(('1321', 01), ('MessageXZY', 02), ('DescriptionSKS', 03), ('S7_6', 04), ('S7_3', 05)), (('0A3B', 06), ('MessageZYA', 07),
 ('DescriptionKAM', 08)), (('9K44', 09), ('MessageYAL', 10),
 ('DescriptionAUS', 11), ('S7_2', 12))]

If the list that contains my 4 character terms is called code and the list containing the tuples is called original_list, what code would I need to achieve this?

Edit: This is where I have gotten up to:

grouped_list = []
for tuple in original_list:
    for string in tuple:
        if string in code:
            grouped_list = list(zip ##zip that tuple and all consecutive tuples until the next item in code
Gunjan

I am assuming that you have list of codes with which you want to split on. According to that see if this code works for you.

original_list = [('1321', '01'), ('MessageXZY', '02'), ('DescriptionSKS', '03'), ('S7_6', '04'), ('S7_3', '05'), ('0A3B', '06'), ('MessageZYA', '07'), ('DescriptionKAM', '08'), ('9K44', '09'), ('MessageYAL', '10'),
 ('DescriptionAUS', '11'), ('S7_2', '12')]

code_list = ['1321', '0A3B','9K44']


grouped_tuples = []
for entry in original_list:
    if entry[0] in code_list:
        new_tuple = []
        new_tuple.append(entry)
        for i in range(original_list.index(entry)+1, len(original_list)):
            if(original_list[i][0] not in code_list):
                new_tuple.append(original_list[i])
            else:
                break
        grouped_tuples.append(tuple(new_tuple))
print grouped_tuples

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to group list of tuples?

How to sort a list of tuples that contain numpy array?

Reordering strings in a list based on number they contain

How to convert list of tuples into list of strings with no commas

How to replace group of strings in pandas series based on a dictionary with values as list?

List of indices of tuples of tuples that contain certain tuples

how to sort a list of tuples based on another list

How to convert a list of strings representing tuples to tuples of integers?

How to group list items into sequential tuples in Python?

How to group tuples in a list with multiple values matching

how to group by a list of tuples using the inex

How to obtain the smallest value in this list of lists which contain tuples?

How to sort list of tuples that contain a directed acyclical graph?

Quickly remove tuples that contain tuples of other list

Pythonic approach to pull and group list items based on a list of indices with tuples and non-tuples

convert a list of strings to tuples?

List of strings to tuples

Select pandas dataframe columns based on which names contain strings in list

Group list of tuples by item

Group list of tuples efficiently

List to group of tuples

How can I evaluate a list of strings as a list of tuples in Python?

How to get all permutations of string as list of strings (instead of list of tuples)?

Convert list of strings of tuples to list of tuples

How can I group tuples inside a list that have a similar value of one item and then sort them based on another item?

Converting list of strings to list of tuples

Elixir List of Tuples to List of Strings

Python: Compare list of strings to list of tuples, and create a new list based on match or no match

How to group strings based on one of their substring?