python - How to group items in an iterable object based on the first character of the item? -


To start with the sorted recurring object, I must put the item in group with their first letter (say each Group Z for letters and a group for numbers and symbols).

For a more concrete example, suppose I have this list:

L = ['A', 'A', 'AC', 'BA', 'BB' , 'BC', 'CA', 'CB', 'CC', '10', '% A', 'B', ...]

And I want something like this:

GL = [['A', 'A', 'AC'], ['BA', 'BB', 'BC'], ['ca', 'cb', 'cc'] ['10', '% A', 'B']]

What are the options to do this, and which is the most efficient? Import itertools as L = ['A', 'AB']

  'AC', 'BA', 'BB', 'BC', 'CA', 'CB', 'CC', '10', '% A', 'B'] Sorter = Lambda x: x [0] () Sorted ((L, key = sorter), key = sorter), for example X and X [0] .isalpha () other '}' GL = [for catalog (v) in k, v it.groupby (sorted) ]   

returns:

  [['A', 'AB', 'AC'], ['BA', 'BB', 'BC You must have all non-alpha-characters ('C', 'CB', 'CC'], ['10', '% A', 'B']]   

In sorting You can use something other than '}' to place it in the correct position.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -