1. ”’ 
      题目: 
      给定一个int list a,满足a[i+1]>=a[i],给定int key,找出list a中第一个大于等于key的元素的index,无满足要求的元素则返回-1. 
      函数定义: 
      def findIndex(int_list,int_key)

      ”’

      #定义函数
      def findIndex(int_list, int_key):
          '''
          给定一个int list a,满足a[i+1]>=a[i],给定int key,找出list a中第一个大于等于key的元素的index,无满足要求的元素则返回-1.
          :param int_list: 
          :param int_key: 
          :return: 
          '''
          for i in int_list:
              if i >= int_key:
                  return int_list.index(i)
                  break
          if int_key not in int_list:
              return -1
      
      if __name__ == "__main__":
          #定义一个 int list,并初始化
          list1 = [1,1,2,2,3,3,4,4,5,5,6,6]
          #调用函数,并传入参数,注:传入的int_key值是一个不存在int_list的值.返回-1;
          index1 = findIndex(list1, 8)
          print (index1)
      
          # 调用函数,并传入参数,注:传入的int_key的值在int_list的值.返回其首次出现时对应的下标;
          index2 = findIndex(list1, 3)
          print(index2)

      ”’ 
      使用到的知识点的总结: 
      1.for循环语句 
      2.if判断语句 
      3.break的使用 
      break用在循环语句当中,用于结束当前循环.满足某种条件的时候,立即结束,跳出循环. 
      扩充:continue用在循环语句当中用于跳过本次循环,进入下次循环,用于加速循环. 
      4.列表的方法使用,查询方法(in, not in, index) 
      5.函数,有参数有返回值的函数,函数有返回值,在调用函数的时候,需要定义一个变量取接那个返回的值! 
      ”’

    2. 有一个3G大小的文件,文件每行一个string,内容为酒店的id和一个图片的名字,使用“ ”分割

      示例:ht_1023134 + " " + hidfadsfadsfdfadsf2r234523,jpg

      表示的是一个酒店包含的一张图片,统计含有图片数量为[20,无穷大]的酒店id,含有图片数量为[10,20]的酒店id、含有图片数量为[10,5]的酒店id,含有图片数量为[0,5]的酒店id,并将结果输出到文件中

      0-5 + “ ” + id1 +  “ ” + id2 + .....

      5-10 + “ ” + id1 +  “ ” + id2 + .....

      10-20 + “ ” + id1 +  “ ” + id2 + .....

      20-无穷大 + “ ” + id1 +  “ ” + id2 + .....

      from collections import Counter
      count_dict = {}
      cou = Counter()
      with open('a.txt', encoding='utf-8') as f:
          for line in f:
              hotel, image = line.split()
              hotel_id = hotel.split('_')[1]
              cou.update({hotel_id,1})
              if hotel_id in count_dict:
                  count_dict[hotel_id] += 1
              else:
                  count_dict[hotel_id] = 1
      del cou[1]
      zero_five = ['0-5']
      five_ten = ['5-10']
      ten_twenty = ['10-20']
      twenty_infinite = ['10-去穷大']
      for hotel_id,count in count_dict.items():
          if count < 5 :
              zero_five.append(hotel_id)
          elif count < 10 :
              five_ten.append(hotel_id)
          elif count < 20:
              ten_twenty.append(hotel_id)
          else:
              twenty_infinite.append(hotel_id)
      with open('b.txt','w',encoding='utf-8') as b:
          b.write('	'.join(zero_five))
          b.write('
      ')
          b.write('	'.join(five_ten))
          b.write('
      ')
          b.write('	'.join(ten_twenty))
          b.write('
      ')
          b.write('	'.join(twenty_infinite))
  • 相关阅读:
    错误日志记录
    解决连接Oracle 11g报ORA-01034和ORA-27101的错误
    windows和linux 下将tomcat注册为服务
    ORA-12170: TNS:Connect timeout occurred
    ubuntu修改时区和时间的方法
    SecureCRT中文显示乱码的解决方法
    修復 “Failed to bring up eth0″ in Ubuntu virtualbox
    HDU 1358 Period
    rcp(插件开发)点击按钮出现 The chosen operation is not enabled 解决办法
    CRM上线之路 走上了CRM实施顾问-第12天上班 -第三周
  • 原文地址:https://www.cnblogs.com/hizf/p/8481579.html
Copyright © 2020-2023  润新知