Re: Dictionary help

2014-02-18 Thread Tim Chase
On 2014-02-18 10:30, kjaku...@gmail.com wrote:
 So let's say I have a file and it looks like this:
 Title 1: item 
 Title 2: item 
 etc
 
 Is it possible to use a dictionary for something like the input
 above? Because I want to be able to use the input above to delete
 the Title 1 and Title 2 but still show the items (on separate
 lines). Basically I want it to just show: item item 
 etc..
 
 What I've got is 
 dict(item.split(:) for item in cInfo.split( ))
 
 Where cInfo is a function that extracts the first 5 lines of a file

It sounds like all you need is some basic string functions, not a
dictionary:

  for line in file('input.txt'):
title, _, item = line.partition(':')
print(item.strip())

-tkc



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread Dave Angel

Krishna Pacifici wrote:

Hi,
kind of a newbie here, but I have two questions that are probably pretty simple.

1.  I need to get rid of duplicate values that are associated with different 
keys in a dictionary.  For example I have the following code.
s={}
s[0]=[10,2,3]
 s[10]=[22,23,24]
 s[20]=[45,5]
s[30]=[2,4]
s[40]=[6,7,8]

Now I want to be able to loop through the primary keys and get rid of 
duplicates (both in keys and values) so that I would have either a new 
dictionary or the same dictionary but with the following values:

s[0]=[3]
 s[10]=[22,23,24]
 s[20]=[45,5]
s[30]=[2,4]
s[40]=[6,7,8]

It doesn't matter which value gets removed as long as there is only one 
remaining, so in this example it doesn't matter that 2 got removed from s[0] or 
from s[30] as long as there is only one 2 in the dictionary.

2.  I need to be able to loop over the values in the dictionary when there are 
multiple values assigned to each key like above and assign new values to those 
values.  Taking the above example I would want to assign a new value so that 
when you called s[0] it would equal [3,4] say if 4 was the new value.  I think 
this should be as simple as adding a value, but I kept on having difficulty.

Any suggestions would be greatly appreciated.

Thank you very much,
Krishna


  
Sounds like homework.  If it was for an unconstrained project, I'd 
design a different data structure, one that directly enforced the data 
constraints.  So far, I can't imagine a useful reason for this 
particular set of constraints.


Let's break the problems down.

1a)   Do you know how to write a loop that visits all the keys of a 
dictionary?
1b)  Do you know how to safely check if a particular key exists? 
e.g.  if   key in s:
1c)  Do you know how to collect a set of values, so that when a 
duplicate is found, it can be recognized as such?

1d) Do you know how to remove an item from a list?

2a)  Like 1a)
2b) Do you know how to append a value to the end of a list?  Is s[key] a 
list?



DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread Krishna Pacifici
Thanks for the help.

Actually this is part of a much larger project, but I have unfortunately 
pigeon-holed myself into needing to do these things without a whole lot of 
flexibility.

To give a specific example I have the following dictionary where I need to 
remove values that are duplicated with other values and remove values that are 
duplicates of the keys, but still retain it as a dictionary.  Each value is 
itself a class with many attributes that I need to call later on in the 
program, but I cannot have duplicates because it would mess up some estimation 
part of my model.

d =
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23, 
12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}

So I want a new dictionary that would get rid of the duplicate values of 21, 
22, 36 and 20 and give me back a dictionary that looked like this:

new_d=
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12, 
32], 26: [25, 27, 16], 30: [40]}

I understand that a dictionary may not be the best approach, but like I said I 
have sort of pigeon-holed myself by the way that I am simulating my data and 
the estimation model that I am using.  Any suggestions or comments about the 
above problem would be greatly appreciated.

Thanks again,
Krishna



 Dave Angel da...@ieee.org 08/11/09 7:38 AM 
