Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图用字符替换列表中的类似项_Python_List_Set - Fatal编程技术网

Python 试图用字符替换列表中的类似项

Python 试图用字符替换列表中的类似项,python,list,set,Python,List,Set,我有一个包含一些数据的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/Cust

我有一个包含一些数据的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[:]
不够深入。退房

如果您不想使用库,只需要在深层复制,下面是一个使用列表理解构建新列表的“暴力”“深度复制”示例。如果需要的话,可以使用递归复制多个级别来更新它,但是您已经明白了

a = [[1, 2, 3], 9]
b = a[:]
b[0].append(4)

print(a)
print(b)

new = [x[:] if isinstance(x, list) else x for x in a]
new[0].append(5)
print()
print(a)
print(new)

如果您正在寻找代码审查,那么在代码运行后,这是询问问题的更合适的地方。堆栈溢出更适用于不工作的代码。

请提供预期输出的示例,谢谢。这就回答了问题。快速提问。我有什么理由不使用库吗?因为
copy
库是python的本机库,所以这并不是您不想或不能使用库的最好例子。不能使用软件包的一个例子是在公司防火墙后面使用一些新的开源软件包,或者在云环境中工作时受到限制。作为记录,我完全支持使用软件包,而不是重新发明轮子,但在某些情况下,你必须这样做。
[[1, 2, 3, 4], 9]
[[1, 2, 3, 4], 9]

[[1, 2, 3, 4], 9]
[[1, 2, 3, 4, 5], 9]