python基础:如何先获取完整的输入并稍后获得完整的输出?

时间:2016-11-11 20:38:24

标签: python python-2.7

好的,我刚刚开始学习python,我正在解决codechef问题 我写了以下解决方案 this问题:

testcase = input()
remain = []
chef = []
assistant = []
for _ in range(testcase):
    comp,rem = map(int,raw_input().split())
 for _ in range(1,comp+1):
    remain.append(_)
    temp = map(int,raw_input().split())
    remain = [x for x in remain if x not in temp]
  for i in range(len(remain)):
    if i%2==0:
        chef.append(remain[i])
    else:   
        assistant.append(remain[i])
  sarr = [str(a) for a in chef]
  darr = [str(a) for a in assistant]        
  print " ".join(sarr)
  print " ".join(darr)  
  remain[0:] = []
  chef[0:] = []
  assistant[0:] = []

现在程序正在做它应该做的事情但是我想在获得输出之前获取所有输入,如this问题示例中所示

2 个答案:

答案 0 :(得分:0)

将所有输入读入某种集合(列表将是最直接的选择),然后从该集合中获取它们以进行处理。

答案 1 :(得分:0)

执行类似的操作,然后再处理存储的输入:

comp=[]
rem=[]
indexes = []

for _ in range(testcase):
    rem_t,comp_t = map(int,raw_input().split())
    rem.append(rem_t)
    comp.append(comp_t)

    for _ in range(1,comp[i]+1):
      indexes.append(map(int,raw_input().split()))

现在,对于每个测试用例i,您需要在循环内定义chefassitant列表并操纵存储在comp[i], rem[i] and indexes[i]中的输入:

for i in range(testcase):
    # define chef and assistant lists.
    # use comp[i], rem[i] and indexes[i].
    # print the the content of chef and assistant array.