Differences between Python lists and sets

Asked 2 years ago, Updated 2 years ago, 97 views

If you try to find the same value among the two lists in Python, it is usually

for i in list1:
    for j in list2:
        if i == j :
            print(i)

I found it using the for statement twice, but I changed it to a set, and then I found the intersection built-in function If you use it, it's easy to find it. In the set, how do you find it? I wonder if I can find it quickly. (It's too fast to just be a difference in the structure of the data.) I'm sure it's a different Does the set sort to find the intersection?

python list set

2022-09-20 19:33

1 Answers

It's almost the same as selecting from a table with indexes and searching from a table without indexes.

And three, each element has a hash value that's indexed. It will be sorted, and binary research is available. You just have to go through the list.


2022-09-20 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.