Skip to content Skip to sidebar Skip to footer

Appending Specific Indices From Zipped Lists

I have been succesfull in filtering a list by appending the value of the list index if it's first three characters are digits. This filters out certain values, although the origina

Solution 1:

You may do what you want like this using only standard Python means:

# initial sample data
IN = [['502', 'a503', '-5.1.0', 'qwe', '999', '1 1 1'],
      [1, 2, 3, 4, 5, 6],
      ['a', 'b', 'c', 'd', 'e', 'f'],
      [0, 0, 0, 0, 0, 0]]

# dictionary with the data we want to filter (single dictionary instead of 3 lists)
data_in = {idx: IN[idx] for idx in range(3)}
print(data_in)

# empty dictionary to store filtered data
data_out = {idx: list() for idx in data_in}

# processingforpos, elem in enumerate(data_in[0]):
    # conditionif elem[:3].isdigit():
        # if condition is true then we treat all the data in the dictionaryfor idx in data_in:
            data_out[idx].append(data_in[idx][pos])

print(data_out)

Output:

{0: ['502', 'a503', '-5.1.0', 'qwe', '999', '1 1 1'], 1: [1, 2, 3, 4, 5, 6], 2: ['a', 'b', 'c', 'd', 'e', 'f']}
{0: ['502', '999'], 1: [1, 5], 2: ['a', 'e']}

Problems like this can be solved with libraries like pandas much easier and more efficiently with literally couple lines of code.

Post a Comment for "Appending Specific Indices From Zipped Lists"