23 jan 2006 ta (Steven D'Aprano) shuo le:
This is the type of solution I was hoping to see: one-liners, with no
use of local variables.
Because you like unreadable, incomprehensible, unmaintainable code?
For practical use: no! But I'm just learning python and to understand
sets/lists/dicts
On 23/01/2006 18:41, Mathijs uttered:
len([ref.pop(ref.index(x)) for x in lis if x in ref])
This is the type of solution I was hoping to see: one-liners, with no
use of local variables. As Tim Chase already wrote, it has only one
less elegant side: it alters the original ref list.
Thanks
[Mathijs]
Example2:
ref=[2, 2, 4, 1, 1]
list=[2, 2, 5, 2, 4]
solution: 3 (note that only the first two 2's count, the third 2 in the
list should not be counted)
[Morten Vold]
May I suggest another one-liner:
len(set(ref).intersection(lis))
I have no idea how this scales/performs
On 24/01/2006 09:46, Peter Otten uttered:
len(set([2, 2, 4, 1, 1]).intersection([2, 2, 5, 2, 4]))
2
Close, but no cigar.
I need more coffee! (note to self: Always read entire problem set first)
-- Morten
--
http://mail.python.org/mailman/listinfo/python-list
Umm,
My answer is not correct, but for a different reason; it seems you need
the length of my
previous answer, thus:
PythonWin 2.4 (#60, Feb 9 2005, 19:03:27) [MSC v.1310 32 bit (Intel)]
on win32.
Portions Copyright 1994-2004 Mark Hammond ([EMAIL PROTECTED]) -
see 'Help/About PythonWin' for
Op 19 jan 2006 vond [EMAIL PROTECTED] :
another approach:
ref = [2,2,4,1,1]
lis = [2,2,5,2,4]
len([ref.pop(ref.index(x)) for x in lis if x in ref])
This is the type of solution I was hoping to see: one-liners, with no use
of local variables. As Tim Chase already wrote, it has only one
Op 19 jan 2006 vond Peter Otten [EMAIL PROTECTED] :
sum(min(list.count(n), ref.count(n)) for n in set(ref))
Is that it?
Seems like this is it! Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Op 19 jan 2006 vond Paddy [EMAIL PROTECTED]:
answer = [ val for val in set(ref) for x in
range(min(lst.count(val), ref.count(val)))] answer
[2, 2, 4]
I don't think it's correct. Your algoritm with the ref and lst below gives
3 as answer. The answer should have been 2 (1,3).
ref=[3, 3, 1, 1,
Op 20 jan 2006 vond Duncan Booth [EMAIL PROTECTED]:
Or in other words, define a function to return a dictionary containing
a count of the number of occurrences of each element in the list (this
assumes that the list elements are hashable). Then you just add up the
values in the test list
Mathijs wrote:
Op 20 jan 2006 vond Duncan Booth [EMAIL PROTECTED]:
Or in other words, define a function to return a dictionary
containing a count of the number of occurrences of each element in
the list (this assumes that the list elements are hashable). Then you
just add up the values in
Mathijs wrote:
Op 20 jan 2006 vond Duncan Booth [EMAIL PROTECTED]:
Or in other words, define a function to return a dictionary containing
a count of the number of occurrences of each element in the list (this
assumes that the list elements are hashable). Then you just add up the
values in
Duncan Booth showed how to solve a problem posed by Mathijs. This is
very similar to Duncan's solution, except I (ab)use setdefault on a
regular basis...
def occurrences(t):
... res = {}
... for item in t:
... res.setdefault(item,[0])[0] += 1
... return res
...
ref =
Bryan Olson wrote:
Duncan Booth wrote:
Here's the way I would do it:
def occurrences(it):
res = {}
for item in it:
if item in res:
res[item] += 1
else:
res[item] = 1
return res
I slightly prefer:
def occurrences(it):
Mathijs wrote:
Python beginner here and very much enjoying it. I'm looking for a
pythonic way to find how many listmembers are also present in a
reference list. Don't count duplicates (eg. if you already found a
matching member in the ref list, you can't use the ref member
anymore).
Duncan Booth wrote:
Here's the way I would do it:
def occurrences(it):
res = {}
for item in it:
if item in res:
res[item] += 1
else:
res[item] = 1
return res
I slightly prefer:
def occurrences(it):
res = {}
res[item] =
Hi,
Python beginner here and very much enjoying it. I'm looking for a
pythonic way to find how many listmembers are also present in a reference
list. Don't count duplicates (eg. if you already found a matching member
in the ref list, you can't use the ref member anymore).
Example1:
ref=[2,
Python beginner here and very much enjoying it. I'm looking
for a pythonic way to find how many listmembers are also
present in a reference list. Don't count duplicates (eg. if
you already found a matching member in the ref list, you can't
use the ref member anymore).
Example1:
def reference(alist1,alist2):
counter = 0
for x in lis:
if x in ref:
ref.pop(ref.index(x))
counter += 1
return counter
this works I think for your examples, but you should check it against
them and other
revision of previous:
def reference(reflist,alist2):
counter = 0
for x in alist2:
if x in reflist:
reflist.pop(reflist.index(x))
counter += 1
return counter
--
another approach:
ref = [2,2,4,1,1]
lis = [2,2,5,2,4]
len([ref.pop(ref.index(x)) for x in lis if x in ref])
--
http://mail.python.org/mailman/listinfo/python-list
Tim Chase wrote:
Python beginner here and very much enjoying it. I'm looking
for a pythonic way to find how many listmembers are also
present in a reference list. Don't count duplicates (eg. if
you already found a matching member in the ref list, you can't
use the ref member
Mathijs wrote:
Python beginner here and very much enjoying it. I'm looking for a
pythonic way to find how many listmembers are also present in a reference
list. Don't count duplicates (eg. if you already found a matching member
in the ref list, you can't use the ref member anymore).
Tim Chase wrote:
I'm a tad confused by the help, as it sounds like sets are
supposed to be first-class citizens, but in ver2.3.5 that I'm
running here (or rather there, on a friend's box), I have to
import sets which I didn't see mentioned in the reference manual.
set and frozenset are a
Python beginner here and very much enjoying it. I'm looking
for a pythonic way to find how many listmembers are also
present in a reference list. Don't count duplicates (eg. if
[snipped]
won't set remove duplicates which he wants to preserve ?
My reading was that the solution shouldn't
Tim Chase wrote:
snip
I'm a tad confused by the help, as it sounds like sets are
supposed to be first-class citizens, but in ver2.3.5 that I'm
running here (or rather there, on a friend's box), I have to
import sets which I didn't see mentioned in the reference manual.
-one of many tims on
Hi,
I liked the twist at the end when you state that only the first two 2's
count. It reminded me
of my maths O'level revision where you always had to read the question
thoroughly.
Here is what I came up with:
ref
[2, 2, 4, 1, 1]
lst
[2, 2, 5, 2, 4]
tmp = [ [val]*min(lst.count(val),
26 matches
Mail list logo