简体   繁体   English

每次运行相同的代码后得到的输出略有不同

[英]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.如果您想保持顺序,请考虑使用listtuple

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.

相关问题 每次为我自己的集束搜索实现获得不同的 output。 但是当我在代码中添加延迟时就解决了。 为什么? - Getting different output every time for my own implementation of beam search. But gets solved when I add delay in the code. Why? 为DataFrame和相同代码的正常实现获取不同的输出 - Getting different output for DataFrame & normal implementation of same code Python3多次运行相同的function,输入相同但每次产生不同的输出 - Python3 running the same function with the same input many times but producing different outputs every time 运行此代码后,它为两个骰子吐出相同的值,我怎样才能将随机值固定为每次都不同? - After running this code it spits out the same value for both dice, how can I fix the random value to be different each time? 在相同的 pandas 数据上运行相同的脚本会产生非常不同的数据帧浮点值 - Running the same script on the same pandas data produces very slightly different dataframes floating-point values 以略有不同的功能对代码进行重复数据删除 - Deduplicating code in slightly different functions Web 报废每次都会给出不同的 output - Web scrapping gives different output every time 我在不同的设备上运行相同的代码,但它们没有相同的输出 - I'm running the same code on different device but they don't have the same output 为什么在不同的 python 版本上运行相同的代码会得到不同的输出? - Why do I get different output running the same code on different python versions? 通过运行一次语句来获取结果并解释同时分析来自Postgresql的输出 - Getting results and explain analyze output at the same time from Postgresql by running the statement once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM