-1

if

original = 'MK320MK180FM340SH230LL2HHH22TGF22',

Then I have stripped a string list such that looks like this (string --> tuple):

list = {('MK320',), ('MK180',), ('FM340',), ('SH230',), ('LL2',), ('HHH22',), ('TGF22',)}

I want to get rid of the comma inside the parentheses. That is, I want to make these tuples to be string back again. What I did was:

for i in range(len(list)):
''.join(list[i])

But it didn't get any better. Then I wanted to pass this to a different command, which is Counter. However, that actually included Counter{(blabla)}, which I didn't really like. What I wished to get a result as a dictionary, looking like this:

 {('MK320': 1, 'MK180': 1, 'FM340': 1, 'SH230': 1, 'LL2': 1, 'HHH22' : 1, 'TGF22': 1}

Which eventually counts the frequency of each product name.

5
  • A Counter is a dictionary. You access the keys no differently than a regular dict object Commented Sep 24, 2017 at 21:25
  • Please show the code that generated the tuples Commented Sep 24, 2017 at 21:26
  • 1
    And you can create a plain dictionary from a Counter by doing dict(counter) Commented Sep 24, 2017 at 21:26
  • Is it a list or a dict? It appears list is a dict in your code Commented Sep 24, 2017 at 21:29
  • what I am looking for is just to get rid of that ('dd,') form to just ' dd ' Commented Sep 24, 2017 at 21:39

5 Answers 5

2

Try this:

lst = [('MK320',), ('MK180',), ('FM340',), ('SH230',), ('LL2',), ('HHH22',), ('TGF22',)]
lst = [t[0] for t in lst]
Sign up to request clarification or add additional context in comments.

1 Comment

That actually worked, but can you explain more?
1
list = {('MK320',), ('MK180',), ('FM340',), ('SH230',), ('LL2',), ('HHH22',), ('TGF22',)}

That's a set rather than a list.

Please don't use list as a variable name, as there's already a very useful builtin function by that name. I'm going to pretend you named it list1.

It sounds like you want to hoist the elements out of the 1-tuples:

list2 = [elt for elt, in list1]

Then you could join it with ', '.join(list2) as you wish.

1 Comment

Whats elt for elt supposed to mean? Python is still very very new for me.
1

I don't know how you are converting your string to the set of tuple you are having. And, why are you even creating it when you don't need it?

You may use re module to get the desired list using the re.findall(pattern, string):

>>> import re

>>> my_str = 'MK320MK180FM340SH230LL2HHH22TGF22'
>>> re.findall('[A-Za-z]+\d+', my_str)
['MK320', 'MK180', 'FM340', 'SH230', 'LL2', 'HHH22', 'TGF22']

Then, you may use collection.Counter to get the count. For example:

>>> from collections import Counter
>>> word_list = re.findall('[A-Za-z]+\d+', my_str)

>>> Counter(word_list)
Counter({'MK180': 1, 'SH230': 1, 'LL2': 1, 'FM340': 1, 'MK320': 1, 'TGF22': 1, 'HHH22': 1})

1 Comment

I am converting string to set of tuples then doing the same thing over again because the question asks me to do so.
1

Your original variable has a comma after it which makes it a tuple. If you want it to be a string, remove the trailing comma.

original = 'MK320MK180FM340SH230LL2HHH22TGF22'

You haven't shown how you generated the list variable. (Sidenote: don't name variables list or str, it shadows the built-in types.) But you can split this string into separate pieces like so:

import re

original = 'MK320MK180FM340SH230LL2HHH22TGF22'

split_list = re.findall(r'[A-Z]+\d+', original)

Your list variable is actually a set. The syntax for sets is {item1, item2, item3} while the syntax for lists is [item1, item2, item3].

If you want to turn every tuple in this set into its first element:

new_list = [item[0] for item in list]

Finally, if you want to count the instances of each item in this list, you can pass it to collections.Counter.

import re
import collections

original = 'MK320MK180FM340SH230LL2HHH22TGF22'

split_list = re.findall(r'[A-Z]+\d+', original)

count = collections.Counter(split_list)

Comments

0

You may have a typo, or you doing something different from what you've described. Your list is actually a set of tuples. If that's what you meant to create then you can't use indexing as you've shown in your for loop. Given the desired output you're showing, it is also not really clear what you're going for as this is not valid syntax with the parenthesis after the curly brace.

The answer from @Moses-koledoye is the correct one given what you seem to want.

2 Comments

No it isn't exactly. Slicing in fives does not give the exact results they want.
Clearly you're right. Didn't realize they weren't all the same length.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.