简体   繁体   English

如何对这个列表进行排序以使指数值 go 在前?

[英]How to sort this list to make the exponent value go first?

Given list = [10, '3 ^ 2', '2 ^ 3'] , how to sort the list to make the exponents values ( '3 ^ 2' and 2 ^ 3 ) are before any integer / float value and exponent values are sorted from the base.给定list = [10, '3 ^ 2', '2 ^ 3'] ,如何对列表进行排序以使指数值( '3 ^ 2'2 ^ 3 )在任何integer / float值和指数值之前从基地排序。

Desired Output: ['2 ^ 3', '3 ^ 2', 10] Desired Output: ['2 ^ 3', '3 ^ 2', 10]

I have tried to remove() and insert() the value but I can't figure out how to find the index to insert the value in.我试过remove()insert()这个值,但我不知道如何找到索引来插入这个值。

You shouldn't use list as a variable, it is reserved in python.您不应该将列表用作变量,它在 python 中保留。

Not pretty, but you can use this不漂亮,但你可以用这个

sorted(mylist, key=lambda x: (-len(str(x).split('^')), str(x).split('^')[0]))

This is sorting according to two criteria, first if there is an exponent, and then by the value of the base.这是根据两个标准进行排序,首先是有指数,然后是底数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按每个列表中的第一个值将列表作为字典中的值排序? - How to sort a list as a value in a dictionary by the first value in each list? 如何按内部列表中的第一个值对列表列表进行排序 - How to sort list of lists by first value in inner list 当值是一个列表时,如何按值对python中的字典进行排序,我想按该列表的第一个索引对其进行排序 - How to sort a dictionary in python by value when the value is a list and I want to sort it by the first index of that list 如何对列表进行排序,首先按项目的频率,然后按项目的值 - How to sort list, first by frequency of items, then by the value of the item 如何根据Python中值的首字母对列表值进行排序 - How to sort the list values according to the first letter of the value in Python 当输入中给出一个键来决定排序是升序还是降序时,如何按第一个值对元组列表进行排序? - How to sort a list of tuples by the first value when a key is given in the input to decide whether the sort is ascending or descending? 先按类型排序卡片列表,然后按值排序? - sort card list by type first then by value? 按第一个值对带有元组的列表进行排序 - Sort list of lists with tuples by first value 如何使用第一个值对列表列表进行排序,如果两者相等,则检查第二个,依此类推 - How to sort a list of list with the first value, if the both are equal check the second and so on 如何制作替代指数功能的功能? - How to make a function that replaces the exponent feature?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM