How to quicksort a nested list. [duplicate] - python

This question already has answers here:
Sorting a nested list by a certain element without the sorting function.
(3 answers)
Closed 5 years ago.
I'm not sure how to implement the quicksort algorithm into a nested list. If I have a list like this:
L = [['James', '1', '2'], ['Alan', '1', '1'], ['Henry', '1', '5']]
and I want to order it based on the last number in each sublist.
Output:
final = [['Henry', '1', '5'], ['James', '1', '2'], ['Alan', '1', '1']]

I find an algorithm implementation from this question Quicksort with Python. And just redefine the comparison funciton:
L = [['James', '1', '2'], ['Alan', '1', '1'], ['Henry', '1', '5']]
less_than = lambda x, y: x[2] < y[2]
more_than = lambda x, y: x[2] >= y[2]
def qsort(arr):
if len(arr) <= 1:
return arr
else:
return qsort([x for x in arr[1:] if less_than(x, arr[0])]) + [arr[0]] + qsort([x for x in arr[1:] if more_than(x, arr[0])])
print(qsort(L))
Output:
[['Alan', '1', '1'], ['James', '1', '2'], ['Henry', '1', '5']]
Hope this helps.

Related

How to remove letters and space from line in python?

Im new in python and I have problem like this:
I have a list
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
and I wat to get
max(Index)
I have something like this sofar
import numbers
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
Index1 = [x for x in Index if x] #to remove empty space
Index2 = [x for x in Index1 if isinstance(x, numbers.Number)] #to remove all letters
Index3 = map(int, Index2)
print (max(Index3))
But output from Index1 and Index2 is always [].
Perhaps you meant to use the isdigit() method of strings:
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
Index1 = [x for x in Index if x] #to remove empty space
Index2 = [x for x in Index1 if x.isdigit()] #to remove all letters
Index3 = map(int, Index2)
print (max(Index3))
Output:
29
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
my_index = [int(elem) for elem in Index if elem.isnumeric()]
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
print(max(map(int,filter(str.isdigit,Index))))
Filter out anything not a digit,
filter(str.isdigit,Index)
then map all to integers,
map(int,)
Then get the max.
max()
Index = ['TH', '1', '1', '2', '28', '29', '2', '']
result = [i for i in Index if not(not i or i.isalpha())]
print(result)
Output:- ['1', '1', '2', '28', '29', '2']

List Comprehension - Conditional [duplicate]

This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 22 days ago.
For these 2 lists:
A=['3.40', '4.00', '151.00', '8.00', '81.00', '23.00', '17.00', '8.50', '5.00', '151.00', 'SCR', 'SCR', '13.00']
B=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '12', '10', '3']
The desired output is:
C=['11', '5', '2', '4', '6', '9', '7', '8', '10', '1', '3']
So - list 'A' and list 'B' are the same length. List 'C' is the same as list 'B' - but does not have the values where 'SCR' exists in list 'A'.
My attempt at this is:
C = [x for x in B if x in A!='SCR']
Thankyou
just zip them together:
C = [b for a,b in zip(A,B) if a != 'SCR']
Based on what I think you're trying to accomplish, I think you would need this:
C = [B[x] for x in range(len(B)) if A[x] != 'SCR']
This is straightforward using the built-in enumerate function:
[x for (idx, x) in enumerate(B) if A[idx] == 'SCR']

how can i replace an element in list of lists in python

