当前位置: 首页>>代码示例>>Python>>正文


Python Parser.next方法代码示例

本文整理汇总了Python中Parser.Parser.next方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.next方法的具体用法?Python Parser.next怎么用?Python Parser.next使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Parser.Parser的用法示例。


在下文中一共展示了Parser.next方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: problem1

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import next [as 别名]
def problem1(filename):

  # dictionary storing occurence of bands
  bands_count = {}

  parser = Parser(filename)
  while True :
    try :
      current_line = parser.next()
    except StopIteration :
      break
    bands = current_line[1]
    # if the band has already been discovered, we increment the number 
    # of occurence if not, we insert it with 1 
    for band in bands:
      if band in bands_count :
        bands_count[band] += 1
      else :
        bands_count[band] = 1

  # n and m represent the first and second bigger occurences of bands
  # several bands can be stored into it because of ties, so we store the 
  # number of occurence and the list of bands
  n = [0]
  m = [0]
  for (b, i) in bands_count.items():
    # if we find a bigger count, we move n to m and create the new n
    if i > n[0] :
      m = n
      bands = [b]
      n = [i, bands]
    # if the count is the same than n (or m), we add the band to n (or m) list
    elif i == n[0]:
      n[1].append(b)
    elif i == m[0]:
      m[1].append(b)
    # if we find a count smaller than n but bigger than m we update the second
    elif i>m[0] :
      bands = [b]
      m = [i, bands]

  # then we print the names
  for name in n[1]:
    print name
  for name in m[1]:
    print name
开发者ID:rmelisson,项目名称:playing,代码行数:48,代码来源:problem1.py

示例2: problem3

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import next [as 别名]
def problem3(filename):
    parser = Parser(filename)

    # associate one band to all colleagues who like it
    # {'The Doors' : ['Alice', 'Bob'], 'U2' : ['Bob'], ... }
    bands_colleagues = {}

    # set of colleagues who are not satisfied yet
    unhappy_colleagues = set()

    while True:
        try:
            current_line = parser.next()
        except StopIteration:
            break
        name = current_line[0]
        bands = current_line[1]
        for band in bands:
            if band in bands_colleagues:
                bands_colleagues[band].add(name)
            else:
                bands_colleagues[band] = set()
                bands_colleagues[band].add(name)
        unhappy_colleagues.add(name)

    """
  While there is still unhappy colleagues :
    find the band who is the most liked by remaining unhappy colleagues
    add this band to the result
    remove colleague who like this band from the unhappy list
  """
    result = []
    while unhappy_colleagues:
        band = find_closer_band(bands_colleagues, unhappy_colleagues)
        result.append(band)
        for colleague in bands_colleagues[band]:
            if colleague in unhappy_colleagues:
                unhappy_colleagues.remove(colleague)

    for band in result:
        print band
开发者ID:rmelisson,项目名称:playing,代码行数:43,代码来源:problem3.py

示例3: problem2

# 需要导入模块: from Parser import Parser [as 别名]
# 或者: from Parser.Parser import next [as 别名]
def problem2(filename):

  parser = Parser(filename)
  bands_colleagues = {}
  
  while True :
    try :
      current_line = parser.next()
    except StopIteration :
      break
    colleague_name = current_line[0]
    bands = current_line[1]

    for band in bands :
      if band in bands_colleagues :
        bands_colleagues[band].append(colleague_name)
      else :
        bands_colleagues[band] = [colleague_name]

  for (band, names) in bands_colleagues.items():
    print band + ': ' + ', '.join(names)
开发者ID:rmelisson,项目名称:playing,代码行数:23,代码来源:problem2.py


注:本文中的Parser.Parser.next方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。