【问题标题】:Iterate table and return count of pairs on lists in Python在 Python 中迭代表并返回列表上的对数
【发布时间】:2020-02-19 15:20:51
【问题描述】:

有一列人和一列说这些人的位置,他们的位置按时间排序,例如:

People=["A", "A", "B", "B", "B", "C", "C", "C"]
Location=["GA", "IL", "GA", "IL", "GA", "CA", "IL", "GA"]

你将如何编写一个返回这个的迭代:

Change_of_location=["GA-IL","IL-GA","CA-IL]count=[2, 2, 1],因为“GA-IL”对出现了两次,等等。也就是说,迭代应该根据人们的位置行返回所采取的旅行(位置变化)。

【问题讨论】:

    标签: python-3.x iteration


    【解决方案1】:

    我将使用基于zip 工具的配方。

    people = ["A", "A", "B", "B", "B", "C", "C", "C"]
    location = ["GA", "IL", "GA", "IL", "GA", "CA", "IL", "GA"]
    
    chg_of_loc = {}
    
    for first, second, loc1, loc2 in zip(people, people[1:], location, location[1:]):
        # if version(python) < 3.6 use format 
        start_end = f'{loc1}-{loc2}'
        if first == second:
            chg_of_loc[start_end] = chg_of_loc.get(start_end, 0) + 1
    
    
    print(f"Change={list(chg_of_loc.keys())} and count={list(chg_of_loc.values())}")
    

    【讨论】:

      【解决方案2】:

      我将一些似乎在 Python 3.6 中执行此操作的简单代码放在一起。

      #PYTHON 3.6
      
      # Input arrays
      people = ["A", "A", "B", "B", "B", "C", "C", "C"]
      location = ["GA", "IL", "GA", "IL", "GA", "CA", "IL", "GA"]
      
      # Output arrays
      Change_of_location = []
      count = []
      
      # Other variables
      temp = []           # Temporary array for storing sorted values
      lastPerson = ''     # String for storing last person in array
      cnt = 0             # Counter for cycling through array
      
      # Cycle through the records of people
      for i in people:
          # Check if the current person is the same as the last
          if lastPerson == people[cnt]:
              # Save the locations travelled by this person
              temp.append(location[cnt-1] + '-' + location[cnt])
          # Update the last person and increment the counter
          lastPerson = people[cnt]
          cnt+=1
      
      # Sort the temporary array
      temp.sort()
      
      # Create a counter variable for cycling through the temporary array,
      #  a variable for counting the number of trips, and a string to store
      #  the last trip
      cnt2 = 0
      cntTrip = 1
      lastTrip = ''
      
      # Cycle through the temporary array
      for i in temp:
          # check if the current trip is the same as the last
          if lastTrip == temp[cnt2]:
              # increment the counter
              cntTrip+=1
          # else, check if the last trip string is empty (first iter)
          elif lastTrip != temp[cnt2] and lastTrip != '':
              # append trip to array
              Change_of_location.append(lastTrip)
              # append trip count to array
              count.append(cntTrip)
              # reset the trip counter to 1
              cntTrip = 1
          # set the last trip to the current trip
          lastTrip = temp[cnt2]
          # increment the counter
          cnt2+=1
      
      # Append the last values from the temporary array
      Change_of_location.append(lastTrip)
      count.append(cntTrip)
      
      # Print the results
      print(Change_of_location)
      print(count)
      

      肯定有更有效的方法来做到这一点;但这有效且不依赖于任何导入。

      输出是 Change_of_location = ['CA-IL','GA-IL','IL-GA'] 和 count = [1,2,2]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-20
        • 2015-09-23
        • 1970-01-01
        • 2022-01-12
        相关资源
        最近更新 更多