• 注册
  • Python Python 关注:172 内容:11

    服工—字典

  • 查看作者
  • 打赏作者
    • Python
    • 服工—字典

      第一题:

      服工—字典

      aDict = {'1':'Mon','2':'Tue','3':'Wed','4':'Thu','5':'Fri',

               '6':'Sat','7':'Sun'}

      a = input()

      b = aDict.get(a)

      print(b)

      第二题:

      服工—字典

      服工—字典

      aDict = dict()

      while(True):

          s = input().split(':')

          if s == ['']:

              break

          aDict[s[0]] = s[1]

      s = input('请输入要查询的课程:\n')

      try:

          print(aDict[s])

      except:

          print('没有该门课程')

      思考:列表下标的索引,split()返回序列!!!,抛出异常

      第三题:

      服工—字典

      s1 = input().split()

      s2 = input().split()

      aDict = {}

      for i in range(len(s1)):

          aDict[s1[i]] = s2[i]

      temp = sorted(aDict.keys())

      result = []

      for j in temp:

          result = result + [(j,aDict[j])]

      print(result)

      第四题:

      服工—字典

      data = list(map(int,input().split(',')))

      goal = int(input())

      flag = False

      for i in range(len(data)):

          for j in range(i+1,len(data)):

              if(data[i]+data[j] == goal):

                  print('{} {}'.format(i,j))

                  flag = True

      if (flag == False) :

          print('no answer')

          

      第五题:待解决

      服工—字典

      服工—字典

      oDict = eval(input())

      tDict = eval(input())

      one = set(oDict.keys())

      two = set(tDict.keys())

      one.update(two)

      keys = sorted(one)

      values = []

      for i in keys:

          temp = oDict.get(i)+tDict.get(i)

          values.append(temp)

      result = dict(zip(keys,values))

      方式二:

      oDict = eval(input())

      temp = oDict.copy()

      tDict = eval(input())

      oDict.update(tDict)

      for i in oDict:

          if i in temp and i in tDict:

              oDict[i] = temp[i] + oDict[i]

      keys = list(oDict.keys())

      keys.sort()

      result = {}

      for i in keys:

          result[i] = oDict.get(i)

      print(result)

      第六题:

      服工—字典

      s = input()

      aSet = set(s)

      aDict = {}

      for i in s:

          aDict[i] = s.count(i)

      aList = list(aDict.items())

      aList.sort(key = lambda x:x[1],reverse = True)

      for i in aList:

          print('{} : {}'.format(i[0],i[1]))

      思考:for i in s为了保证输出的原来序列,放入的时候按顺序放,因而先for i in aSet错误!!!

      服工—字典

      服工—字典

      请登录之后再进行评论

      登录
      帖子间隔 侧栏位置:
      关闭窗口
      下载海报