我有一个包含一些数据的rar文件,我想用它建立一个目录结构,并将它保存在一个excel文件中。我使用rarfile库读取数据,但它会像这样打印数据
Resources/Custom Brushes/ClothDamageVDM.ZBP, 55.02MB
Resources/Custom Brushes/ControledGravityFolds.ZBP, 0.14MB
Resources/Custom Brushes/GeoStitches.ZBP, 0.06MB
Resources/Custom Brushes/LeatherTexturer.ZBP, 0.23MB
Resources/Custom Brushes/MagicFolds_Hard.ZBP, 5.89MB
Resources/Custom Brushes/MagicFolds_Soft.ZBP, 5.89MB
Resources/Custom Brushes/PreasureSizeFolds.ZBP, 0.07MB
Resources/Custom Brushes/VDM-folds.ZBP, 9.41MB
Resources/Custom Brushes/WovenNanoBrush.ZBP, 0.05MB
Resources/Custom Brushes/Wrinkles_Wrap.ZBP, 0.75MB
Resources/Custom Brushes/Xfolds.ZBP, 0.52MB
Resources/Hotkeys.TXT, 0.0MB
Resources/Masking Alphas/Masking Fold Alpha 01.jpg, 0.29MB
我想用逗号替换这些行中的类似项,以便在excel工作表中很好地排列
我设法把这些行变成一个列表列表,每个列表都包含由/分隔的行的内容,现在我想用逗号替换重复的项,下面是我遇到的问题
我编写了以下代码来生成一个用逗号填充的新列表
original_list= [[1,2,1],[1,2,3],[1,3],[1,3,5]]
# copy a new list
new_list = original_list[:]
for i in range(1, len(new_list)):
# adding the second item in the original list to a set a
a = set(original_list[i])
# adding the first item in the original list to a set b
b = set(original_list[i-1])
# getting the similar items between them in set c
c = list(a.intersection(b))
# for the length of the similar items, go through the new list and change similar items into a ,
for d in range (0, len(c)):
new_list[i][d] = ','
print(original_list)
print(new_list)
#both list are changed
但当我更改新列表时,原来的列表也会更改。我不知道为什么当新列表不是一个引用时,我使用了[:]来确保它是一个新列表
我很确定有更好的方法来完成我的工作,所以如果你们能给我指出正确的方向,我会很感激的。现在,我被这个困住了
谢谢
您遇到的复制问题是因为您有嵌套列表,所以
list[:]
不够深入。查看this solution如果你不想使用一个库,你只需要在深层复制,这里有一个例子“暴力”“深层复制”使用列表理解来建立一个新的列表。如果需要的话,可以使用递归来复制多个级别来更新它,但是您已经明白了
如果您正在寻找代码评审,那么Code Review是在您的代码运行后询问问题的更合适的地方。堆栈溢出更适用于不工作的代码
相关问题 更多 >
编程相关推荐