2017-05-07 58 views
-4

我得到一個索引超出範圍錯誤,當我運行此代碼。它適用於tmp> 3並且tmp [2] == tmp [3]。即時通訊嘗試寫入文件索引[0]中的條件滿足時的數字。又名tmp [0] == tmp [1]。無論我嘗試過什麼,爲什麼我不能調用tmp [0]。Python。試圖獲得索引[0] ==索引[1]

i = 0 
tmp = [] 
while(i < len(sequence)): 
    tmp = sequence[i].replace("(","").replace(")","").split(",") 
    if(len(tmp) > 1): 
     if(tmp[0] == tmp[1]): 
      print tmp[0] 
      with open(output_file1, 'a') as output: 
       output.write(str(tmp[0]) + '\n') 
i = i+1 
return True 

python app.py 
Traceback (most recent call last): 
File "app.py", line 71, in <module> 
generate(6, out_path2,out_path1, out_path) 
File "app.py", line 45, in generate 
random6th = random.choice(sequence2).replace("[", "").replace("]","").replac 
e("'","").replace(" ","") 
File "C:\Python27\lib\random.py", line 275, in choice 
return seq[int(self.random() * len(seq))] # raises IndexError if seq is emp 
ty 
IndexError: list index out of range 

i = 0 
tmp = [] 
while(i < len(sequence)): 
    tmp.append(sequence[i].replace("(","").replace(")","").split(",")) 
    if(len(tmp) > 0): 
     if(tmp[0] == tmp[1]): 
      print tmp[0] 
      with open(output_file1, 'a') as output: 
       output.write(str(tmp[0]) + '\n') 
i = i+1 
return True 

Traceback (most recent call last): 
File "app.py", line 69, in <module> 
generate_2(seq_path, out_path1, out_path2); 
File "app.py", line 20, in generate_2 
if(tmp[0] == tmp[1]): 
IndexError: list index out of range 

隨着第二代碼我得到另一追蹤誤差,在如果(TMP [0] == TMP [1] 帶有打印TMP [0],從不運行的Cuz與第二代碼回溯上述錯誤的。

序列看起來像這樣

(8, 16, 5, 5, 65, 27) 
(7, 15, 4, 4, 64, 26) 
(21, 17, 12) 
(22, 22, 11, 11, 59, 24) 
(21, 21, 10, 9, 58, 23) 

我想匹配21和21 TMP [0] == TMP [1],然後寫入文件21

+0

你能分享確切的回溯? –

+0

你可以在賦值後給我們一個'print(tmp)'的輸出嗎? – AetherUnbound

+1

'sequence [i] .replace(「(」,「」)。replace(「)」,「」)。split(「,」)'看起來像個窮人的'ast.literal_eval' –

回答

0

你設定tmp = []之前你的while循環,但是然後你重新分配它,每當你在循環中。您的代碼應改爲:

i = 0 
tmp = [] 
while(i < len(sequence)): 
    tmp.append(sequence[i].replace("(","").replace(")","").split(",")) 
    if(len(tmp) > 1): 
     if(tmp[0] == tmp[1]): 
      print tmp[0] 
      with open(output_file1, 'a') as output: 
       output.write(str(tmp[0]) + '\n') 

這可能會解決您的問題。

+0

將列表附加到列表可能不是OP想要的 –

+0

由於'sequence [i]'看起來像是列表中,它會追加序列中的一個項目(在此上下文中看起來是一個字符串)。 – AetherUnbound

0

好了,它看起來像數據是字符串(給您呼叫的字符串的方法)的列表,所以用這樣的假設,這應該工作:

# sequences = \ 
# """(8, 16, 5, 5, 65, 27) 
# (7, 15, 4, 4, 64, 26) 
# (21, 17, 12) 
# (22, 22, 11, 11, 59, 24) 
# (21, 21, 10, 9, 58, 23)""" 
# sequences = sequences.split('\n') 

# Alternatively, if the data is within a textfile called sequence.txt 
with open(os.path.join(home_dir, 'sequence.txt'), 'r') as infile: 
    sequences = infile.read().splitlines(keepends=False) 

output_file1 = 'good_sequences.txt' 

# assuming you were using mode='a' only because open was called each iteration, otherwise change to 'a' 
with open(output_file1, 'w') as outfile: 
    for sequence in sequences: 
     seq_data = sequence.replace('(', '').replace(')', '').replace(' ', '').split(',') 
     if seq_data and len(seq_data) > 2: 
      if seq_data[0] == seq_data[1]: 
       print(seq_data[0]) 
       # didn't see a need to cast as str 
       outfile.write(seq_data[0] + '\n') 

輸出將是:

22 
21 
+0

該代碼的工作與它,但是,序列= os.path.join(home_dir,'sequence.txt')即時使用,它不會打印seq_data [0] – nerdboy

+0

檢出編輯版本。這是否更好? – TSeymour

+0

最後一個不輸出任何東西到文件或打印seq_data [0],沒有打印沒有輸出tho – nerdboy

0

我解決這個問題的最簡單的方法是改變int(tmp [0])== int(tmp [1])現在做的很好。