Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List/set
#1
Hi 
I need little help.
I have five Set on  list. Like this:
 list = [{"a","b","c","d"},{"k","f","r","c"},{"c","a","o","r"},{"m","p","l"},{"i","r","p","b"}] 
How can I find what sets are the resemblance,And print their indexes like this:(0,2)

(Apr-25-2017, 08:56 AM)janek30 Wrote: Hi I need little help. I have five Set on list. Like this:
 list = [{"a","b","c","d"},{"k","f","r","c"},{"c","a","o","r"},{"m","p","l"},{"i","r","p","b"}] 
How can I find what sets are the most resemblance,And print their indexes like this:(0,2)
Reply
#2
@User-janek30
You can use index() method which gets you the index number of each element of the list/tuple/dict that you're iterating

list = [{"a","b","c","d"},{"k","f","r","c"},{"c","a","o","r"},{"m","p","l"},{"i","r","p","b"}]
so if you loop trough your list and use the following print(list.index(x)), it will give you each index number of each set.
First you would get the index of the first set which is 0 then 1, 2 and so on.

0
1
2
3
4
Reply
#3
 There are five sets in one list, and I need to find 2 most of the same elements .and print their indexs...
Reply
#4
what have you tried? show us your code and ask specific questions.
the flow is something like this:
1.loop over the elements of the list
2. compare each of set with the rest elements till the end of the list, e.g. compare element 0 wit elements 1-4, then element 1 with elements 2-4 and so on...
3. find the number of common elements (you may want to use intersection of sets to find the common elements)
4. check if this number (of common elements) is greater (or equal) to the latest highest number of common elements
5. based on 4. add the indexes ot a result list, or replace the latest result list

in your example you have 2 pairs of sets with 2 common elements, so the result would be [(0,2),(1,2)]
Reply
#5
I'm stuck here :
 
list = [{"a","b","c","d"},{"k","f","r","c"},{"c","a","o","r"},{"m","p","l"},{"i","r","p","b"}]
for i in range(len(list)):
    for j in range(i+1,len(list)): 
        if list[i] & list[j]:
            print(list[i]&list[j]) 
I don't understand how to find the most resemblance and how and print their indexs........ 
Reply
#6
we don't iterate over elements of a list like this (i.e. using range(len(list)) in Python.
also, don't use list or other reserved words as names of variables:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list('some string')
['s', 'o', 'm', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
>>> list=['a','b']
>>> list('some string')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
#! /usr/bin/python3
lst = [{"a","b","c","d"},{"k","f","r","c"},{"c","a","o","r"},{"m","p","l"},
        {"i","r","p","b"}]
indexes=[]
max_common_elements = 0
for i, set1 in enumerate(lst):
   for k, set2 in enumerate(lst[i+1:], start=i+1):
       num_common_elements = len(set1 & set2)
       if num_common_elements > max_common_elements:
           indexes = [(i, k),]
           max_common_elements = num_common_elements
       elif max_common_elements == num_common_elements:
           indexes.append((i,k))
print('{}, max # common elements:{}'.format(indexes, max_common_elements))
Reply
#7
Thanks Buran! it works.......
Reply
#8
(Apr-25-2017, 05:13 PM)janek30 Wrote: Thanks Buran! it works.......
Thanks for letting me know :-)
I wouldn't post code if it doesn't work.
Reply


Forum Jump:

User Panel Messages

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