Re: [Tutor] Help on best way to check resence of item inside list

2014-05-27 Thread Peter Otten
jarod...@libero.it wrote:

> Dear All
> 
> clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra","
> electra"]
> clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia",
> "sally"," Castiel","Sam"]
> 
> I have a list of names that I would to annotate  in function of presence
> in different clubs:
> 
> my input files is a long file where I have this :
> 
> mary
> luke
> luigi
> jane
> jessica
> rebecca
> luis
> à
> 
> with open("file.in") as p:
> mit = []
> for i in p:
>lines =i.strip("\n").split("\t")
>if  (lines[0] in clubA:
>   G =lines[-1] +["clubA"]
>else:
>G = lines[-1] +["no"]
> mit.append(G)
>   
> 
> for i in mit:
>if i.strip("\n").split("\t")[0] in clubB:
>  G =lines[-1] +["clubB"]
>else:
>G = lines[-1] +["no"]
>   finale.append(G)
> ###
> I just wonder if is appropriate to use a loops to check if is present the
> value on a list. Is it the right way? I can use a dictionary because I
> have many repeated names.

You mean you have people who are members in more than one club? You can 
still use a dictionary if you make the value a list:

# untested code!

membership = {}

club = "club_of_people_who_are_not_in_any_club"
members = ["Erwin", "Kurt", "Groucho"]

for name in members:
# can be simplified with dict.setdefault() or collections.defaultdict
if name in membership:
membership[name].append(club)
else:
membership[name] = [club]

Put that in a loop over (club, members) pairs, and you'll end up with a dict 
that maps name --> list_of_clubs.

Then iterate over the lines in the file:

with open("file.in") as source:
for line in source:
name = line.strip()
if name in membership:
print(name, *membership[name])
else:
print(name, "no")


> 
> In the end I wan to have
> 
> 
> mary  clubA clubB
> luke clubA
> luigi  no
> Thanks in advance for any help
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on best way to check resence of item inside list

2014-05-27 Thread Steven D'Aprano
On Tue, May 27, 2014 at 10:05:30AM +0200, jarod...@libero.it wrote:
[...]
> with open("file.in") as p:
> mit = []

You have lost the indentation, which makes this code incorrect.

But the rest of the code is too complicated.

> for i in p:
>lines =i.strip("\n").split("\t")
>if  (lines[0] in clubA:
>   G =lines[-1] +["clubA"]
>else:
>G = lines[-1] +["no"]  
> mit.append(G)
> 
> for i in mit:
>if i.strip("\n").split("\t")[0] in clubB:
>  G =lines[-1] +["clubB"]
>else:
>G = lines[-1] +["no"]  
>   finale.append(G)

Look at the result you want to get:

> mary  clubA clubB
> luke clubA
> luigi  no

That suggests to me that the best data structure is a dict with sets:

{'mary': set(['clubA', 'clubB']),
 'luke': set(['clubA']),
 'luigi': set(),
 }


Something like this should work:

names = {}
with open("file.in") as p:
# This assumes the data file is one name per line.
for line in p:
name = line.strip()
s = names.get(name, set())  # If name not in the names, 
# return an empty set.
if name in clubA:
s.add("clubA")
if name in clubB:
s.add("clubB")
names[name] = s

print(names)


And I think that should work.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on best way to check resence of item inside list

2014-05-27 Thread Dave Angel
"jarod...@libero.it"  Wrote in message:
> Dear All
> 
> clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra","
> electra"]
> clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally","
> Castiel","Sam"]
> 
> I have a list of names that I would to annotate  in function of presence in
> different clubs:
> 
> my input files is a long file where I have this :
> 
> mary
> luke
> luigi
> jane
> jessica
> rebecca
> luis
> à
> 
> with open("file.in") as p:
> mit = []
> for i in p:
>lines =i.strip("\n").split("\t")
>if  (lines[0] in clubA:
>   G =lines[-1] +["clubA"]
>else:
>G = lines[-1] +["no"]  
> mit.append(G)
>   
> 
> for i in mit:
>if i.strip("\n").split("\t")[0] in clubB:
>  G =lines[-1] +["clubB"]
>else:
>G = lines[-1] +["no"]  
>   finale.append(G)
> ###
> I just wonder if is appropriate to use a loops to check if is present the
> value on a list. Is it the right way? I can use a dictionary because I have
> many repeated names.
> 
> In the end I wan to have
> 
> 
> mary  clubA clubB
> luke clubA
> luigi  no
> Thanks in advance for any help

There are numerous errors in the above code. You should use
 copy/paste, so we don't waste energy identifying errors that
 don't even exist in your actual code. As it stands,  it wouldn't
 even compile. 

But even if you fix the typos and indentation errors and
 initialization errors,  you still have logic errors if you want
 the output you specify. First,  the second loop doesn’t set the
 lines variable at all, but just uses the value from the last
 iteration of the first loop. Second,  even if you untangle that, 
 luigi would end up with two 'no's,  not one.

You don't say what list could have repeats. I don't see any in
 your sample data. You also don't say how they should be treated. 
 For example,  Are all seventeen  mary's in clubA?
 

Now to your specific question.  You aren't using the loops to
 check the lists for a name.  You're quite reasonably using
 'in'.

You can accomplish your apparent goal much more reasonably by
 using a single loop and more complex if elif and
 else.




-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help on best way to check resence of item inside list

2014-05-27 Thread Mark Lawrence

On 27/05/2014 09:05, jarod...@libero.it wrote:

Dear All

clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra","
electra"]
clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally","
Castiel","Sam"]

I have a list of names that I would to annotate  in function of presence in
different clubs:

my input files is a long file where I have this :

mary
luke
luigi
jane
jessica
rebecca
luis
à

with open("file.in") as p:
mit = []
for i in p:
lines =i.strip("\n").split("\t")
if  (lines[0] in clubA:
   G =lines[-1] +["clubA"]
else:
G = lines[-1] +["no"]
mit.append(G)


for i in mit:
if i.strip("\n").split("\t")[0] in clubB:
  G =lines[-1] +["clubB"]
else:
G = lines[-1] +["no"]
   finale.append(G)
###
I just wonder if is appropriate to use a loops to check if is present the
value on a list. Is it the right way? I can use a dictionary because I have
many repeated names.

In the end I wan to have


mary  clubA clubB
luke clubA
luigi  no
Thanks in advance for any help


You can use the in keyword to check for an item in a list.  However a 
very quick glance at your code suggests that you could cut out the list 
completely and do the same using the in keyword against your dict. 
Better still I think the defaultdict is what you need here, I'll leave 
you to look it up as I must dash :)



--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help on best way to check resence of item inside list

2014-05-27 Thread jarod...@libero.it
Dear All

clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra","
electra"]
clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally","
Castiel","Sam"]

I have a list of names that I would to annotate  in function of presence in
different clubs:

my input files is a long file where I have this :

mary
luke
luigi
jane
jessica
rebecca
luis
à

with open("file.in") as p:
mit = []
for i in p:
   lines =i.strip("\n").split("\t")
   if  (lines[0] in clubA:
  G =lines[-1] +["clubA"]
   else:
   G = lines[-1] +["no"]  
mit.append(G)
  

for i in mit:
   if i.strip("\n").split("\t")[0] in clubB:
 G =lines[-1] +["clubB"]
   else:
   G = lines[-1] +["no"]  
  finale.append(G)
###
I just wonder if is appropriate to use a loops to check if is present the
value on a list. Is it the right way? I can use a dictionary because I have
many repeated names.

In the end I wan to have


mary  clubA clubB
luke clubA
luigi  no
Thanks in advance for any help
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor