0

How can I create a 2 column list for X (unknown) amounts of rows? I need to be able to extend/append the list and edit single entries afterward.

def get_types(sequence_list):
    seq_types = [[], []]

    for sequence in sequence_list:
        if sequence.seq_type not in seq_types:
            seq_types.append(sequence.seq_type)
        elif sequence.seq_type in seq_types:
            seq_index = seq_types.index(sequence.seq_type)
            # now do +1 on a second column of that seq_index

    return seq_types

I could probably do this with two separate lists and zip() them afterward but that feels somewhat wrong.

kpie
  • 9,588
  • 5
  • 28
  • 50
Niclas
  • 25
  • 1
  • 1
  • 6
  • 1
    It's hard to follow what's going on here. Could you give us an example of what the input and output to this function look like? – Ben Grossmann Feb 21 '22 at 20:46
  • @BenGrossmann, so sequence.seq_type will be a string, and the if basically checks if that string already exists in the list. If it does not exist it will append that string to the list. If it already exists it will take the second column of that already existing string and add +1 to the "counter" column. – Niclas Feb 21 '22 at 20:51
  • That way I am trying to "analyze" the number of "duplicate" entries in a file. – Niclas Feb 21 '22 at 20:51
  • 2
    Based on what your function does, it *seems* that you really want to use a dictionary whose keys are the `seq_types` and whose values default to `1` and get incremented each time they're discovered. – Michael Ekoka Feb 21 '22 at 20:53
  • 1
    This sounds like an [X-Y Problem](//meta.stackexchange.com/q/66377/174780). It looks like you're trying to count the number of sequences for each sequence type, for which you should use a dictionary where the keys are `sequence.seq_type` and values are the count for that type, instead of two lists. Or, use `collections.Counter` See https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item – Pranav Hosangadi Feb 21 '22 at 21:00
  • A dictionary looks amazing for what I am trying to achieve :) Thank you very much! – Niclas Feb 21 '22 at 21:09

1 Answers1

1
def dupe_count(sequence_list):
    rv = {}
    for s in sequence_list:
        try:
            rv[s.seq_type] += 1 
        except KeyError:
            rv[s.seq_type] = 1 
    return rv

You can then turn this into a list of tuples (or lists, as the case may be) by itemizing the dict that it returns.

Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79