Krishna Pacifici wrote:
 Hi,
 kind of a newbie here, but I have two questions that are probably pretty 
 simple.

 1.  I need to get rid of duplicate values that are associated with different 
 keys in a dictionary.  For example I have the following code.
 s={}
 s[0]=[10,2,3]
  s[10]=[22,23,24]
  s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]

 Now I want to be able to loop through the primary keys and get rid of 
 duplicates (both in keys and values) so that I would have either a new 
 dictionary or the same dictionary but with the following values:

 s[0]=[3]
  s[10]=[22,23,24]
  s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]

 It doesn't matter which value gets removed as long as there is only one 
 remaining, so in this example it doesn't matter that 2 got removed from s[0] 
 or from s[30] as long as there is only one 2 in the dictionary.

 2.  I need to be able to loop over the values in the dictionary when there 
 are multiple values assigned to each key like above and assign new values to 
 those values.  Taking the above example I would want to assign a new value so 
 that when you called s[0] it would equal [3,4] say if 4 was the new value.  I 
 think this should be as simple as adding a value, but I kept on having 
 difficulty.

 Any suggestions would be greatly appreciated.

 Thank you very much,
 Krishna


   
Sounds like homework.  If it was for an unconstrained project, I'd 
design a different data structure, one that directly enforced the data 
constraints.  So far, I can't imagine a useful reason for this 
particular set of constraints.

Let's break the problems down.

1a)   Do you know how to write a loop that visits all the keys of a 
dictionary?
1b)  Do you know how to safely check if a particular key exists? 
e.g.  if   key in s:
1c)  Do you know how to collect a set of values, so that when a 
duplicate is found, it can be recognized as such?
1d) Do you know how to remove an item from a list?

2a)  Like 1a)
2b) Do you know how to append a value to the end of a list?  Is s[key] a 
list?


DaveA


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread MRAB

Krishna Pacifici wrote:

Thanks for the help.

Actually this is part of a much larger project, but I have unfortunately 
pigeon-holed myself into needing to do these things without a whole lot 
of flexibility.


To give a specific example I have the following dictionary where I need 
to remove values that are duplicated with other values and remove values 
that are duplicates of the keys, but still retain it as a dictionary.  
Each value is itself a class with many attributes that I need to call 
later on in the program, but I cannot have duplicates because it would 
mess up some estimation part of my model.


d =
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: 
[21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}


So I want a new dictionary that would get rid of the duplicate values of 
21, 22, 36 and 20 and give me back a dictionary that looked like this:


new_d=
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 
12, 32], 26: [25, 27, 16], 30: [40]}


I understand that a dictionary may not be the best approach, but like I 
said I have sort of pigeon-holed myself by the way that I am simulating 
my data and the estimation model that I am using.  Any suggestions or 
comments about the above problem would be greatly appreciated.



 d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
 new_d = {}
 seen = set(d.keys())
 for k, v in d.items():
... new_d[k] = [x for x in v if x not in seen]
... seen |= set(new_d[k])
...
 new_d
{36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
32], 26: [25, 27, 16], 30: [40]}

--
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread Simon Forman
On Aug 11, 11:51 am, MRAB pyt...@mrabarnett.plus.com wrote:
 Krishna Pacifici wrote:
  Thanks for the help.

  Actually this is part of a much larger project, but I have unfortunately
  pigeon-holed myself into needing to do these things without a whole lot
  of flexibility.

  To give a specific example I have the following dictionary where I need
  to remove values that are duplicated with other values and remove values
  that are duplicates of the keys, but still retain it as a dictionary.  
  Each value is itself a class with many attributes that I need to call
  later on in the program, but I cannot have duplicates because it would
  mess up some estimation part of my model.

  d =
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
  [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}

  So I want a new dictionary that would get rid of the duplicate values of
  21, 22, 36 and 20 and give me back a dictionary that looked like this:

  new_d=
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
  12, 32], 26: [25, 27, 16], 30: [40]}

  I understand that a dictionary may not be the best approach, but like I
  said I have sort of pigeon-holed myself by the way that I am simulating
  my data and the estimation model that I am using.  Any suggestions or
  comments about the above problem would be greatly appreciated.

   d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
   new_d = {}
   seen = set(d.keys())
   for k, v in d.items():
 ...     new_d[k] = [x for x in v if x not in seen]
 ...     seen |= set(new_d[k])
 ...
   new_d
 {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
 32], 26: [25, 27, 16], 30: [40]}

