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 . 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. Consider using a list
or tuple
if you would like to maintain order.
A set
in Python is an unordered data structure, so so it does not preserve the insertion order.
You should use a list
instead. Or you can sort
the set. But sort
return a list anyway.
Using 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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.