i have a list of list like below
bo = [['7', '8', '9'], ['4', '5', '6'], ['1', '2', '3']]
i wanted to a code that change an elemnt if it is found.for example if 9 is in the nested list i want 9 to be changed to letter "x" and the board will be like
bo=[['7', '8', 'x'], ['4', '5', '6'], ['1', '2', '3']]
please know that i am in introductory level.don't know about lambda or numpy.
Use list comprehension
def replace_num(lst, find_num, new_num):
return [[new_num if x == find_num else x for x in sublist] for sublist in lst]
lst = [[1,2,3],[4,5,6]]
print(replace_num(lst, 5, 25))
# Output: [[1, 2, 3], [4, 25, 6]]
import numpy as np
b=[[1,2,3],[4,5,6]]
a = np.array(b)
b = np.where(a==3, "xx", a)
print(b)
>>[['1' '2' 'xx'] ['4' '5' '6']]
Probably slower than the other solution but you can just write a loop:
b=[[1,2,3,4,5],[3,4,5,6,7]]
test=4
new=2
for i in range(len(b)):
for j in range(len(b[i])):
if b[i][j] ==test:
b[i][j]=new
Or if you use numpy:
import numpy as np
a=np.array([[1,2,3,4,5],[1,2,3,46,7]])
a[a==4]=2

Python 3.6 - reading the the values of rows from two lists after a conditional statement

Hi here is my code that looks for values that repeat 3 or more times in list a:
a = ['1','1','1','2','2','3']
b = ['4','5','1','7','8','4']
d = [item for item in a if a.count(item) >= 3]
print(d)
# ['1', '1', '1']
So my question is how can I also read the corresponding values in list b. Also list a and b are always the same size. My desired output should be:
output: [['1', '1', '1'], ['4', '5', '1']]
thank you!
You can solve this using zip:
>>> list(zip(*[(ai, bi) for ai, bi in zip(a, b) if a.count(ai) >= 3]))
[('1', '1', '1'), ('4', '5', '1')]
Just use enumerate:
d = [(item,c[1][i]) for i,item in enumerate(c[0]) if c[0].count(item) >= 3]
Then you can zip them together
d = zip(*d)
You can use itertools.compress() like below:
import itertools
a = ['1', '1', '6', '2', '1', '3']
b = ['4', '5', '1', '7', '8', '4']
a_items = [item for item in a if a.count(item) >= 3]
b_items = list(itertools.compress(b, (i in set(a_items) for i in a)))
res = [a_items, b_items]
Output:
>>> res
[['1', '1', '1'], ['4', '5', '8']]

I am trying to find the set of lists in a tuple

for example,
(['2', '3', '5'], ['1', '3', '4', '5'])
the above should yield the numbers 3 and 5
(['1', '2', '4'], ['1', '2'])
this should give out 1 , 2
(['2', '3', '5'], ['1', '2', '4'], ['2', '3'])
this, should give out 2, because 2 is contained in all 3 lists in the tuple. If there is no set it should just return an empty list
for i,e in enumerate(tup):
while index < len(tup):
print(tup[index], tup[index + 1])
index = index + 1
For now i have this, i am not sure how to go through the tup(Tuple) and extract each list to find the set of each 2 lists and iterate and compare with the rest of lists in the tuple
def intersect(lists):
return list(set.intersection(*map(set, lists)))
In [8]: t = (['2', '3', '5'], ['1', '3', '4', '5'])
In [9]: functools.reduce(lambda a,b: set.intersection(set(a), set(b)), t)
Out[9]: {'3', '5'}
Be consistent in return types. Don't return an empty string when you have an empty set.
That being said, I'd find the numbers like this:
>>> lsts = (['2', '3', '5'], ['1', '3', '4', '5'])
>>> set(lsts[0]).intersection(*lsts[1:])
set(['3', '5'])
>>>
>>> lsts = (['1', '2', '4'], ['1', '2'])
>>> set(lsts[0]).intersection(*lsts[1:])
set(['1', '2'])
>>>
>>> lsts = (['2', '3', '5'], ['1', '2', '4'], ['2', '3'])
>>> set(lsts[0]).intersection(*lsts[1:])
set(['2'])
The function for that would be:
>>> def common(lsts):
... return set() if not lsts else set(lsts[0]).intersection(*lsts[1:])
Simple as that: python has a set type, so just
sets = [set(l) for l in (['2', '3', '5'], ['1', '3', '4', '5']) ]
common_elements = reduce(lambda a,b: a & b, sets)

Categories

Resources