Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Sorting a list using a named function

ex_lst = ['hi', 'how are you', 'bye', 'apple', 'zebra', 'dance']

def second_let(x):

    lst= []
    for wrd in x:
        lst.append(wrd[1])
    return lst

print(second_let(ex_lst))
print(sorted(second_let(ex_lst)))

sorted_by_second_let= sorted(ex_lst, key= second_let)
Comment

python sort a list using defined order

>>> A = [[3,5],[1,3],[6,1]]
>>> B = [6,1,3]
>>> srt = {b: i for i, b in enumerate(B)}
>>> sorted(A, key=lambda x: srt[x[0]])
[[6, 1], [1, 3], [3, 5]]
Comment

python sort a list using defined order

>>> sorted(A, key = lambda i: B.index(i[0]))
[[6, 1], [1, 3], [3, 5]]
Comment

PREVIOUS NEXT
Code Example
Python :: input and print 
Python :: RMSE value from cross validation 
Python :: aiohttp set port 
Python :: from Player import Player 
Python :: webex teams api attach file 
Python :: unique list 
Python :: typer python 
Python :: how to hello world in python 
Python :: how to allow a range of numbers for example 1 to 14 on regular expression python 
Python :: numpy subtract 
Python :: scrapy access settings from spider 
Python :: class variable in python 
Python :: how to access a txt file through os library in python 
Python :: python grab results from cursor.execute 
Python :: NumPy bitwise_xor Syntax 
Python :: django models filter 
Python :: bad request 400 heroku app 
Python :: convert png rgba to rgb pyhton 
Python :: import folder from another folder python 
Python :: lambda example python 
Python :: midpoint circle drawing algorithm 
Python :: pandas convert hex string to int 
Python :: binary search tree python 
Python :: Align axis labels in subplots 
Python :: read csv in spark 
Python :: compare dates in python 
Python :: dataFrame changed by function 
Python :: onehotencoder = OneHotEncoder(categorical_features = [1]) X = onehotencoder.fit_transform(X).toarray() X = X[:, 1:] 
Python :: python open file location 
Python :: pandas include nan in value_counts 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =