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
  remove partial duplicates from csv ledgreve 0 746 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  itertools and amazing speed Pedroski55 8 1,985 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  Problem : Count the number of Duplicates NeedHelpPython 3 4,282 Dec-16-2021, 06:53 AM
Last Post: Gribouillis
  Eliminating error in Python update-alternatives Led_Zeppelin 2 2,943 Apr-14-2021, 12:49 PM
Last Post: Led_Zeppelin
  Removal of duplicates teebee891 1 1,735 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  eliminating letters when typed in python window deebo 0 1,706 Jan-05-2021, 09:26 AM
Last Post: deebo
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,356 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Displaying duplicates in dictionary lokesh 2 1,936 Oct-15-2020, 08:07 AM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Python3 itertools.groupby printing the key august 1 2,046 Aug-17-2020, 05:46 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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