如果一行中有多個整數,如何將該行轉換爲整數。 (在完成行可能是「2 4」和完成= int(行)的原因和錯誤。從Python中的文件中讀取int時出錯
另外,我怎樣才能訪問特定元素的轉換在for循環底部。名單,但我需要比較特定元素
with open(r'C:\Users\Andrew\Documents\College\Theory of Computation\dfa19btest.txt', 'r') as f:
for i, line in enumerate(f):
if i == 0:
language = line
print("Language =", language)
if i == 1:
numstates = int(line)
print ("Number of states =", numstates)
if i == 2:
start = int(line)
print ("Start =", start)
#if i == 3:
#finish = int(line)
#print ("Finish =", finish)
if i >= 4:
transitions.append(line)
print ("Transitions = ", transitions[i -4])
for x in range(numstates-1, -1, -1):
print(transitions[x])
# string is in 4 a 2 b 3 format
# if [0] == [3] then answer insert [2]* into answer array
# if [2] and [4] are followed by same number insert [2]|[4]
應該只是'numstates = int(numstates)',但在兩個for循環中使用'i'是危險的:你不一定需要枚舉(使用'f.next()') – colcarroll