0

I have rar file that contains some data, and I want to make a directory structure out of that and save it in an excel file. I used rarfile library to read the data, but it prints the data out like this

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

I would like to replace the similar items in these lines with a comma so it is nicely arranged in the excel sheet.

I managed to turn these lines to a list of lists, each list contains the content of the lines separated by the /, now I would like to replace the repeated items with commas, and here is the problem i have encountered.

i wrote the following code to generate a new list populated with commas

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

but when I change the new list, the original changes as well. I don't know why when the new list is not a reference, I have used [:] to make sure it is a new list.

I'm quite sure there is a better method to what I am doing overall, so i would appreciate it if you guys can point me in the right direction. right now, I'm stuck at this.

thanks

ARTillery
  • 17
  • 5

1 Answers1

0

The copying issue you are having is because you have nested lists, so list[:] doesn't go "deep enough". Check out this solution.

If you don't want to use a library, and you only need to copy on level deep, here is an example of a "brute force" "deep copy" using list comprehension to build a new list. This could be updated using recursion to copy multiple levels if needed, but you get the idea.

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)
[[1, 2, 3, 4], 9]
[[1, 2, 3, 4], 9]

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

If you are looking for a code review, then Code Review is the more appropriate place to ask your question once your code is working. Stack Overflow is more intended for code that is not working.

cwalvoort
  • 1,851
  • 1
  • 18
  • 19
  • thank you. that answered it. quick question.Is there any reason why would I not use a library? – ARTillery Dec 25 '19 at 12:51
  • Since the `copy` library is native to python, this one isn't the greatest example of when you wouldn't want to, or couldn't, use one. An example of not being able to use a package would be using some new open source package behind a company firewall, or limitations when working within a cloud environment. For the record, I am all for using packages and not reinventing the wheel, but there are some situations when you have to. – cwalvoort Dec 26 '19 at 20:36