3

I have format a list of lists with parent_id, id and name like cascade style.

My input as follows:

category = [['id','name','parent_id'],[1, 'Root', 0],[10, 'Beans', 4],[2, 'Sub Root', 1],[3, 'Fruits', 2],[4, 'Veg', 2],[5, 'Apple', 3],[6, 'Onion', 4]]

And my excepted output follows as

out_category = [[1, 'Root', 0],[2, 'Sub Root', 1],[3, 'Fruits', 2],[4, 'Veg', 2],[5, 'Apple', 3],[6, 'Onion', 4],[10, 'Beans', 4]]

I tried so far

out_category = []
for item in category[1:]:
    print item[0].split(',')
    categ = item[0].split(',')
    out_category.append(filter(lambda x: x[0]==categ[2],categ))
print out_category
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

3 Answers3

3

Use filter for remove non int and sorted with key for search by first item:

sorted(filter(lambda x: isinstance(x[0], int), category), key=lambda x: x[0])

If it is difficult to understand, in two lines it looks like this:

# Remove titles (first element of category)
without_first_string_list = filter(lambda x: isinstance(x[0], int), category)

# Or you can use if this list always have only one list with titles,
#                          but if not, the sorting may be incorrect
without_first_string_list = category[1:]

# Sort by first item
sorted_list = sorted(without_first_string_list, key=lambda x: x[0])
JRazor
  • 2,707
  • 18
  • 27
  • 1
    In this case you don't even need the key. Sorting will do the right thing and use the first element in the list. – Roland Smith May 05 '16 at 17:09
  • I was trying to show how to explicitly sort on the first key. In another situation (e.g. sort by last key) sorting will behave differently and the Asker will not understand what the problem is. But yes, you are right, the first key will be `sorted` and sorting without a `key` – JRazor May 05 '16 at 17:12
0

Alternatively you could do this pretty readably in a list comprehension:

sorted([item for item in category if type(item[0])==int])
jfbeltran
  • 1,808
  • 3
  • 13
  • 17
0

category looks like the output of the csv module. If that's the case, you can skip the headers when you parse it by reading and discarding the first line before you read the rest of the file.

Anyway, if you want to exclude the first list in the output and then sort according to the first element in each nested list, the simplest solution would be this:

out_category = sorted(category[1:])

If you want to sort by any list index (x[0], x[1] or x[2]), you can use sorted and pass a lambda as key:

out_category = sorted(category[1:], key=lambda x : x[0]) 
jDo
  • 3,962
  • 1
  • 11
  • 30
  • What am I missing? – jDo May 05 '16 at 17:15
  • Are there any reasons why you duplicated an answer? – JRazor May 05 '16 at 17:17
  • @JRazor I didn't duplicate anything. Whose answer are you referring to? – jDo May 05 '16 at 17:19
  • @JRazor Ah, the last line of your answer is almost the same. I just saw you using `filter`, `isinstance`, etc. and thought it was unnecessary. Anyway, you're abusing the voting system if you use it to "fight off competition"; it should be used to judge content. – jDo May 05 '16 at 17:31
  • what? Your answer repeats my with a difference in 20 minutes. I don't understand why you're putting the questions duplicates – JRazor May 05 '16 at 17:56
  • @JRazor Are you implying that I actually looked at your answer, thought *"How brilliant! Lemme just steal this, thank you very much!"*, and then posted your code in different words? :D If that's the case, I'm sorry but this is generic stuff... If anything, we should both be looking for duplicate *questions* since I'm sure there are plenty. To answer your question (again), I hadn't seen that you're doing the same operation - probably because you've split it into two lines. I just noticed the `filter` and `isinstance` at the top and thought they were unnecessary. – jDo May 05 '16 at 19:23
  • Firstly, I was not talking about it. If the answer is already there - no need to duplicate, in order not to confuse the author of the question and not clutter up Stackoverflow. Secondly, if the answer is already there, you can remove your response. I've done it many times, if someone wrote a similar response to mine and instead he put a plus, because I agree with the answer. isinstance is not superfluous - it allows to filter with a 100% chance of invalid strings. I know this because I've been doing Python for over 6 years. – JRazor May 05 '16 at 19:53
  • @JRazor I've added a simpler solution since I don't think the `lambda` is needed as long as we can skip the first element in the list. Since that approach is not in your answer, there shouldn't be any need for you to down-vote and complain about duplication. – jDo May 05 '16 at 20:36