0
import os
import tqdm
list_name = []
path_dir = ['sofile_ads_og_pt']
path_so = "/Users/PycharmProjects/"

def listdir(path,list_name):  # 传入存储的list    
    for file in os.listdir(path):
        filecheck = file.split('.')
        if filecheck[0] == 'so' and filecheck[len(filecheck)-1] == 'log':
            file_path = os.path.join(path, file)
            # if os.path.isdir(file_path):
            #     listdir(file_path, list_name)
            # else:
            #     list_name.append(file_path)
        list_name.append(file_path)
    print(list_name)

    list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
    print(list_name)
    print("载入文件完成:",len(list_name))

if __name__ == '__main__':

    for dirname in (path_dir):
        list_name.clear()
        path_so_all = path_so + dirname
        listdir(path_so_all,list_name)
        print("main部分:")
        print(list_name)
        list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
        print(list_name)
        print(path_so_all)


    import os
    import tqdm
    list_name = []
    path_dir = ['sofile_ads_og_pt']
    path_so = "/Users/PycharmProjects/"
    def listdir(path,list_name):  # 传入存储的list
    
        for file in os.listdir(path):
            filecheck = file.split('.')
            if filecheck[0] == 'so' and filecheck[len(filecheck)-1] == 'log':
                file_path = os.path.join(path, file)
                # if os.path.isdir(file_path):
                #     listdir(file_path, list_name)
                # else:
                #     list_name.append(file_path)
            list_name.append(file_path)
        print(list_name)
    
        list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
        print(list_name)
        print("载入文件完成:",len(list_name))
    
    if __name__ == '__main__':
    
        for dirname in (path_dir):
            list_name.clear()
            path_so_all = path_so + dirname
            listdir(path_so_all,list_name)
            print("main 部分:")
            print(list_name)
            list_name = sorted(list_name, key=lambda x: (int(x.split('.')[1])))
            print(list_name)
            print(path_so_all)

output:

listdir funtion:

list_name = [not sorted]

list_name = [sorted]

main funtion:

list_name = [not sorted]

list_name = [sorted]

why the main part of list_name is not sorted ???

And if the list_name is local variable, why global list_name have items ???

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
Iaoceot
  • 1
  • 1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 15 '22 at 12:33
  • Does this answer your question? [How do I clone a list so that it doesn't change unexpectedly after assignment?](https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment) – Iguananaut Jun 15 '22 at 12:36
  • To answer the question in the title: because Python does not perform reference passing. Values are always passed by name. – Konrad Rudolph Jun 15 '22 at 13:04

1 Answers1

2

By using sorted you are creating a brand new list. This new list is assigned to to the variable list_name, but that name is local to the function; it doesn't change the list_name back out in main.

If you use sort instead, it will modify the list in place and that modification will be seen in both places.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • But if list_name is local variable, why in main part list_name have some items to output not empty list ? I'm fuzzy about this. And i add print(id(list_name)) in function listdir and main all output : 140649046215744, which means is pass-by-reference – Iaoceot Jun 15 '22 at 13:07
  • 1
    @Iaoceot because `append` modifies the list object without having to reassign it. And try printing the `id` after you sort. – Mark Ransom Jun 15 '22 at 13:10
  • oh which means sorted function change the list_name in Memory place? before sorted:id(list_name) = 140677502236544 after sorted:id(list_name) = 140676958919552 – Iaoceot Jun 15 '22 at 13:14