Jump to content

What I'm trying to do is write two functions that each generate a list of random numbers. After the lists are generated I want to use another block of code to check the elements in list 1 and if an element from list 1 is in list 2, I want it to append that element to a new list and at the end print out this new list. I can't get the functions to cooperate. I thought this should work only because the functions take no arguments. Thanks in advance. Ignore the top commented out block. Here's what I got.

Link to comment
https://linustechtips.com/topic/427976-list-cross-checker-in-python/
Share on other sites

Link to post
Share on other sites

You need to store the results of the random functions in a variable.

ints1 = random_list1()ints2 = random_list2()

Also you don't need two different functions if they both do the same thing. One of the reasons to use functions is to remove duplicate code.

Link to post
Share on other sites

You need to store the results of the random functions in a variable.

ints1 = random_list1()ints2 = random_list2()

Also you don't need two different functions if they both do the same thing. Of one the reasons to use functions is to remove duplicate code.

Wow. That is so simple. Thank you. Why couldn't I think of that?

Link to post
Share on other sites

neons, on 11 Aug 2015 - 2:18 PM, said:

Wow. That is so simple. Thank you. Why couldn't I think of that?

As a testament to how much I love Python, I'd also like to show you the following:

You can replace your entire for loop to compare those lists, with one single list comprehension:

 

combined = []for element in ints1:    if element in ints2:        combined.append(element)    else:        continue
becomes

 

combined = [e for e in ints1 if e in ints2]
I've also cleaned up your code a tiny bit and made it a bit more "Pythonic" in how it works. Let me know if you have any questions! You were casting a lot of stuff as sets/lists where you really didn't need to. Python is good about figuring out what data structures you're working with for the most part.

 

import random  def random_list():    min = int(raw_input('How low do you wanna go?: '))    max = int(raw_input('Only the universe is the limit!: '))    length = int(raw_input('This list can be any length!: '))    #Error correction    if min > max:        print 'Error. The bigger number can not be smaller than the lower value.\nTry again.'        exit(1)    return [random.randrange(min, max+1) for e in xrange(0, length)] def main():    #Compares lists and makes new list with only common elements with no repeats    ints1 = random_list()    ints2 = random_list()     combined = [e for e in ints1 if e in ints2]    print combined if __name__ == '__main__':    main()

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

combined = [e for e in ints1 if e in ints2]

Or for bigger lists it would be much better to use sets.

combined = list(set(ints1) & set(ints2))
edit: Just for funsies i decided to test it, with 2 lists of 1,000,000 elements each, using sets finishes almost instantly while the list comprehension has been running for about 20 minutes now and is still going.

Edit edit: Reread the op and missed a part, the sets won't work if you want to include duplicates.

1474412270.2748842

Link to post
Share on other sites

List comprehension does use more memory as I believe it's loaded into RAM while it's running the comprehension, and as a result takes a long time... As sets get larger and larger, they're harder and harder to do operations on. Plus a for loop is simply an awful way to do the comparison, especially considering they aren't even in order. It's literally looping over a set a million times each time to check if the element exists. Sorting both sets would speed up the process.

 

All that said... anding the sets together does yield the correct result, in this case, since we can assume OP is looking for a list of duplicate elements. 

 

Anding them is definitely the faster option for large sets.
 
Running the following script yields these results: https://gist.github.com/NeilHanlon/4ee3b0dd10e8ee466ae0

 

400000 elements:

Sets are the same? FalseComprehension method took: 3999.37800002sAnding method took: 0.125sComprehension has 160177 elemsAnding has 160177 elemsr1 == r2: True

 
With 200000 elements:
 

Sets are the same? FalseComprehension method took: 1272.24699998sAnding method took: 0.0780000686646sComprehension has 40245 elemsAnding has 40245 elemsr1 == r2: True

 
With 100000 elements:
 

Sets are the same? FalseComprehension method took: 524.167999983sAnding method took: 0.0420000553131sComprehension has 9922 elemsAnding has 9922 elemsr1 == r2: True

50000 elements:
 

Sets are the same? FalseComprehension method took: 55.8589999676sAnding method took: 0.0139999389648sComprehension has 2545 elemsAnding has 2545 elemsr1 == r2: Tru

25000 elements:
 

Sets are the same? FalseComprehension method took: 11.364000082sAnding method took: 0.00600004196167sComprehension has 645 elemsAnding has 645 elemsr1 == r2: True

 
12500 elements:
 

Sets are the same? FalseComprehension method took: 3.18000006676sAnding method took: 0.0019998550415sComprehension has 166 elemsAnding has 166 elemsr1 == r2: True

 
This yielded some interesting graphs of time vs elements
 
2015-08-12_2242.png
 
2015-08-12_2242.png

--Neil Hanlon

Operations Engineer

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×