Python list rearrange the elements in a required order

2024/11/17 10:28:48

My main list has 44 names as elements. I want to rearrange them in a specific order. I am giving here an example. Note that elements in my actual list are some technical names. No way related to what I have given here.

main_list = ['one','two','five','six',.................'twentyone','three','four','seven','eight',.....,'fortyfour']

I want to rearrange the list. I have no idea how to proceed. But my expected output should like this Expected output:

main_list = ['one','two','three','four','five','six','seven','eight'.................'twentyone',,.....,'fortyfour']
Answer

You can have a dictionary to use as your sorting key:

sort_keys = {'one': 1,'two': 2,'three': 3,'ten': 10,'twenty': 20,
}
main_list = ['twenty', 'one', 'three', 'ten', 'two']
main_list.sort(key=lambda i: sort_keys[i])
print(main_list)

Output:

['one', 'two', 'three', 'ten', 'twenty']
https://en.xdnf.cn/q/120199.html

Related Q&A

GMB API accounts.locations.reviews.list

Can i get source code to get reviews using gmb python code also heard mybusiness is discontinued. I tried using my business api is discontinued can i get the implementation process to look for extract…

How to get words from an online text archive such as pastebin?

Im trying to get the words(users) from a text file hosted online, such as from www.site.com/mytextfile.txt or pastebin.com/raw/1111111. I will have multiple "users", one in each line. My code…

Python Histogram using matplotlib on top words

I am reading a file and calculating the frequency of the top 100 words. I am able to find that and create the following list:[(test, 510), (Hey, 362), ("please", 753), (take, 446), (herbert, …

how can I add field in serializer?

Below is my serializer.py file: from rest_framework import serializersclass TaskListSerializer(serializers.Serializer):id = serializers.CharField()user_id = serializers.CharField()status = serializers.…

Read and write a variable in module A from module B

Need a solution or workaround to read and write the variable in moduleA from moduleB please. Here is the code:moduleAimport moduleBvariable = 10def changeVariable():global variablevariable = 20def main…

Iterate through a pandas dataframe to get a specific output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 1 year ago.The comm…

Detect if you click inside a box in Python Zelle graphics

I have created a dice betting game. When my code is run, there is a "CLICK TO ROLL" button. Currently, if you click anywhere on the screen, the dice will roll. How can I make it so the progra…

Nested Triangle in Python

My assingmentAt each level the complete triangle for the previous level is placed into an extra outer triangle. The user should be asked to input the two characters to be used and the width of the inne…

How to fix cannot open file Test.py: [Errno2] No such file or directory?

I tried to run a python script which does not exist in current folder, for exampleC:\>python Test.pypython:cant open file Test.py:[Errno2] No such file or directoryI have to specify the absolute pat…

Highlight cells in a column in google spreadsheet when the value above a threshold with python

Here is a simplified example of my codes and the screenshot of the results I want to get in google spreadsheet. I hope to either save the dataframe style to google spreadsheet as applying table style t…