0

我正在尝试编写一个数值聚类工具。基本上,我有一个列表(这里称为“产品”),应该从升序列表转换为指示数据集中数字之间链接的列表。读取数据集,删除回车符和连字符都可以,但是根据数据集操作列表给我带来了问题。

# opening file and returning raw data
file = input('Data file: ')
with open(file) as t:
    nums = t.readlines()
    t.close()

print(f'Raw data: {nums}')

# counting pairs in raw data 
count = 0
for i in nums:
    count += 1
print(f'Count of number pairs: {count}')

# removing carriage returns and hyphens    
one = []
for i in nums:
    one.append(i.rsplit())
new = []
for i in one:
    for a in i:
        new.append(a.split('-'))
print(f'Data sets: {new}')

# finding the range of the final list
my_list = []
for i in new:
    for e in i:
        my_list.append(int(e))
ran = max(my_list) + 1

print(f'Range of final list: {ran}')
# setting up the product list
rcount = count-1
product = list(range(ran))
print(f'Unchanged product: {product}')
for i in product:
    for e in range(rcount):
        if product[int(new[e][0])] < product[int(new[e][1])]:
            product[int(new[e][1])] = product[int(new[e][0])]
        else:
            product[int(new[e][0])] = product[int(new[e][1])]

print(f'Resulting product: {product}')  

我希望结果是 [0, 1, 1, 1, 1, 5, 5, 7, 7, 9, 1, 5, 5],但是当使用不同的数据集。

用于给出上述所需产品的数据集如下:'1-2\n', '2-3\n', '3-4\n', '5-6\n', '7-8 \n', '2-10\n', '11-12\n', '5-12\n', '\n'

但是,我面临的最大问题是使用其他数据集。如果没有额外的回车,事实证明,我将出现列表索引超出范围错误。

4

1 回答 1

0

我无法完全弄清楚您在这里实际上要做什么。“表示联系”是什么意思,最终输出是如何做到的?另外,您能否展示一个实际失败的数据集示例?并提供您得到的实际异常?

无论如何,您的代码过于复杂,稍微清理一下也可以解决您的索引问题。nums从上面的示例中使用:

# Drop empty elements, split on hyphen, and convert to integers
pairs = [list(map(int, item.split('-'))) for item in nums if item.strip()]

# You don't need a for loop to count a list
count = len(pairs)

# You can get the maximum element with a nested generator expression
largest = max(item for p in pairs for item in p)

此外,在您的最终循环中,您在进行迭代的product同时还对其进行了就地修改,这往往不是一个好主意。如果我对您要达到的目标有更多了解,我可能会提出更好的方法。

于 2019-09-11T16:51:07.647 回答