【问题标题】:Print data between positions within a loop在循环内的位置之间打印数据
【发布时间】:2017-11-01 16:09:25
【问题描述】:

我有一个文件。 File1 有 3 列。数据以制表符分隔

文件1:

2  4  Apple
6  7  Samsung

假设我运行 10 次迭代的循环。如果迭代在 File1 的第 1 列和第 2 列之间有值,则从 File1 打印相应的第 3 列,否则打印“0”。

列可能已排序,也可能未排序,但第 2 列始终大于第 1 列。两列中的值范围在行之间不重叠。

输出结果应该是这样的。

结果:

0
Apple
Apple
Apple
0
Samsung
Samsung
0
0
0

我的python程序在这里:

chr5_1 = [[]]
for line in file:
  line = line.rstrip()
  line = line.split("\t")
  chr5_1.append([line[0],line[1],line[2]])
  # Here I store all position information in chr5_1 list in list
chr5_1.pop(0)
for i in range (1,10):
  for listo in chr5_1:            
      L1 = " ".join(str(x) for x in listo[:1])
      L2 = " ".join(str(x) for x in listo[1:2])
      L3 = " ".join(str(x) for x in listo[2:3])
      if int(L1) <= i and int(L2) >= i:
          print(L3)
          break
      else:
          print ("0")
          break

我对循环迭代及其断点感到困惑。

【问题讨论】:

  • 不清楚规范是什么。结果中的0 来自哪里?为什么第三列值打印多次?什么决定了第三列值被打印的次数?规则是什么?!
  • 我已经编辑了帖子:假设我运行 10 次迭代的循环。如果迭代在 File1 的第 1 列和第 2 列之间有值,则从 File1 打印相应的第 3 列,否则打印“0”。
  • 文件中的行是否按第一列排序?两列中的值范围是否可以在行之间重叠 - 第一行可以有2 5,第二行可以有4 8吗?
  • 我重新编辑了帖子:列可能会或可能不会排序,但第 2 列总是大于第 1 列。两列中的值范围在行之间不重叠。

标签: python for-loop


【解决方案1】:

试试这个:

chr5_1 = dict()
for line in file:
    line = line.rstrip()
    _from, _to, value = line.split("\t")
    for i in range(int(_from), int(_to) + 1):
        chr5_1[i] = value

for i in range (1, 10):
    print chr5_1.get(i, "0")

【讨论】:

  • 哦...我忘了 dict...它工作得很好...谢谢
  • @kashiff007,没问题
【解决方案2】:

我认为这是else 的工作:

position_information = []
with open('file1', 'rb') as f:
    for line in f:
        position_information.append(line.strip().split('\t'))

for i in range(1, 11):
    for start, through, value in position_information:
        if i >= int(start) and i <= int(through):
            print value
            # No need to continue searching for something to print on this line
            break
    else:
        # We never found anything to print on this line, so print 0 instead
        print 0

这给出了您正在寻找的结果:

0
Apple
Apple
Apple
0
Samsung
Samsung
0
0
0

【讨论】:

    【解决方案3】:

    设置:

    import io
    s = '''2  4  Apple
    6  7  Samsung'''
    
    # Python 2.x
    f = io.BytesIO(s)
    # Python 3.x
    #f = io.StringIO(s)
    

    如果文件的行没有按第一个排序:

    import csv, operator
    reader = csv.reader(f, delimiter = ' ', skipinitialspace = True)
    f = list(reader)
    f.sort(key = operator.itemgetter(0))
    

    读取每一行;做一些数学计算来确定要打印的内容以及要打印的数量;打印东西;迭代

    def print_stuff(thing, n):
        while n > 0:
            print(thing)
            n -= 1
    limit = 10
    prev_end = 1
    
    for line in f:
        # if iterating over a file, separate the columns
        begin, end, text = line.strip().split()
        # if iterating over the sorted list of lines
        #begin, end, text = line
    
        begin, end = map(int, (begin, end))
        # don't exceed the limit
        begin = begin if begin < limit else limit
        # how many zeros?
        gap = begin - prev_end
        print_stuff('0', gap)
        if begin == limit:
            break
        # don't exceed the limit
        end = end if end < limit else limit
        # how many words?
        span = (end - begin) + 1
        print_stuff(text, span)
        if end == limit:
            break
        prev_end = end
    # any more zeros?
    gap = limit - prev_end
    print_stuff('0', gap)
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 2015-04-05
      • 2016-06-23
      • 1970-01-01
      相关资源
      最近更新 更多