[英]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.