如何将随机的 0 和 1 转换为列表?

我正在学习一门课程,其中我制作了一个打印'*'1 和' '0 的程序。


我想把图像变成0和1。所以我从网站上获取了它,以便我可以用自己的代码打印它。我编写了另一个代码,用于将该未格式化的列表转换为格式化列表,该网站给出了以下结果,我想将其转换为列表。


1000111111111110000111111111111111111111

1001111111111100000111111111111111111111

0001111000000000000011111111111111111111

0001110000000000000011111111111111111111

1000110000000000000011111111111111111111

1110000000110000000011111111111111111111

0000000000111000000011111111111111111111

0000100001110000000011111111111111111111

0001111111100000000011111111111111111111

0000011000000000000011111111111111111111

0000000110000000000011111111111111111111

1100000111000000000011111111111111111111

1110000000000000000011111111111111111111

1110000000000000000111111111111111111111

1111000000000000000111111111111111111111

1111000000000000000111111111111111111111

1111100000000000000111111111111111111111

1110100000000000001111111111111111111111

1110100000000000001111111111111111111111

1000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000000000000000000011111111111111111111

0000011000000000000011111111111111111111

0000011100000000000011111111111111111111

0010011100000100000011111111111111111111

0100111100000000000011111111111111111111

0001111000000000000111111111111111111111

0011111000000000000111111111111111111111

0010110000011100000011111111111111111111

0000010100000000000111111111111111111111

0000000000000000001111111111111111111111

0000000000000000001111111111111111111111


i = 0

pyt_img = ''

for num in img:

    if i == 0:

        pyt_img += f'[{num}'

        i = i+1

    elif i >= 1 & i < 39:

        pyt_img += f',{num}'

        i = i + 1

    elif i >= 40:

        pyt_img += f'{num}]\n'

        i = 0



print(pyt_img)

问题是它永远不会到达,i = 40所以i = 40线路永远无法工作。


慕少森
浏览 154回答 4
4回答

蝴蝶不菲

给定您的img字符串,您可以使用str.join嵌套列表理解来实现此目的>>> print('\n'.join(''.join('*' if i == '1' else ' ' for i in line) for line in img.split('\n')))*&nbsp; &nbsp;***********&nbsp; &nbsp; **********************&nbsp; ***********&nbsp; &nbsp; &nbsp;*********************&nbsp; &nbsp;****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp;***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *********************&nbsp; &nbsp;**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ***********************&nbsp; &nbsp; &nbsp; &nbsp;**&nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ***&nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp; *&nbsp; &nbsp; ***&nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp;********&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp; &nbsp;**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp; &nbsp; &nbsp;**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**********************&nbsp; &nbsp; &nbsp;***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ***********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;***********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *************************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*************************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;**************************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ************************ *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;************************* *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;***********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp;**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;********************&nbsp; &nbsp; &nbsp;***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; *&nbsp; ***&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp; ********************&nbsp;*&nbsp; ****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp;****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *********************&nbsp; *****&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *********************&nbsp; * **&nbsp; &nbsp; &nbsp;***&nbsp; &nbsp; &nbsp; ********************&nbsp; &nbsp; &nbsp;* *&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **********************&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **********************

慕婉清6462132

&您使用的按位运算符的优先级elif i >= 1 & i < 39:高于比较运算符。因此,由于优先级,条件被评估为elif i >= (1 & i) < 39:比较链使其等价于elif (i >= (1 & i))  and  ((1 & i) < 39):1 & i是 和 的按位1与i,它将等于 的最后一位i,因此如果i是偶数则为0,如果i为奇数则为 1。i> (1 & i)永远都是如此True,也是如此(1 & i) < 39。你的整体状况始终是True。

侃侃尔雅

您正在执行按位与&运算而不是逻辑and。Python 利用and关键字来执行您所建议的操作。此外,您i == 39在if声明中排除了大小写,因此i永远不会变成 40。修改您的if声明以包含其中,因此if i >= 1 and i <= 39。

桃花长相依

elif i >= 1 & i < 39:&nbsp; &nbsp; &nbsp; &nbsp; pyt_img += f',{num}'&nbsp; &nbsp; &nbsp; &nbsp; i = i + 1i仅当大于或等于 1 且i小于 39 时,才会进入该代码分支。这意味着如果i 是 39,则不会进入,因为 39 不小于 39。因此,它不会加 1,i并且i永远不会达到 40。如果你使用elif i >= 1 and i <=39:它应该可以正常工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python