List of categories in Python

What is the best way to categorize a list in python?

eg:

totalist is below

totalist[1] = ['A','B','C','D','E']
totalist[2] = ['A','B','X','Y','Z']
totalist[3] = ['A','F','T','U','V']
totalist[4] = ['A','F','M','N','O']

Let's say I want to get a list in which the first two elements ['A','B']are basically list[1]and list[2]. Is there an easy way to get them without repeating one element at a time? How is something like this?

if ['A','B'] in totalist

I know this is not working.

+4
source share
5 answers

You can check the first two elements of each list.

for totalist in all_lists:
    if totalist[:2] == ['A', 'B']:
        # Do something.

Note: The disposable solutions offered by Kasramvd are also very pleasant. I found my solution more readable. Although I have to say that understanding is a little faster than usual for loops. (What I experienced myself.)

+3
source

, itertools push- C:

from future_builtins import map  # Py2 only; not needed on Py3
from itertools import compress
from operator import itemgetter

# Generator
prefixes = map(itemgetter(slice(2)), totalist)
selectors = map(['A','B'].__eq__, prefixes)

# If you need them one at a time, just skip list wrapping and iterate
# compress output directly
matches = list(compress(totalist, selectors))

:

matches = list(compress(totalist, map(['A','B'].__eq__, map(itemgetter(slice(2)), totalist))))

. , totalist , , itertools.tee, , :

 totalist, forselection = itertools.tee(totalist, 2)

prefixes map forselection, totalist; compress , tee .

, , C, . - collections.defaultdict(list) list ( tuple, dict) list list, . N list, , , totaldict['A', 'B'], O(1) ( , ).

:

from collections import defaultdict

totaldict = defaultdict(list)
for x in totalist:
    totaldict[tuple(x[:2])].append(x)

# Optionally, to prevent autovivification later:
totaldict = dict(totaldict)

matches :

matches = totaldict['A', 'B']
+2

, python . , :

, , :

>>> [sub for sub in totalist if sub[:2] == ['A', 'B']]
[['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'X', 'Y', 'Z']]

, enumerate:

>>> [ind for ind, sub in enumerate(totalist) if sub[:2] == ['A', 'B']]
[0, 1]

Numpy, , :

>>> import numpy as np    
>>> 
>>> totalist = np.array([['A','B','C','D','E'],
...                      ['A','B','X','Y','Z'],
...                      ['A','F','T','U','V'],
...                      ['A','F','M','N','O']])

>>> totalist[(totalist[:,:2]==['A', 'B']).all(axis=1)]
array([['A', 'B', 'C', 'D', 'E'],
       ['A', 'B', 'X', 'Y', 'Z']], 
      dtype='|S1')

python, , , filter, , :

>>> list(filter(lambda x: x[:2]==['A', 'B'], totalist))
[['A', 'B', 'C', 'D', 'E'], ['A', 'B', 'X', 'Y', 'Z']]
+1

.

>>> for i in totalist:
...     if ['A','B']==i[:2]:
...             print i
+1

, (). , , . "" , .

If the need for filtering based on the first two elements is fixed (it does not generalize to the first n elements), then I would add the lists, as they are done, to the dict, where the key is a tuple of the first two elements and the element is a list of lists.

then you just retrieve your list by searching by type. This is easy to do and will bring potentially great acceleration, with virtually no memory and time costs when compiling lists.

0
source

Source: https://habr.com/ru/post/1653347/


All Articles