-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.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
colbyjackson
  • 175
  • 2
  • 10

5 Answers5

2

Try this:

lst = [('MK320',), ('MK180',), ('FM340',), ('SH230',), ('LL2',), ('HHH22',), ('TGF22',)]
lst = [t[0] for t in lst]
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
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})
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • I am converting string to set of tuples then doing the same thing over again because the question asks me to do so. – colbyjackson Sep 24 '17 at 21:42
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)
totallyhuman
  • 155
  • 1
  • 8
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.

J_H
  • 17,926
  • 4
  • 24
  • 44
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.

ABirnberg
  • 119
  • 11