2012-10-03 3 views
1

다음 파일에 데이터를 쓰려고 시도하지만 절반 정도 아래쪽으로 진행하면 첫 번째 행 세트에 대해 상수가있는 한 열의 값이 변경됩니다. 내 코드는 현재 사전에이 형식으로 도움을텍스트 파일에 데이터 쓰기 중반 변경

512 #number of 
comment goes here 
H 6.000000 19.000000 14.000000 
H 11.000000 2.000000 7.000000 
H 15.000000 20.000000 16.000000 

감사를 XYZ 파일을 생성

import random 
import time 

start_time = time.time() #time measurement 
numpoints = 512 
L = 20 
d = 1 
points = set() 

# Open f and write 
with open("question2.xyz","w") as f: 
    f.write("%d\ncomment goes here\n" % numpoints) #this is for the 2nd line in my xyz 
    while len(points) < numpoints: 
     p = (random.randint(0, L), random.randint(0, L), random.randint(0, L)) 
     if p not in points: 
      points.add(p) 
      f.write('H %f %f %f\n' % p) 

: 이것은 내 코드입니다!

편집, 죄송합니다 woops,이게 내가 달성하고자하는 것입니다

512 #number of 
comment goes here 
H 6.000000 19.000000 14.000000 
H 11.000000 2.000000 7.000000 
H 15.000000 20.000000 16.000000 
O 6.000000 19.000000 14.000000 
O 11.000000 2.000000 7.000000 
O 15.000000 20.000000 16.000000 
지금 내 코드 라인 (256)에서 시작, 모든 512 개 라인의 첫 번째 값에 대한 H에두고

가, 내가로 변경하는 것이 필요 O

+0

당신은 또한 당신이 무엇을 얻으려고하는지의 예를 게시 할 수 있을까요? –

+0

예제 입력 파일과 예상 출력을 게시 할 수 있습니까? –

+0

입력 파일이 없으며 파일이 생성되고 위에 표시된 출력 파일이 표시됩니다 –

답변

2

당신은 포인트 생성을위한 발전기를 사용하고, 두 for 루프 수 :

def pointgen(used): 
    while True: 
     p = (random.randint(0, L), random.randint(0, L), random.randint(0, L)) 
     if p not in used: 
      used.add(p) 
      yield p 

# Open f and write 
with open("question2.xyz","w") as f: 
    f.write("%d\ncomment goes here\n" % numpoints) #this is for the 2nd line in my xyz 
    pg = pointgen(points) 
    for i in xrange(numpoints // 2): 
     f.write('H %f %f %f\n' % pg.next()) 
    for i in xrange(numpoints // 2): 
     f.write('O %f %f %f\n' % pg.next())