Ha ha, MRAB beat me to it:

d = {
36: [35, 37, 26, 46],
75: [74, 76, 65, 85],
21: [20, 22, 11, 31],
22: [21, 23, 12, 32],
26: [25, 27, 16, 36],
30: [20, 31, 40],
}


new_d = { # Given, and apparently incorrect.
36: [35, 37, 26, 46], # 26 is a key and should be gone.
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


expected = {
36: [35, 37, 46],
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


def removeDuplicates(D):
'''
Remove values that are duplicated with other values
and remove values that are duplicates of the keys.

Assumes that values in the lists are already unique within
each list.  I.e. duplicates are only in the keys or in other
lists.

This function works in place on D, so it doesn't return
anything.  Caller must keep a reference to D.
'''

seen = set(D) # Get a set of the keys.

for key, values_list in D.iteritems():

# Filter out values that have already been seen.
filtered_values = [
value
for value in values_list
if not value in seen
]

# Remember newly seen values.
seen.update(filtered_values)

D[key] = filtered_values


## Example:
##
## d == expected
##False
## removeDuplicates(d)
## d == expected
##True
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread Krishna Pacifici
Wow, thanks MRAB and Simon, you guys are good.

I guess I will go ahead and ask the next question that has also stumped me for 
awhile now.

So basically I need to loop through the values in the new dictionary and append 
attributes of a class object.  Each of the values (and keys) represent a block 
in a grid with a specific location (e.g. 35 is the block in row 3 col 5) and 
each block is an object with several attributes associated with it.  I want to 
call that block and append two separate attributes to the dictionary within 
that same key.

So again it would look something like this:

block 35 has 2 attributes, say a and b, a=2, b=5 
block 37 has a=1, b=3
block 46 has a=3, b=8

the two attributes come from two different definitions within the class 
statement, 
def detections
...
return a

def abundance
...
return b

so I would want to append to key 36 those two attributes for each block so that 
the resulting dictionary item would look like this:
 a   b   
{36:[35,37,46], [2,1,3], [5,3,8] ...}

Any help with this would be greatly appreciated.  And thank you so much for all 
of your help thus far, I'm still pretty new to python and am enjoying all of 
the flexibility associated with a scripting and programming language.

Thanks again,
Krishna

 Simon Forman sajmik...@gmail.com 08/11/09 12:15 PM 
On Aug 11, 11:51 am, MRAB pyt...@mrabarnett.plus.com wrote:
 Krishna Pacifici wrote:
  Thanks for the help.

  Actually this is part of a much larger project, but I have unfortunately
  pigeon-holed myself into needing to do these things without a whole lot
  of flexibility.

  To give a specific example I have the following dictionary where I need
  to remove values that are duplicated with other values and remove values
  that are duplicates of the keys, but still retain it as a dictionary.  
  Each value is itself a class with many attributes that I need to call
  later on in the program, but I cannot have duplicates because it would
  mess up some estimation part of my model.

  d =
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
  [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}

  So I want a new dictionary that would get rid of the duplicate values of
  21, 22, 36 and 20 and give me back a dictionary that looked like this:

  new_d=
  {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
  12, 32], 26: [25, 27, 16], 30: [40]}

  I understand that a dictionary may not be the best approach, but like I
  said I have sort of pigeon-holed myself by the way that I am simulating
  my data and the estimation model that I am using.  Any suggestions or
  comments about the above problem would be greatly appreciated.

   d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
   new_d = {}
   seen = set(d.keys())
   for k, v in d.items():
 ... new_d[k] = [x for x in v if x not in seen]
 ... seen |= set(new_d[k])
 ...
   new_d
 {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
 32], 26: [25, 27, 16], 30: [40]}

