如何在python中反转生成的整数顺序(How to reverse generated integer order in python)

此代码生成一个数字,因为它将十进制转换为二进制。

dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 print (int(rem), end = " ") dec = int(dec/2)

输出如下。

1 0 0 1 0

有没有办法扭转输出顺序?

例如。 0 1 0 0 1

如果生成的单个数字连接到一个字符串并且字符串被反转,它可能会起作用,但我不知道如何去做。

This code produces one number after the other as it converts decimal to binary.

dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 print (int(rem), end = " ") dec = int(dec/2)

The output is the following.

1 0 0 1 0

Is there a way to reverse the output order?

Eg. 0 1 0 0 1

It may work if the individual numbers generated are joined to a string and the string is reversed, but I don't know how to go about doing that.

最满意答案

我认为以下代码将起作用。

result = [] dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 result.append(rem) dec = int(dec/2) result = result[::-1] print(' '.join(str(r) for r in result))

I think the following code will work.

result = [] dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 result.append(rem) dec = int(dec/2) result = result[::-1] print(' '.join(str(r) for r in result))如何在python中反转生成的整数顺序(How to reverse generated integer order in python)

此代码生成一个数字,因为它将十进制转换为二进制。

dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 print (int(rem), end = " ") dec = int(dec/2)

输出如下。

1 0 0 1 0

有没有办法扭转输出顺序?

例如。 0 1 0 0 1

如果生成的单个数字连接到一个字符串并且字符串被反转,它可能会起作用,但我不知道如何去做。

This code produces one number after the other as it converts decimal to binary.

dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 print (int(rem), end = " ") dec = int(dec/2)

The output is the following.

1 0 0 1 0

Is there a way to reverse the output order?

Eg. 0 1 0 0 1

It may work if the individual numbers generated are joined to a string and the string is reversed, but I don't know how to go about doing that.

最满意答案

我认为以下代码将起作用。

result = [] dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 result.append(rem) dec = int(dec/2) result = result[::-1] print(' '.join(str(r) for r in result))

I think the following code will work.

result = [] dec = int(input("Please enter number to convert to decimal: ")) while dec>0: quoteint = dec/2 rem = dec%2 result.append(rem) dec = int(dec/2) result = result[::-1] print(' '.join(str(r) for r in result))