0

If a have a list like this:

[['welcome','a1'],['welcome','a1'],['hello','a2'],['hello','a3']]

and I want to return something like this:

[['welcome','a1', 2],['hello','a2', 1],['hello','a3', 1]]

If the same pair of strings in a sublist is encountered, increment the count

What I have so far:

counter = 0
for i in mylist:
  counter += 1 
  if i[0]== i[0]:
    if i[1] == i[1]:
        counter -= 1
 ouptut.append([mylist, counter])

I'm new at this and I appreciate your help!

4
  • 1
    It sounds like a dictionary problem but the question is kinda poorly stated... Commented Aug 10, 2013 at 1:05
  • 1
    How did 'hello' became 'hi'? Commented Aug 10, 2013 at 1:10
  • Shouldn't the output of the example be [['welcome', 'a1', 2], ['hello', 'a3', 1], ['hello', 'a2', 1]]? Commented Aug 10, 2013 at 1:11
  • sorry I edited the question, I need to avoid the repetitions by comparing the 1st elem of each sublist and if it is alike compare the 2nd element (code) to chk the difference, if so count them separately otherwise just count it once. Commented Aug 10, 2013 at 1:19

1 Answer 1

1

Use a set here to get only unique items:

>>> lis = [['welcome','a1'],['welcome','a1'],['hello','a2'],['hello','a3']] 
>>> [list(x) + [1] for x in set(map(tuple, lis))]
>>> [['welcome', 'a1', 1], ['hello', 'a3', 1], ['hello', 'a2', 1]]

Explanation:

Set always returns unique items from an iterable or iterator, but as sets can only contain immutable item so you should convert them to a tuple first. A verbose version of the above code, only difference is that will also preserve the original or

>>> lis = [['welcome','a1'],['welcome','a1'],['hello','a2'],['hello','a3']] 
>>> s = set()
>>> for item in lis:
...     tup = tuple(item)  #covert to tuple
...     s.add(tup)
>>> s
set([('welcome', 'a1'), ('hello', 'a3'), ('hello', 'a2')])

Now use a list comprehension to get the expected output:

>>> [list(item) + [1] for item in s]
[['welcome', 'a1', 1], ['hello', 'a3', 1], ['hello', 'a2', 1]]

If the order of items matter(sets don't preserve order), then use this:

>>> seen = set()
>>> ans = []
>>> for item in lis:
...     tup = tuple(item)
...     if tup not in seen:
...         ans.append(item + [1])
...         seen.add(tup)
...         
>>> ans
[['welcome', 'a1', 1], ['hello', 'a2', 1], ['hello', 'a3', 1]]

I am not sure what's the point of using 1 here.

Sign up to request clarification or add additional context in comments.

8 Comments

I was just about to edit my answer to include this exact thing, +1
It seems that it work but I don't understand fully your code to do it... but thanks
How you can edit the loop so as to get [['hello', 'a1',1], ['hello', 'a1',1]]; return ['hello', 'a1',2],... This is just to understand the logic better. Thank you for your replies! :)
@JPP Have a look at collections.Counter
Should I change if tup not in seen: to => if tup in seen: ??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.