Ha ha, MRAB beat me to it:

d = {
36: [35, 37, 26, 46],
75: [74, 76, 65, 85],
21: [20, 22, 11, 31],
22: [21, 23, 12, 32],
26: [25, 27, 16, 36],
30: [20, 31, 40],
}


new_d = { # Given, and apparently incorrect.
36: [35, 37, 26, 46], # 26 is a key and should be gone.
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


expected = {
36: [35, 37, 46],
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}


def removeDuplicates(D):
'''
Remove values that are duplicated with other values
and remove values that are duplicates of the keys.

Assumes that values in the lists are already unique within
each list.  I.e. duplicates are only in the keys or in other
lists.

This function works in place on D, so it doesn't return
anything.  Caller must keep a reference to D.
'''

seen = set(D) # Get a set of the keys.

for key, values_list in D.iteritems():

# Filter out values that have already been seen.
filtered_values = [
value
for value in values_list
if not value in seen
]

# Remember newly seen values.
seen.update(filtered_values)

D[key] = filtered_values


## Example:
##
## d == expected
##False
## removeDuplicates(d)
## d == expected
##True
-- 
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-11 Thread MRAB

Krishna Pacifici wrote:

Wow, thanks MRAB and Simon, you guys are good.

I guess I will go ahead and ask the next question that has also stumped 
me for awhile now.


So basically I need to loop through the values in the new dictionary and 
append attributes of a class object.  Each of the values (and keys) 
represent a block in a grid with a specific location (e.g. 35 is the 
block in row 3 col 5) and each block is an object with several 
attributes associated with it.  I want to call that block and append two 
separate attributes to the dictionary within that same key.


So again it would look something like this:

block 35 has 2 attributes, say a and b, a=2, b=5
block 37 has a=1, b=3
block 46 has a=3, b=8

the two attributes come from two different definitions within the class 
statement,

def detections
...
return a

def abundance
...
return b

so I would want to append to key 36 those two attributes for each block 
so that the resulting dictionary item would look like this:
 a   b  
{36:[35,37,46], [2,1,3], [5,3,8] ...}



That doesn't look like a dictionary. Perhaps want you want is for the
value to be a list of lists:

{36: [[35,37,46], [2,1,3], [5,3,8]] ...}

although you'd have parallel lists, ie a list of blocks, a list of 'a',
and a list of 'b'. A better format might be to keep a block's attributes
with the block itself.

{36: [[35,2,5], [37,1,3], [46,3,8]] ...}

Any help with this would be greatly appreciated.  And thank you so much 
for all of your help thus far, I'm still pretty new to python and am 
enjoying all of the flexibility associated with a scripting and 
programming language.



[snip]

--
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary help

2009-08-10 Thread J. Cliff Dyer
On Mon, 2009-08-10 at 22:11 -0400, Krishna Pacifici wrote:
 Hi,
 kind of a newbie here, but I have two questions that are probably
 pretty simple.
 
 1.  I need to get rid of duplicate values that are associated with
 different keys in a dictionary.  For example I have the following
 code.
 s={}
 s[0]=[10,2,3]
 s[10]=[22,23,24]
 s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]
 
 Now I want to be able to loop through the primary keys and get rid of
 duplicates (both in keys and values) so that I would have either a new
 dictionary or the same dictionary but with the following values:
 
 s[0]=[3]
 s[10]=[22,23,24]
 s[20]=[45,5]
 s[30]=[2,4]
 s[40]=[6,7,8]
 
 It doesn't matter which value gets removed as long as there is only
 one remaining, so in this example it doesn't matter that 2 got removed
 from s[0] or from s[30] as long as there is only one 2 in the
 dictionary.
 
