Python Forum
Making lists using itertools and eliminating duplicates.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making lists using itertools and eliminating duplicates.
#1
I'm trying to get all combinations of the following numbers as long as they don't total over 50,000 and must be in combos of 6. From there I want to condense into a new list that has no duplicates even if they're are out of order (ex: [8000, 9300, 7000, 8800, 7400, 9200] would be eliminated if [8800, 7400, 9200, 8000, 9300, 7000] was already in the list).

I think my code is correct but it is taking forever for the ouput to load in PyCharm. For now, I'm just looking to see if the len of the lists will be different to see if it is worth eliminating duplicates.

Sorry but I don't know how to get it to hold the indents when I post.

import itertools

num = {
9200, 8200, 7600, 9100, 7400, 8900, 7900,
7500, 7700, 6800, 9300, 9000, 7000, 8000,
8600, 7100, 8800, 7300, 8300, 8700, 8500,
9400, 6900, 7200
}

entry = []
for i in num:
for combination in itertools.combinations(num, 6):
if sum(combination) <= 50000:
entry.append(combination)

non_dup = []
for i in entry:
if i not in non_dup:
non_dup.append(i)



print("Entry list: " + str(len(entry)))
print("Non-Dup list: " + str(len(non_dup)))
Reply
#2
Figured it out. Sorry, I don't know how to delete a post.

print(len(list(set(entry))))
Reply
#3
You use bbcode "python" tags around your code in the post to display properly. You can click the Python logo button above the editor to insert them automatically.

If you just need the size of the set, you don't have to convert it to a list. len() works just fine on sets, so

print(len(set(entry)))
should be sufficient. If you don't need the ordering, you could just create non_dup as a set in the first place and then add objects to it (rather than append them).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminating error in Python update-alternatives Led_Zeppelin 2 301 Apr-14-2021, 12:49 PM
Last Post: Led_Zeppelin
  Removal of duplicates teebee891 1 264 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  eliminating letters when typed in python window deebo 0 223 Jan-05-2021, 09:26 AM
Last Post: deebo
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 320 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Displaying duplicates in dictionary lokesh 2 418 Oct-15-2020, 08:07 AM
Last Post: DeaD_EyE
  Python3 itertools.groupby printing the key august 1 537 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 483 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 1,700 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  how do i pass duplicates in my range iterator? pseudo 3 751 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  Deleting duplicates in tuples Den 2 1,266 Dec-14-2019, 10:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020