【发布时间】:2021-04-23 02:39:11
【问题描述】:
我有这段代码应该反转.ppm 图像的像素。
我能够反转像素,但我一直在删除列表和元组括号列表,以便仅获取整数。
我想要:
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
我得到了什么:
('15', '0', '15') ('0', '0', '0') ('0', '0', '0') ('0', '0', '0')
('0', '0', '0') ('0', '0', '0') ('0', '15', '7') ('0', '0', '0')
('0', '0', '0') ('0', '15', '7') ('0', '0', '0') ('0', '0', '0')
('0', '0', '0') ('0', '0', '0') ('0', '0', '0') ('15', '0', '15')
代码:
takePPM = input("Enter the pmm file name: ")
try:
openFile = open(takePPM, "r")
readFile = openFile.readlines()
except FileNotFoundError:
print("File not found")
exit(1)
lists = []
for row in readFile[3:]:
lists.append(row.split())
#convert each list in lists to lists of 3-tuples
target = []
for list in lists:
target.append([tuple(list[i:i+3]) for i in range(0,len(list),3)])
revList = []
for i in reversed(target):
revList.append(i)
#newFile = open("Mirror_" + takePPM, "w")
for row in revList:
#newFile.write(*row)
removeList = tuple(row)
print(*removeList)
注释行是将新的反转像素打印到新文件中,但想先检查一下我得到了正确的格式
【问题讨论】:
-
''.join(list[i:i+3])可能在列表理解中 -
是的,这行得通,非常感谢你????
标签: python python-3.x list file tuples