Transferring Values in nested dictionaries/lists - python

list = [[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
I would like to check if the number 1 is in the third column of all the nested lists, if it is than it should replace the 1 with a 0 and the 2 in that list with a 1.
Thanks in advance

Try the following:
nested_lists = [[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
for list_ in nested_lists:
if list_[2] == 1:
list_[2] = 0
list_ = [1 if n == 2 else n for n in list_]
After execution, nested_lists goes from the given
[[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2],
[2, 3, 1, 0, 0], [3, 0, 1, 2, 0], [2, 0, 1, 3, 0]]
To
[[1, 2, 3, 0, 0], [0, 0, 3, 2, 1], [1, 0, 0, 3, 2]
[1, 3, 0, 0, 0], [3, 0, 0, 1, 0], [1, 0, 0, 3, 0]]

Related

assigning class name to list column in pandas

I have a column in dataframe of type list
categories
[0, 0, 2, 2, 2]
[0, 0, 2, 2]
[0, 0, 2, 2, 2]
[1, 1, 2, 2]
[2, 2, 0, 0]
[1, 0, 2, 3]
here is the sample list
li = [[0, 0, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [1, 1, 2, 2], [2, 2, 0, 0], [1, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2], [1, 1, 2], [0, 2, 2, 0], [0, 0, 2, 2], [0, 1], [0, 0], [0, 0, 2, 2], [0, 0], [0, 0, 2, 2], [0, 2, 2, 0], [2, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [0, 0, 2, 2], [2, 2, 0, 1], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [2, 1], [0, 0, 2, 2, 2], [2, 2, 0, 0], [2, 0], [2, 2, 0, 0], [0, 2], [0, 2, 2], [0, 0, 2, 2], [0, 2, 2, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [0, 0, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [3, 2, 0, 0], [0, 0], [0, 0, 2, 2], [0, 0, 2, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [1, 3], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 2, 0, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2, 0, 0], [2, 2], [0, 0, 2, 2], [0, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2], [2], [0, 0, 2, 2], [2, 2, 2, 1, 1], [0, 0], [0, 3], [2, 2], [1, 2], [1,3]]
I want to create a new column (class_name) based on the following rule
The rules are based on priority and should be done one after other
if 1 and 3 are present, set class_name to class1
On the remaining rows, wherever 1 is present, set class_name to class2
On the remaining rows, wherever 3 is present, set class_name to class3
if 0 and 2 are present, set class_name to class4
On the remaining rows, wherever 0 is present, set class_name to class5
On the remaining rows, wherever 2 is present, set class_name to class6
What I have tried so far
df.loc[:, "class_name"] = None
for index, row in df.iterrows():
if row["class_name"] == None:
categories = list(row["categories"])
if 1 in categories and 3 in categories:
df.loc[index, "class_name"] = "class1"
Similarly, for each condition I have a separate loop.. but it's too slow.. is there a way to do it without looping ?
I think I understood the question correctly. And I tried something like this, it seems to work fine.
import pandas as pd
li = [[0, 0, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [1, 1, 2, 2], [2, 2, 0, 0], [1, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2], [1, 1, 2], [0, 2, 2, 0], [0, 0, 2, 2], [0, 1], [0, 0], [0, 0, 2, 2], [0, 0], [0, 0, 2, 2], [0, 2, 2, 0], [2, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0, 0], [0, 0, 2, 2], [2, 2, 0, 1], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [2, 1], [0, 0, 2, 2, 2], [2, 2, 0, 0], [2, 0], [2, 2, 0, 0], [0, 2], [0, 2, 2], [0, 0, 2, 2], [0, 2, 2, 0], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2, 2], [0, 0, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [3, 2, 0, 0], [0, 0], [0, 0, 2, 2], [0, 0, 2, 2, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [1, 3], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 0, 2, 2], [0, 2, 0, 2], [0, 0, 2, 2], [2, 2, 0, 0], [2, 2, 0, 0], [2, 2], [0, 0, 2, 2], [0, 2], [0, 0, 2, 2], [0, 0, 2, 2], [2, 2, 0], [2, 2, 0, 0], [0, 0, 2, 2], [0, 0, 2], [2], [0, 0, 2, 2], [2, 2, 2, 1, 1], [0, 0], [0, 3], [2, 2], [1, 2], [1,3]]
df=pd.DataFrame(data={'category':li})
def check(x):
class_name=False
if all(item in x for item in [1,3]):
class_name='class1'
elif not all(item in x for item in [1,3]) and 1 in x:
class_name='class2'
elif not all(item in x for item in [1,3]) and 3 in x:
class_name='class3'
elif all(item in x for item in [0,2]):
class_name='class4'
elif 0 in x:
class_name='class5'
elif 2 in x:
class_name='class6'
else:
class_name='no_class'
return class_name
df['check']=df['category'].apply(lambda x: check(x))
print(df)
'''
category check
66 [1, 3] class1
92 [1, 3] class1
3 [1, 1, 2, 2] class2
5 [1, 0, 2, 2] class2
17 [1, 1, 2] class2
20 [0, 1] class2
32 [2, 2, 0, 1] class2
41 [2, 1] class2
87 [2, 2, 2, 1, 1] class2
91 [1, 2] class2
59 [3, 2, 0, 0] class3
89 [0, 3] class3
0 [0, 0, 2, 2, 2] class4
'''
Okay, so I ran your problem using a couple of different methods. The fastest of them all was using pandas.DataFrame.apply. Here's the code:
from __future__ import annotations
import pandas as pd
def class_name(row: list) -> str | None:
if 1 in row and 3 in row:
return "class1"
if 1 in row:
return "class2"
if 3 in row:
return "class3"
if 0 in row and 2 in row:
return "class4"
if 0 in row:
return "class5"
if 2 in row:
return "class6"
return None
# == How to use ============================
df["class_name"] = df["categories"].apply(class_name)
# Result using on a dataframe with 186000 rows:
"""
CPU times: user 80.9 ms, sys: 904 µs, total: 81.8 ms
Wall time: 82 ms
"""
Other Implementations I Tried
I've also tried some other implementations to compare. Here's them:
from __future__ import annotations
import pandas as pd
# == Code to Generate Sample DataFrame ==============
li = [
[0, 0, 2, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2, 2],
[1, 1, 2, 2],
[2, 2, 0, 0],
[1, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2, 2],
[0, 0, 2, 2],
[2, 2, 0, 0],
[2, 2, 0, 0],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[2, 2, 0, 0],
[2, 2],
[1, 1, 2],
[0, 2, 2, 0],
[0, 0, 2, 2],
[0, 1],
[0, 0],
[0, 0, 2, 2],
[0, 0],
[0, 0, 2, 2],
[0, 2, 2, 0],
[2, 2, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[2, 2, 0, 0],
[0, 0, 2, 2],
[2, 2, 0, 1],
[2, 2, 0, 0],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2, 2],
[2, 1],
[0, 0, 2, 2, 2],
[2, 2, 0, 0],
[2, 0],
[2, 2, 0, 0],
[0, 2],
[0, 2, 2],
[0, 0, 2, 2],
[0, 2, 2, 0],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2, 2],
[0, 0, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[3, 2, 0, 0],
[0, 0],
[0, 0, 2, 2],
[0, 0, 2, 2, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[1, 3],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[0, 2, 0, 2],
[0, 0, 2, 2],
[2, 2, 0, 0],
[2, 2, 0, 0],
[2, 2],
[0, 0, 2, 2],
[0, 2],
[0, 0, 2, 2],
[0, 0, 2, 2],
[2, 2, 0],
[2, 2, 0, 0],
[0, 0, 2, 2],
[0, 0, 2],
[2],
[0, 0, 2, 2],
[2, 2, 2, 1, 1],
[0, 0],
[0, 3],
[2, 2],
[1, 2],
[1, 3],
]
def make_df(size=1):
return pd.DataFrame({'categories': [v for i in range(size) for v in li]})
# == Implementation 1 ============================
def class_name2(row: list) -> str | None:
categories = row["categories"]
if 1 in categories and 3 in categories:
return "class1"
if 1 in categories:
return "class2"
if 3 in categories:
return "class3"
if 0 in categories and 2 in categories:
return "class4"
if 0 in categories:
return "class5"
if 2 in categories:
return "class6"
return None
df = make_df(2000)
df["class_name"] = df.apply(class_name2, axis=1)
# Result:
"""
CPU times: user 1.69 s, sys: 17 ms, total: 1.71 s
Wall time: 1.71 s
"""
# == Implementation 2 ============================
# This is your original implementation
df = make_df(2000)
df.loc[:, "class_name"] = None
for index, row in df.iterrows():
if row["class_name"] == None:
categories = list(row["categories"])
if 1 in categories and 3 in categories:
df.loc[index, "class_name"] = "class1"
elif 1 in categories:
df.loc[index, "class_name"] = "class2"
elif 3 in categories:
df.loc[index, "class_name"] = "class3"
elif 0 in categories and 2 in categories:
df.loc[index, "class_name"] = "class4"
elif 0 in categories:
df.loc[index, "class_name"] = "class5"
elif 2 in categories:
df.loc[index, "class_name"] = "class6"
# Result:
"""
CPU times: user 24.2 s, sys: 65.6 ms, total: 24.3 s
Wall time: 24.5 s
"""
# == Implementation 3 ============================
# This is your original implementation without the if statement
df = make_df(2000)
df.loc[:, "class_name"] = None
for index, row in df.iterrows():
categories = list(row["categories"])
if 1 in categories and 3 in categories:
df.loc[index, "class_name"] = "class1"
elif 1 in categories:
df.loc[index, "class_name"] = "class2"
elif 3 in categories:
df.loc[index, "class_name"] = "class3"
elif 0 in categories and 2 in categories:
df.loc[index, "class_name"] = "class4"
elif 0 in categories:
df.loc[index, "class_name"] = "class5"
elif 2 in categories:
df.loc[index, "class_name"] = "class6"
# Result:
"""
CPU times: user 24 s, sys: 91.2 ms, total: 24.1 s
Wall time: 24.3 s
"""
# == Implementation 4 ============================
# This is your original implementation without the if statement
# and the list conversion
df = make_df(2000)
df.loc[:, "class_name"] = None
for index, row in df.iterrows():
categories = row["categories"]
if 1 in categories and 3 in categories:
df.loc[index, "class_name"] = "class1"
elif 1 in categories:
df.loc[index, "class_name"] = "class2"
elif 3 in categories:
df.loc[index, "class_name"] = "class3"
elif 0 in categories and 2 in categories:
df.loc[index, "class_name"] = "class4"
elif 0 in categories:
df.loc[index, "class_name"] = "class5"
elif 2 in categories:
df.loc[index, "class_name"] = "class6"
# Result:
"""
CPU times: user 23.4 s, sys: 80 ms, total: 23.5 s
Wall time: 24.2 s
"""
# == Implementation 5 ============================
# Using `swifter`. Install swifter before trying this one:
# pip install swifter
import swifter
def class_name(row: list) -> str | None:
if 1 in row and 3 in row:
return "class1"
if 1 in row:
return "class2"
if 3 in row:
return "class3"
if 0 in row and 2 in row:
return "class4"
if 0 in row:
return "class5"
if 2 in row:
return "class6"
return None
df = make_df(2000)
df["class_name"] = df["categories"].swifter.apply(class_name)
# Result:
"""
CPU times: user 572 ms, sys: 11 ms, total: 582 ms
Wall time: 930 ms
"""
Summary
Here's a summary of all the results:
Implementation
Total Time
Times Faster
Best
82 ms
300x
Implementation 1
1.71 s
14.3x
Implementation 2
24.5 s
1x
Implementation 3
24.3 s
1.008x
Implementation 4
24.2 s
1.012x
Implementation 5
930 ms
26x

get lines where value does not exist

I'm trying to get from 2D list the lines where value = 2 does not exists:
[[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
I've been using:
for i in range(len(list)):
if list[i] in [list]:
# dot stuff
the problem here is that evrytime the code is using the actual line which is list[i] and ignore the the rest even if 2 does not exists on it
hope Iwas clear
Thank you
something like the below
lst = [[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
lst = [x for x in lst if 2 not in x]
print(lst)
output
[[0, 0, 1, 1], [0, 0, 1, 0]]
Is this your expected output?
[0, 0, 1, 1]
[0, 0, 1, 0]
This is the working code:
a = [[0, 0, 1, 1],
[2, 1, 0, 2],
[2, 2, 2, 2],
[0, 0, 1, 0]]
for i in range(len(a)):
if 2 not in a[i]:
print(a[i])

Need a recursive function to get all permutations of an array where each element is itself plus 0 to n

Sorry for the wording of the title as I am unsure how to phrase the question.
I am trying to get all permutations of an array where each element could be it's value plus 0 to n ('wild' value)
e.g.
The array [0, 1, 0, 2, 1] with the wild value equal to 1 would have the permutations:
[1, 1, 0, 2, 1]
[0, 2, 0, 2, 1]
[0, 1, 1, 2, 1]
[0, 1, 0, 3, 1]
[0, 1, 0, 2, 2]
The array [1, 2, 0, 0] with the wild value equal to 2 would have the permutations:
[3, 2, 0, 0]
[2, 3, 0, 0]
[2, 2, 1, 0]
[2, 2, 0, 1]
[1, 4, 0, 0]
[2, 3, 0, 0]
[1, 3, 1, 0]
[1, 3, 0, 1]
[1, 2, 2, 0]
[2, 2, 1, 0]
[1, 3, 1, 0]
[1, 2, 1, 1]
... and so on...
This is the code I have tried, but it is not producing the desired results:
def generateAllMatrices(length, buckets, ind, wild):
if ind == length:
# possible_buckets.append(buckets.copy())
print(buckets)
return
if wild != 0:
for i in range(1, wild + 1):
buckets[ind] += 1
generateAllMatrices(length, buckets, 0, wild - 1)
buckets[ind] -= wild
generateAllMatrices(length, buckets, ind + 1, wild)
An example result produced from the above code is:
Original = [1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
Wild = 1
Permutations:
[2, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 1, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 1, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 3, 0, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 1, 1, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 2, 0, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 1, 0, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 1, 0, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 1, 1, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 2, 1, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 2, 0, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 1, 0]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 1]
[1, 0, 0, 2, 0, 1, 0, 0, 0, 1, 1, 0, 0]
Are there any similar algorithms I could reference for this? Or what route should I take regarding developing something that will produce what I need.
Thanks!
You could do the following:
import itertools
def make_reps(l, wild):
for indices in itertools.product(range(len(l)), repeat=wild):
new_l = list(l)
for i in indices:
new_l[i] += 1
yield new_l
With your given examples:
In [12]: list(make_reps([0, 1, 0, 2, 1], 1))
Out[12]:
[[1, 1, 0, 2, 1],
[0, 2, 0, 2, 1],
[0, 1, 1, 2, 1],
[0, 1, 0, 3, 1],
[0, 1, 0, 2, 2]]
In [14]: list(make_reps([1, 2, 0, 0], 2))
Out[14]:
[[3, 2, 0, 0],
[2, 3, 0, 0],
[2, 2, 1, 0],
[2, 2, 0, 1],
[2, 3, 0, 0],
[1, 4, 0, 0],
[1, 3, 1, 0],
[1, 3, 0, 1],
[2, 2, 1, 0],
[1, 3, 1, 0],
[1, 2, 2, 0],
[1, 2, 1, 1],
[2, 2, 0, 1],
[1, 3, 0, 1],
[1, 2, 1, 1],
[1, 2, 0, 2]]

Create a 5 x 5 matrix using Nested list comprehensions [duplicate]

This question already has answers here:
How to define a two-dimensional array?
(30 answers)
Closed 3 years ago.
Matrices can be represented as a list of lists in Python.
For example a 5 x 5 matrix with values 0 to 4 in each row can be written as:
matrix = [[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]
How to recreate this matrix by using nested listed comprehensions?
Here is my code:
num = [[num1,num2,num3,num4,num5] for num1 in range(5) for num2 in range(5)for num3 in range(5)for num4 in range(5)for num5 in range(5)]
And it produce the following output:
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 2],
[0, 0, 0, 0, 3],
[0, 0, 0, 0, 4],
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 2],
[0, 0, 0, 1, 3],
[0, 0, 0, 1, 4],
[0, 0, 0, 2, 0],
[0, 0, 0, 2,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 1],
[1, 2, 3, 4, 2],
[1, 2, 3, 4, 3],
[1, 2, 3, 4, 4],
[1, 2, 4, 0, 0],
[1, 2, 4, 0, 1],
[1, 2, 4, 0, 2],
[1, 2, 4, 0, 3],
[1, 2, 4, 0, 4],
[1, 2, 4, 1, 0],
[1, 2, 4, 1, 1],
[1, 2, 4, 1, 2],
[1, 2, 4, 1, 3],
[1, 2, 4, 1, 4],
[1, 2, 4, 2, 0],
[1, 2, 4, 2, 1],
[1, 2, 4, 2, 2],
[1, 2, 4, 2, 3],
[1, 2, 4, 2, 4],
[1, 2, 4, 3, 0],
[1, 2, 4, 3, 1],
[1, 2, 4, 3, 2],
[1, 2, 4, 3, 3],
[1, 2, 4, 3, 4],
[1, 2, 4, 4, 0],
[1, 2, 4, 4, 1],
[1, 2, 4, 4, 2],
[1, 2, 4, 4, 3],
[1, 2, 4, 4, 4]]
Please someone help me to solve this.
Try this,
>>> [[j for j in range(5)] for i in range(5)]
Output:
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]

Sort a list of lists containing integers

I've got a list of lists containing integers sorted at the moment by the sum of the contents:
[[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
I would like to sort them in ascending order by the common structure of its contents i.e like
[[1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 0, 1], [0, 0, 2], [0,0,3], [0,0,4]... ]
I have seen the docs but I can't figure out how I can do this.
Is this what you're after...
>>> l = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
>>> l.sort(key=lambda x: (-x.count(0), x[::-1]))
>>> l
[[1, 0, 0], [2, 0, 0], [3, 0, 0], [4, 0, 0], [0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 0, 4], [1, 1, 0], [2, 1, 0], [3, 1, 0], [1, 2, 0], [2, 2, 0], [1, 3, 0], [1, 0, 1], [2, 0, 1], [3, 0, 1], [0, 1, 1], [0, 2, 1], [0, 3, 1], [1, 0, 2], [2, 0, 2], [0, 1, 2], [0, 2, 2], [1, 0, 3], [0, 1, 3], [1, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2]]
See http://docs.python.org/2/howto/sorting.html for an explanation of the Python sort capability, including a discussion of "Key Functions" which gives you whatever additional flexibility you need beyond the basic sort.
Use your sorting criteria function as key in the sorting.
input_list = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0], [0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0], [0, 0, 4], [0, 1, 3], [0, 2, 2], [0, 3, 1], [0, 4, 0], [1, 0, 3], [1, 1, 2], [1, 2, 1], [1, 3, 0], [2, 0, 2], [2, 1, 1], [2, 2, 0], [3, 0, 1], [3, 1, 0], [4, 0, 0]]
sorted_list = sorted(input_list,key=my_sorting_func)

Categories

Resources