Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Alan Gauld
 for i in range(len(a)):
 for k in range(len(a)):

for k in range(i,len(a)):

is faster, and if you calculate len before starting the 
loops that will speed it up too. (You calculate len for 
each iteration of each loop!)

 if i != k:
 if a[i] == a[k]:
 print a[i]
 break

HTH

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Srinivas Iyyer
Dear Danny, thank you for ur help. But a basic
question ?

In a table, more specifically a matrix 3X3,

AppleFruitDenmark
F-16 Fighter  USA
Taj  Wonder   India
MummyAntique  Egypt


IF I have to sort on country, it should be

AppleFruitDenmark
MummyAntique  Egypt
Taj  Wonder   India
F-16 Fighter  USA


How can I sort by binding Denmark to fruit and apple
as a string. String does not support sort function.
Sincerly I dont know how to sort to follow your
suggestion. can you please help.. sorry for asking
basic question.

thank you




--- Danny Yoo [EMAIL PROTECTED] wrote:

 
 
 On Wed, 26 Jan 2005, Srinivas Iyyer wrote:
 
  I have a list with 4 columns and column1 elements
 are unique.  I wanted
  to extract unique elements in column3 and and
 place the other elements
  of the column along with unique elements in column
 4 as a tab delim
  text.
 
  Table:
 
  col1col2col3   col4
  A   Apple 5Chennai
  B   Baby 11Delhi
  I   Baby* 1Delhi
  M   Dasheri+  5Mumbai
  K   Apple 12   Copenhagen
 
 
 [Meta: we seem to be getting a run of similar
 questions this week. Scott
 Melnyk also asked about grouping similar records
 together:

http://mail.python.org/pipermail/tutor/2005-January/035185.html.]
 
 
 Hi Srinivas,
 
 I see that you are trying to group records based on
 some criterion.  You
 may find the problem easier to do if you fist do a
 sort on that criterion
 column: that will make related records clump
 together.
 
 
 For your sample data above, if we sort against the
 second column, the
 records will end up in the following order:
 
 ###
 A   Apple 5Chennai
 K   Apple 12   Copenhagen
 B   Baby  11   Delhi
 I   Baby  1Delhi
 M   Dasheri   5Mumbai
 ###
 
 
 In this sorting approach, you can then run through
 the sorted list in
 order.  Since all the related elements should be
 adjacent, grouping
 related lines together should be much easier, and
 you should be able to
 produce the final output:
 
 ###
 Apple A,K 5,12Chennai,Copenhagen
 Baby  B,I 1,11Delhi
 Dasheri   M   5   Mumbai
 ###
 
 without too much trouble.  You can do this problem
 without dictionaries at
 all, although you may find the dictionary approach a
 little easier to
 implement.
 
 
 
 
  A dictionary option does not work
 
 A dictionary approach is also very possible.  The
 thing you may be stuck
 on is trying to make a key associate with multiple
 values.  Most examples
 of dictionaries in tutorials use strings as both the
 keys and values, but
 dictionaries are more versatile: we can also make a
 dictionary whose
 values are lists.
 
 
 For example, here is a small program that groups
 words by their first
 letters:
 
 ###
  def groupAlpha(words):
 ... groups = {}
 ... for w in words:
 ... firstLetter = w[0]
 ... if firstLetter not in groups:
 ... groups[firstLetter] = []
 ... groups[firstLetter].append(w)
 ... return groups
 ...
  groupAlpha(this is a test of the emergency
 broadcast system.split())
 {'a': ['a'],
  'b': ['broadcast'],
  'e': ['emergency'],
  'i': ['is'],
  'o': ['of'],
  's': ['system'],
  't': ['this', 'test', 'the']}
 ###
 
 
 If you have more questions, please feel free to ask.
 
 




__ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique Items in Lists

2005-01-27 Thread Bill Mill
Srinivas,

You can't sort a string, since it's immutable. You can, however, sort
a list. To sort your table by the third element, you can do something
like this:

 table = ((apple, fruit, denmark),
... (mummy, antique, egypt),
... (taj, wonder, india),
... (f-16, fighter, usa))
 sorter = [(elt[2], elt) for elt in table]
 sorter.sort()
 tuple([elt[1] for elt in sorter])
(('apple', 'fruit', 'denmark'), 
('mummy', 'antique', 'egypt'), 
('taj', 'wonder', 'india'), 
('f-16', 'fighter', 'usa'))  

# I edited the table output edited for clarity

When you sort a list of tuples, the default is to sort the list by the
first element in the tuples. If you make a list where the element you
want to sort on is first in all of the tuples (see the 'sorter = ...'
line), then sort that list, then remove the element you added (the
'tuple([...])' line), you are left with a list which is ordered the
way you want it.

Peace
Bill Mill
bill.mill at gmail.com


On Thu, 27 Jan 2005 08:29:10 -0800 (PST), Srinivas Iyyer
[EMAIL PROTECTED] wrote:
 Dear Danny, thank you for ur help. But a basic
 question ?
 
 In a table, more specifically a matrix 3X3,
 
 AppleFruitDenmark
 F-16 Fighter  USA
 Taj  Wonder   India
 MummyAntique  Egypt
 
 IF I have to sort on country, it should be
 
 AppleFruitDenmark
 MummyAntique  Egypt
 Taj  Wonder   India
 F-16 Fighter  USA
 
 How can I sort by binding Denmark to fruit and apple
 as a string. String does not support sort function.
 Sincerly I dont know how to sort to follow your
 suggestion. can you please help.. sorry for asking
 basic question.
 
 thank you
 
 
 --- Danny Yoo [EMAIL PROTECTED] wrote:
 
 
 
  On Wed, 26 Jan 2005, Srinivas Iyyer wrote:
 
   I have a list with 4 columns and column1 elements
  are unique.  I wanted
   to extract unique elements in column3 and and
  place the other elements
   of the column along with unique elements in column
  4 as a tab delim
   text.
  
   Table:
  
   col1col2col3   col4
   A   Apple 5Chennai
   B   Baby 11Delhi
   I   Baby* 1Delhi
   M   Dasheri+  5Mumbai
   K   Apple 12   Copenhagen
 
 
  [Meta: we seem to be getting a run of similar
  questions this week. Scott
  Melnyk also asked about grouping similar records
  together:
 
 http://mail.python.org/pipermail/tutor/2005-January/035185.html.]
 
 
  Hi Srinivas,
 
  I see that you are trying to group records based on
  some criterion.  You
  may find the problem easier to do if you fist do a
  sort on that criterion
  column: that will make related records clump
  together.
 
 
  For your sample data above, if we sort against the
  second column, the
  records will end up in the following order:
 
  ###
  A   Apple 5Chennai
  K   Apple 12   Copenhagen
  B   Baby  11   Delhi
  I   Baby  1Delhi
  M   Dasheri   5Mumbai
  ###
 
 
  In this sorting approach, you can then run through
  the sorted list in
  order.  Since all the related elements should be
  adjacent, grouping
  related lines together should be much easier, and
  you should be able to
  produce the final output:
 
  ###
  Apple A,K 5,12Chennai,Copenhagen
  Baby  B,I 1,11Delhi
  Dasheri   M   5   Mumbai
  ###
 
  without too much trouble.  You can do this problem
  without dictionaries at
  all, although you may find the dictionary approach a
  little easier to
  implement.
 
 
 
 
   A dictionary option does not work
 
  A dictionary approach is also very possible.  The
  thing you may be stuck
  on is trying to make a key associate with multiple
  values.  Most examples
  of dictionaries in tutorials use strings as both the
  keys and values, but
  dictionaries are more versatile: we can also make a
  dictionary whose
  values are lists.
 
 
  For example, here is a small program that groups
  words by their first
  letters:
 
  ###
   def groupAlpha(words):
  ... groups = {}
  ... for w in words:
  ... firstLetter = w[0]
  ... if firstLetter not in groups:
  ... groups[firstLetter] = []
  ... groups[firstLetter].append(w)
  ... return groups
  ...
   groupAlpha(this is a test of the emergency
  broadcast system.split())
  {'a': ['a'],
   'b': ['broadcast'],
   'e': ['emergency'],
   'i': ['is'],
   'o': ['of'],
   's': ['system'],
   't': ['this', 'test', 'the']}
  ###
 
 
  If you have more questions, please feel free to ask.
 
 
 
 __ 
 Do you Yahoo!?
 Meet the all-new My Yahoo! - Try it today!
 http://my.yahoo.com
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique Items in Lists

2005-01-26 Thread Chad Crabtree
Ok.  I think I understand and I happen to be up at 1:30 my time so
here 
is the solution as I understand the problem.  This is a very common 
problem and has  a fairly easy solution.  You can then take
adict.keys() 
which returns a list of unique elements.  Good Luck

import random
l=[random.randrange(1,20) for x in range(100)]
l
[7, 18, 17, 17, 6, 11, 14, 9, 4, 16, 2, 9, 3, 13, 4, 2, 5, 15, 15, 3,
3, 
11, 18, 12, 6, 8, 15, 3, 7, 9, 9, 7, 12, 11, 11, 9, 19, 19, 15, 2,
17, 
18, 16, 8, 15, 3, 19, 19, 19, 1, 3, 17, 3, 8, 16, 1, 5, 19, 17, 16,
19, 
6, 3, 8, 16, 11, 12, 7, 10, 13, 13, 11, 6, 2, 18, 15, 17, 8, 12, 13,
5, 
12, 2, 19, 2, 19, 7, 10, 4, 14, 15, 14, 5, 1, 16, 1, 9, 10, 17, 12]
adict={}
for x in l:
if adict.has_key(x):
#then there is already an item
adict[x]+=1 #increment the count by one
else: #there is no key the item hasn't been seen
adict[x]=1 #there is one instance so far
   
adict
{1: 4, 2: 6, 3: 8, 4: 3, 5: 4, 6: 4, 7: 5, 8: 5, 9: 6, 10: 3, 11: 6,
12: 
6, 13: 4, 14: 3, 15: 7, 16: 6, 17: 7, 18: 4, 19: 9}

Srinivas Iyyer wrote:

Dear Jacob, thank you for your suggestion.

however, i think my question was not clear. what i
meant to ask in my previous question was, how to know
which elements repeated and how many times they were
repeated. 

while my question was flying, i did a small test:

took a list:
  

a


[1, 1, 2, 3, 4, 2, 2]

wanted to know which elements repeated and how many
times:




__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unique Items in Lists

2005-01-26 Thread Brian van den Broek
Srinivas Iyyer said unto the world upon 2005-01-27 01:17:
Dear Jacob, thank you for your suggestion.
however, i think my question was not clear. what i
meant to ask in my previous question was, how to know
which elements repeated and how many times they were
repeated. 

while my question was flying, i did a small test:
took a list:
a
[1, 1, 2, 3, 4, 2, 2]
wanted to know which elements repeated and how many
times:
for i in range(len(a)):
for k in range(len(a)):
if i != k:
if a[i] == a[k]:
print a[i]
break
SNIP
In this very huge list (:-) kidding) of 7 elements, it
is easy know by the above primitive code that there 1
s repeated two times, and 2 s repeated three times and
finally 1 and 2 numbers are repeated in list a:
With sets option i get a list of unique elements, but
by no means i get to know which elements were repeated
and how many times.  With the above primitive code , I
know that 1 and 2 are peated.  However, in case where
there are thousands of entries, how can I get to kno
wich elements got repeated and how many times.  

Do you see any flaws in this code, how can that be
made to look more pyhonian way. 

Hope my question is clear now and appreciate your
suggestions.
Thank you in advance
Srini
SNIP
Hi Srini,
for the task of finding out which items are repeated and how many 
times, I'd do this:

code
def dups_in_list_report(a_list):
'''Prints a duplication report for a list.'''
items_dict = {}
for i in a_list:
if i in items_dict:
items_dict[i] = items_dict[i] + 1
else:
items_dict[i] = 1
for key in items_dict.copy():   # Try it without the .copy()
if items_dict[key] == 1:# and see what happens.
del items_dict[key]
dict_keys = items_dict.keys()
dict_keys.sort()
for key in dict_keys:
print '%s occurred %s times' %(key, items_dict[key])
f = [1,1,2,3,3,3,3,4,4,4,4,4,4,4,5]
dups_in_list_report(f)
/code
And, now that I get back on-line, I see that Chad posted the same 
basic idea. But, perhaps the extra stuff here is of use, too.

HTH,
Brian vdB
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor