这段代码对吗
时间: 2024-02-15 14:03:42 浏览: 33
代码有一些小问题,需要做一些修改才能正确执行。具体来说,需要将这段代码放在一个函数中,并且在函数开头声明f、lst、lst3、lst4这些变量。在函数结束后,应该关闭文件。此外,在调用jishu函数时,应该将lst作为参数传入。最后,在输出lst4时,应该使用循环将每个分组的坐标列表打印出来。修改后的代码如下:
```
def count_ones():
f = open("C:\\Users\\50382\\Desktop\\python\\实验二文档.txt", encoding='utf-8')
lst = f.readlines()
f.close()
def jishu(list):
lst0 = []
for x in range(0, len(list)):
for y in range(0, len(lst[x])):
if list[x][y] == "1":
lst0.append([x, y])
return lst0
def hebing(list):
lstzuihou = []
for x in list:
while True:
lst2 = [x]
for y in lst2:
c = 0
hang = y[0]
lie = y[1]
if [hang, lie+1] in list:
if [hang, lie+1] not in lst2:
lst2.append([hang, lie+1])
c += 1
if [hang, lie-1] in list:
if [hang, lie-1] not in lst2:
lst2.append([hang, lie-1])
c += 1
if [hang+1, lie] in list:
if [hang+1, lie] not in lst2:
lst2.append([hang+1, lie])
c += 1
if [hang-1, lie] in list:
if [hang-1, lie] not in lst2:
lst2.append([hang-1, lie])
c += 1
if c == 0:
lstzuihou.append(lst2)
break
if len(lst2) == 1:
break
return lstzuihou
lst3 = jishu(lst)
lst4 = hebing(lst3)
for group in lst4:
print(group)
```
你可以将这段代码复制到Python的REPL或者一个.py文件中运行,看看输出是否正确。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)