공백으로 구분 된 파일이 있습니다.
1 1 2
1 2 3
2 2 3
1 1 3
각 줄을 목록에 넣어 목록 목록을 만들고 싶습니다. 파일의 첫 번째 열을 생략하고 정수 연산을 수행 할 수 있도록 유형을 Integer로 변환하고 싶습니다. 따라서 about 예제의 목록은 [[1, 2], [2, 3], [2, 3], [1, 3]]처럼 보일 것입니다. 내가 사용한 코드는 다음과 같습니다.
class Graph:
def __init__(self):
f = open("Ai.txt")
next(f)
self.coordinates = []
count = 0
for line in f:
if count == 274:
break
else:
self.coordinates.append([ int(i) for i in line.split()[1:] ])
count += 1
def getLocation( self, vertex ):
return self.coordinates[vertex]
g = Graph()
x = g.getLocation(44)
print x
zip(*zip(*csv.reader(open("my_file.txt"),delimiter=" "))[1:])
int가 필요한 경우 일부 맵에서 래핑 할 수 있습니다.
map(lambda x:map(int,x),zip(*zip(*csv.reader(open("my_file.txt"),delimiter=" "))[1:]))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다