So if the number is a key, you want to keep the key, and delete all
matching values, and if the number is not a key, you want to keep one
(any one) instance of that number?  I'm not sure that what you are
looking for is best represented by a dict.  You might want to consider
creating your own class and overriding __getitem__.


 2.  I need to be able to loop over the values in the dictionary when
 there are multiple values assigned to each key like above and assign
 new values to those values.  Taking the above example I would want to
 assign a new value so that when you called s[0] it would equal [3,4]
 say if 4 was the new value.  I think this should be as simple as
 adding a value, but I kept on having difficulty.
 
Here you might want to either use the append() method on the lists of
each entry.

 Any suggestions would be greatly appreciated.
 
I'm not sure what you are doing maps cleanly to currently existing
datastructures, which means that there might not be a quick shortcut for
you.  Hammer out the specs of what you want your class to be able to do,
and what the API will be for performing each of those functions.  Then
you should be able to begin implementing it, or at least come up with
some more specific questions.



 Thank you very much,
 Krishna

Cheers,
Cliff


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary help

2007-11-01 Thread wes weston
Steve wrote:
 I'm currently working on a little database type program is which I'm 
 using a dictionary to store the information. The key is a component a and 
 the definition is a list of parts that make up the component. My problem 
 is I need to list out several components, but not all, and there 
 associated parts to a printer. Not having any luck. I can list them to 
 the screen but not the printer. Any help/ideas would be appreciated.
 
 Steve

You can do os.system(some printing command; lp fn.txt, etc.) after
putting your stuf in a file.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary help

2007-11-01 Thread Steve
On Thu, 01 Nov 2007 04:16:10 -0700, wes weston wrote:

 Steve wrote:
 I'm currently working on a little database type program is which I'm
 using a dictionary to store the information. The key is a component a
 and the definition is a list of parts that make up the component. My
 problem is I need to list out several components, but not all, and
 there associated parts to a printer. Not having any luck. I can list
 them to the screen but not the printer. Any help/ideas would be
 appreciated.
 
 Steve
 
 You can do os.system(some printing command; lp fn.txt, etc.) after
 putting your stuf in a file.

Thank you for the reply. I finally got it printing in Linux.

Steve
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary help

2007-10-31 Thread Larry Bates
Steve wrote:
 I'm currently working on a little database type program is which I'm 
 using a dictionary to store the information. The key is a component a and 
 the definition is a list of parts that make up the component. My problem 
 is I need to list out several components, but not all, and there 
 associated parts to a printer. Not having any luck. I can list them to 
 the screen but not the printer. Any help/ideas would be appreciated.
 
 Steve

Windows or Linux or Mac.  Printers are handled differently on different 
platforms.  On Windows you can simply open LPT1, LPT2 or LPT3 as a file and 
write to it (for simple printing).  For more complex Windows printing you 
must 
use Win32 extensions or something like wxWindows.

-Larry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary help

2007-10-31 Thread Steve
On Wed, 31 Oct 2007 17:02:49 -0500, Larry Bates wrote:

 Steve wrote:
 I'm currently working on a little database type program is which I'm
 using a dictionary to store the information. The key is a component a
 and the definition is a list of parts that make up the component. My
 problem is I need to list out several components, but not all, and
 there associated parts to a printer. Not having any luck. I can list
 them to the screen but not the printer. Any help/ideas would be
 appreciated.
 
 Steve
 
 Windows or Linux or Mac.  Printers are handled differently on different
 platforms.  On Windows you can simply open LPT1, LPT2 or LPT3 as a file
 and write to it (for simple printing).  For more complex Windows
 printing you must use Win32 extensions or something like wxWindows.
 
 -Larry

I'm currently using Linux. I give it some more thought and research and 
see what I can come up with. Thank you for the reply.

Steve
-- 
http://mail.python.org/mailman/listinfo/python-list