相关疑难解决方法(0)

将列表转换为集更改元素顺序

最近我注意到,当我转换listset元素的顺序发生变化,由字符排序.

考虑这个例子:

x=[1,2,20,6,210]
print x 
# [1, 2, 20, 6, 210] # the order is same as initial order

set(x)
# set([1, 2, 20, 210, 6]) # in the set(x) output order is sorted
Run Code Online (Sandbox Code Playgroud)

我的问题是 -

  1. 为什么会这样?
  2. 如何在不丢失初始订单的情况下进行设置操作(尤其是设置差异)?

python set

94
推荐指数
7
解决办法
10万
查看次数

标签 统计

python ×1

set ×1