2014-02-21 22 views
0

让这项工作有点麻烦。如果问题的格式不正确(或者问题简单),我们抱歉。新手Python程序员:P追加字符串列表次数等于单独列表中的数字

这里就是我想要做的事:

Step 1: Identify lists of strings matching criteria 

Step 2: Append specific string from lists to a new list 

Step 3: Do step 2 a number of times equal to an integer in a separate list 

有点混乱,我知道了。希望我的代码有助于解释它更好一点:

# Create Lists 
trial_length = [2570, 2573, 2575, 2565, 2569, 2499, 2565, 2559, 2563, 2491, 
       2574, 2560, 2566, 2572, 2567, 2507, 2571, 2560, 2573, 2570] 
condition = [] 

# Access file that I'm parsing 
foo = open('test.asc','rw+') 
for line in foo: 
    # Turn lines of file into individual lists 
    new_line = line.split() 
    # Select relevant lines 
    if len(new_line)==6: 
     if new_line[4]=='TEST': 
      # Append new_line to "condition" a number of times 
      # equal to integer in "trial_length" 
      for n in trial_length: 
       for t in range(0,n): 
        condition.append(new_line)  

当然,我现在意识到这是不正确的。例如,对于“trial_length”中的第一个数字,它将new_line附加到“条件”2570次,然后重复该过程20次。我认为这只是我编码逻辑中的一个错误。我在这里错过了什么?

编辑:

应该已经发布此更早。我期望的最终结果将是“测试”附加到“条件”2570次+ 2573次+ ...等。使用这个当前代码,它被追加1,022,980次,这是“trial_length”* 20中的数字的总和。

我这样做的原因是我使用的实际文件是由实验结果。该实验有20个试验,每个试验都在两种条件之一下运行:“一致”或“不一致”。每个试验运行不同的长度(例如2570秒,2573秒等)。对于试验中的每一行,我想在单独的列中附加正确的条件。因此,我正在创建一个字符串“Congruent”和“Incongruent”,并以正确的数量和顺序添加。因此,在短期

,我的名单应该是这样的:

condition = ['Congruent' * 2570 times, 'Incongruent' * 2573 times, 
      'Incongruent' * 2575 times,...etc.]. 

我没有提到这一点前面的原因是,我想问题缩小到了我认为最简单的形式,这是将一个字符串附加到列表的次数等于单独列表中的整数。

编辑2:

针对2rs2rt,我输入文件是一个ASCII/ASCE文件,该文件基本上只是串线。例如,斜体和粗体是我针对的线路之一:

EFIX R 718603 719256 654 285.0 370.0 1105 
719257 288.6 370.8 1064.0 ... 
END 719258 SAMPLES EVENTS RES 29.38 28.94 
INPUT 719259 127 
***MSG 719276 !V TRIAL_VAR CONGRUENT 331*** 
MSG 719277 !V TRIAL_VAR direction 20 
MSG 719278 TRIAL_RESULT 0 
MSG 719279 UPDATE_JITTER_1 
MSG 719283 -5 INSTRUCTIONS 
MSG 719649 0 JITTER 
MSG 719649 TRIALID 2 
MSG 719665 RECCFG CR 1000 2 1 R 
MSG 719665 ELCLCFG MTABLER 
MSG 719665 GAZE_COORDS 0.00 0.00 1023.00 767.00 
MSG 719665 THRESHOLDS R 105 226 
MSG 719665 ELCL_PROC CENTROID (3) 
MSG 719665 ELCL_PCR_PARAM 5 3.0 

出所有这些行的,我选择的是有“全等”或“不一致”,在他们的人。现在,这里是实际数据线的例子:

719667 296.3 380.1 1165.0 ... 
719668 296.0 379.9 1163.0 ... 
719669 296.2 379.7 1161.0 ... 
719670 296.5 379.3 1159.0 ... 
719671 296.7 379.0 1160.0 ... 
719672 296.9 378.8 1160.0 ... 

现在想象一下,这些线路回事2570倍,2573倍,等等。如果“全等”被确定为在这些线路末端的条件变量(标通过“END 719258 SAMPLES EVENTS RES 29.38 28.94”一行),然后将“一致”添加到这个新列表中多次,等于试验的长度。如果“不一致”,则追加“不一致”次数。

+0

什么是你想要的最终结果是什么样子? –

+0

'open('test.asc','rw +')'是否适合你?我的意思是,使用'rw +''。 –

+0

您第一次找到符合条件的行时,您希望追加多少个new_line实例?说'''trial_length = [1,2,3]'''和'''new_line = ['a','b','c','d','TEST','e']''' - 条件应该是什么样子? – wwii

回答

0

我想你想使用一个iterator,因为你想做的n追加不同线路的呼叫。听起来像第一个'TEST'行应该使用trial_length[0],第二个应该使用trial_length[1],等等。

currLength = iter(trial_length) 
with open('test.asc','rw+') as foo: 
    for line in foo: 
     # Turn lines of file into individual lists 
     new_line = line.split() 
     # Select relevant lines 
     if len(new_line)==6 and new_line[4]=='TEST': 
      # Append new_line to "condition" a number of times 
      # equal to integer in "trial_length" 
      try: 
       n = next(currLength) 
      except StopIteration: 
       break 
      for t in range(0,n): 
       condition.append(new_line) 

,让您每次使用新的元素从trial_length这将使你读'TEST'线。 next()要求迭代器给你序列中的下一个元素。当你用尽元素时,调用next()将抛出StopIteration异常。

这实际上是for循环的工作方式。他们依靠内部方法__iter__

如果您不需要检查某条件是否符合条件,则可以使用enumerate()

+0

我会试一试,让你知道它是怎么回事。谢谢:) – user1456918

+0

完美!非常感谢!当我有足够的声望时,我一定会报复你;) – user1456918

-1

它不追加20倍 其追加NEW_LINE 2570 + 2573 + ...... + 2573 + 2570(51149)次的条件 你只是希望它是2570倍,而不是休息

for t in range(trial_length[0]): 
    condition.append(new_line) 
+0

显然,OP正在使用'trial_length'中的其他数字。 – 2rs2ts

0

我不确定从trial_length列表中选择号码的标准是什么。但是原因是,在2570次之后重复20次是因为trial_length阵列中有20个元素。

在问题陈述的第3步中,您提到了Do step 2 a number of times equal to an integer in a separate list。什么是选择这个整数的标准?如果有标准,则用该逻辑替换您的for n in trial_length:

0

您需要根据某些条件从trial_length中选择一个项目el。那么你可以做

for t in range(el): 
    condition.append(new_line) 
0

你能避免调用由只使用类似追加N次:

condition.extend(value for i in xrange(N)) 
相关问题