[英]Getting slightly different output every time after running the same code
Here in this code I am getting output differently every time I run the same code.在这段代码中,每次运行相同的代码时,我都会得到不同的输出。
INPUT:输入:
s='AABCAAADA'
st=[]
def merge_the_tools(size,k):
n=int(len(size)/k)
for i in range(n):
st.append(size[i*n:(i+1)*n])
for i in st:
se=set(i)
print(''.join(se))
print(merge_the_tools(s,3))
First OUTPUT:第一个输出:
AB
AC
AD
None
Another OUTPUT:另一个输出:
AB
CA
DA
None
Another OUTPUT:另一个输出:
BA
CA
DA
None
Like this I am getting different output Can anybody tell why this is happening.像这样我得到不同的输出 谁能告诉为什么会这样。
And I want this OUTPUT:我想要这个输出:
AB
CA
AD
Sets in Python are unordered and unindexed as shown in this demonstration . Python 中的集合是无序和无索引的,如本演示所示。 If you want to maintain a specific order, you can just sort the set as follows.
如果你想保持一个特定的顺序,你可以按如下方式对集合进行排序。
print(''.join(sorted(se)))
Python set
s have no order, so there is no guarantee that the items will be retrieved in the same order every time while join
ing them. Python
set
没有顺序,因此不能保证每次join
都会以相同的顺序检索项目。 Consider using a list
or tuple
if you would like to maintain order.如果您想保持顺序,请考虑使用
list
或tuple
。
A set
in Python is an unordered data structure, so so it does not preserve the insertion order. Python 中的
set
是无序的数据结构,因此它不保留插入顺序。
You should use a list
instead.您应该改用
list
。 Or you can sort
the set.或者您可以
sort
集合进行sort
。 But sort
return a list anyway.但是
sort
无论如何都会返回一个列表。
Using sorted
:使用
sorted
:
s='AABCAAADA'
st=[]
def merge_the_tools(size,k):
n=int(len(size)/k)
for i in range(n):
st.append(size[i*n:(i+1)*n])
for i in st:
se=set(i)
print(''.join(sorted(se)))
print(merge_the_tools(s,3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.