Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>import sys #Read the input file input_data = [line.rstrip().split(",") for line in open("input.txt", 'r').readlines()] #Put the value and index of each line into a dict, #categorizing by the dataset/group name. #Each different dataset/group is a key of the dict, #and each key's value is a list. group_dict = {} index = 0 for line in input_data: group_key = line[0]+","+line[1] if group_key not in group_dict.keys(): group_dict[group_key] = [] group_dict[group_key].append([index, line[2], None]) index += 1 #Sort each list of the dict by the numbers. #Make blank to be a very large number. for key in group_dict.keys(): group_dict[key] = sorted(group_dict[key], key=lambda x: sys.maxint if x[1]=="" else int(x[1])) #####group_dict[key] = group_dict[key][::-1] ##### Uncomment the above line to sort in descending order #Check if there're multiple items with the same number, #If so, set them by the same rank. group_dict[key][0][2] = 1 for i in range(1, len(group_dict[key])): group_dict[key][i][2] = (group_dict[key][i-1][2] if group_dict[key][i][1] == group_dict[key][i-1][1] else i+1) #In order to keep the same line order with the input file, #get all the lists together into a new list, #and sort them by the line index (recorded when put them into the dict). rank_list = [] for rank in group_dict.values(): rank_list += rank rank_list = sorted(rank_list, key=lambda x: x[0]) for rank in rank_list: input_data[rank[0]][4] = str(rank[2]) #Output the final list. for line in input_data: print ",".join(line) </code></pre> <hr> <p>Test:</p> <p>Input:</p> <pre><code>uniquedata1,uniquecell1,123,data,99,data uniquedata1,uniquecell1,,data,99,data uniquedata1,uniquecell1,111,data,99,data uniquedata2,uniquecell2,456,data,99,data uniquedata2,uniquecell2,,data,99,data uniquedata2,uniquecell2,,data,99,data uniquedata2,uniquecell2,789,data,99,data uniquedata1,uniquecell2,386,data,99,data uniquedata1,uniquecell2,512,data,99,data uniquedata1,uniquecell2,486,data,99,data </code></pre> <p>Output:</p> <pre><code>uniquedata1,uniquecell1,123,data,2,data uniquedata1,uniquecell1,,data,3,data uniquedata1,uniquecell1,111,data,1,data uniquedata2,uniquecell2,456,data,1,data uniquedata2,uniquecell2,,data,3,data uniquedata2,uniquecell2,,data,3,data uniquedata2,uniquecell2,789,data,2,data uniquedata1,uniquecell2,386,data,1,data uniquedata1,uniquecell2,512,data,3,data uniquedata1,uniquecell2,486,data,2,data </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload