Python 文件预处理(将列从离散值范围转换为连续值范围。)

标签 python algorithm sorting

我有以下形式的数据集:

user_id::item_id1::rating::timestamp
user_id::item_id2::rating::timestamp
user_id::item_id3::rating::timestamp
user_id::item_id4::rating::timestamp

我要求 item_ids(按排序顺序有 n 个不同的项目 ID。后续行可以具有相同的项目 ID 或不同但保证排序)从 1 到 n 是连续的,它们当前的范围是从 1 到k

对于 k >> n

我有以下代码,但它不是很正确,我已经用了几个小时了,所以非常感谢对此的任何帮助,或者如果有更简单的方法可以在 python 中执行此操作,我将非常感谢指导对此也是如此。

我目前有以下代码:

def reOrderItemIds(inputFile,outputFile):
        #This is a list in the range of 1 to 10681.
        itemIdsRange = set(range(1,10682))
        #currKey = 1
        currKey = itemIdsRange.pop()
        lastContiguousKey=1
        #currKey+1
        contiguousKey=itemIdsRange.pop()
        f = open(inputFile)
        g = open(outputFile,"w")
        oldKeyToNewKeyMap = dict()
        for line in f:
                if int(line.split(":")[1]) == currKey and int(line.split(":")[1])==lastContiguousKey:
                        g.write(line)
                elif int(line.split(":")[1])!=currKey and int(line.split(":")[1])!=contiguousKey:
                        oldKeyToNewKeyMap[line.split(":")[1]]=contiguousKey
                        lastContiguousKey=contiguousKey
                        #update current key to the value of the current key.
                        currKey=int(line.split(":")[1])
                        contiguousKey=itemIdsRange.pop()
                        g.write(line.split(":")[0]+":"+str(lastContiguousKey)+":"+line.split(":")[2]+":"+line.split(":")[3])
                elif int(line.split(":")[1])==currKey and int(line.split(":")[1])!=contiguousKey:
                        g.write(line.split(":")[0]+":"+str(lastContiguousKey)+":"+line.split(":")[2]+":"+line.split(":")[3])

                elif int(line.split(":")[1])!=currKey and int(line.split(":")[1])==contiguousKey:
                        currKey = int(line.split(":")[1])
                        lastContiguousKey=contiguousKey
                        oldKeyToNewKeyMap[line.split(":")[1]] = lastContiguousKey
                        contiguousKey=itemIdsRange.pop()
                        g.write(line.split(":")[0]+":"+str(lastContiguousKey)+":"+line.split(":")[2]+":"+line.split(":")[3])
        f.close()
        g.close()

例子:

1::1::3::100
10::1::5::104
20::2::3::110
1::5::2::104

我要求输出的形式是:

1::1::3::100
10::1::5::104
20::2::3::110
1::3::2::104

因此只有 item_ids 列发生变化,其他所有内容都保持不变。

如有任何帮助,我们将不胜感激!

最佳答案

因为您的数据已经按 item_id 排序 - 您可以使用 itertools.groupby() 来简化解决方案。

from operator import itemgetter
from itertools import groupby

item_id = itemgetter(1)
def reOrderItemIds(inputFile,outputFile):
    n = 1
    with open(inputFile)as infile, open(outputFile,"w") as outfile:
        dataset = (line.split('::') for line in infile)
        for key, group in groupby(dataset, item_id):
            for line in group:
                line[1] = str(n)
                outfile.write('::'.join(line))
            n += 1

关于Python 文件预处理(将列从离散值范围转换为连续值范围。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037836/

相关文章:

c - 给定一个由偶数和奇数组成的数组。先对数组进行排序,然后是赔率。数字的顺序不能改变

algorithm - 二 fork 树检测 - 为什么要比较 2 个遍历

c++ - 与多组件 key 的快速部分匹配

python - 查找小于给定数字的三胞胎

python - 机器学习 : normalize target var based on the impact of independent var

python - 使用类对象从外部文件创建 sqlite 数据库

asp.net - 决策者问题 : compare ASP. NET/Ruby/Python on web UI controls

python - 如何使用 python 从 url 中提取元描述?

javascript - Meteor + Mongo 平均计数

html - 排序触控笔内容