我很好奇什么是独特的这种数据对象的有效方式:
testdata =[ ['9034968', 'ETH'], ['14160113', 'ETH'], ['9034968', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15724032', 'ETH'], ['15481740', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['10307528', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['15481740', 'ETH'], ['15379365', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15379365', 'ETH']
]
Run Code Online (Sandbox Code Playgroud)
对于每一个数据对,左侧的数字串PLUS在合适的类型告知的数据元素的唯一性.它返回与testdata相同的列表列表,但只存在唯一身份证.
问候
这是我的字典
propertyList = {
"id": "int",
"name": "char(40)",
"team": "int",
"realOwner": "int",
"x": "int",
"y": "int",
"description": "char(255)",
"port": "bool",
"secret": "bool",
"dead": "bool",
"nomadic": "bool",
"population": "int",
"slaves": "int",
}
Run Code Online (Sandbox Code Playgroud)
但是当我用"\n".join(myDict)打印出来时,我得到了这个
name
nomadic
dead
port
realOwner
secret
slaves
team
y
x
population
id
description
Run Code Online (Sandbox Code Playgroud)
我知道字典是无序的,但每次出现都是一样的,我不知道为什么.
由于dictPython 3.6 中的实现发生了变化,现在默认排序.现在还set保留秩序吗?
我找不到任何关于它的信息,但由于这两种数据结构在它们工作的方式非常相似,我认为可能就是这种情况.
我知道dict在所有情况下都无法订购s,但它们大多数情况下都是如此.如Python文档中所述:
这个新实现的顺序保留方面被认为是一个实现细节,不应该依赖它
可能重复:
为什么python会这样排序我的字典?
我对以下输出有点困惑.我不明白执行循环的顺序.
domains = { "de": "Germany", "sk": "Slovakia", "hu": "Hungary",
"us": "United States", "no": "Norway" }
for key in domains:
print key
Run Code Online (Sandbox Code Playgroud)
这里的输出是
sk
de
no
us
hu
Run Code Online (Sandbox Code Playgroud)
但不是
de
sk
hu
us
no
Run Code Online (Sandbox Code Playgroud)
同样,这里
num = {1:"one",4:"two",23:"three",10:"four"}
for key in num:
print key
output is
1
10
4
23
Run Code Online (Sandbox Code Playgroud)
但不是
1
4
23
10
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
所以我注意到我们在 python 中说集合没有顺序或排列,尽管您当然可以对集合生成的列表进行排序。
所以我想知道如何在 python 中定义集合的迭代。它是否只是遵循排序的列表顺序,或者是否有其他一些可能在某个时候突然出现的枪?
谢谢。
def parse_urls(weeks_urls):
for wkey in weeks_urls.keys():
results=urllib2.urlopen(weeks_urls[wkey])
lines = list(csv.reader(results))
lines=clean_lines(lines)
week_dict=dict.fromkeys(lines[i][1] for i in range(len(lines)))
fare_data=list((lines[i][1:]) for i in range(3,len(lines)))
fare_data=get_fare_data(fare_data)
n=3
for station in week_dict: .....
.......
Run Code Online (Sandbox Code Playgroud)
当我用来dict.fromkeys( )从字符串列表中生成一个字典时,它会自动对它们进行排序,从而按字母顺序生成一个带字符串的字典.我需要保留字符串的原始顺序.有没有办法做到这一点?
感谢这里的任何帮助
谢谢!
多次运行此代码
t = {'a', 'b', 'c', 'd'}
print(t)
Run Code Online (Sandbox Code Playgroud)
可以打印像:
{'c', 'b', 'a', 'd'}
{'d', 'b', 'c', 'a'} # different
{'d', 'b', 'c', 'a'} # same
{'a', 'd', 'b', 'c'} # different
{'a', 'b', 'c', 'd'} # different
# etc
Run Code Online (Sandbox Code Playgroud)
(如果您使用控制台进行复制,请确保在Rerun每次重新粘贴代码并执行之前单击.如果仍然无法复制,可能您的散列随机化不等于random.在Python 3.3及更高版本上,默认情况下启用哈希随机化.)
另一方面,下面的代码总是打印相同的集合,它实际上是排序的:
s = {1, 6, 3.3, 4}
print(s)
# prints:
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
# {1, 3.3, 4, 6}
Run Code Online (Sandbox Code Playgroud)
问题: …
让我们运行以下代码:
st = {3, 1, 2}
st
>>> {1, 2, 3}
st.pop()
>>> 1
st.pop()
>>> 2
st.pop()
>>> 3
Run Code Online (Sandbox Code Playgroud)
尽管集合被认为是无序的,但该集合的行为就像按升序排序一样。pop()根据文档,该方法应该返回“任意元素”,也按升序返回元素。这是什么原因呢?
我有:
tuple1 = token1, token2
tuple2 = token2, token1
for tuple in [tuple1, tuple2]:
if tuple in dict:
dict[tuple] += 1
else:
dict[tuple] = 1
Run Code Online (Sandbox Code Playgroud)
但是,元组1和元组2都得到相同的计数.什么是一种方法来散列一组2件事,这样的秩序很重要?
似乎有一些一致性,因为调用set()字符串似乎总是解析为相同的(非alabetical)顺序,并且两者都是
set([1,2,3]) & set([1,2,3,4])
Run Code Online (Sandbox Code Playgroud)
和它的表兄弟混在一起
set([2,3,1]) & set([4,3,1,2])
Run Code Online (Sandbox Code Playgroud)
将导致有序的set([1,2,3]).
另一方面,有点像racy,比如
from random import randint
set([randint(0,9) for x in range(3)])
Run Code Online (Sandbox Code Playgroud)
有时会给set([9, 6, 7])...
... 这里发生了什么?
python ×10
set ×4
dictionary ×3
cpython ×2
for-loop ×1
python-3.4 ×1
python-3.6 ×1
python-3.9 ×1
python-3.x ×1
unique ×1
unordered ×1