1

I have following string and I want to convert it to array/list so I can measure its length.

a="abc,cde,ert,ert,eee"
b="a", "b", "c"

The expected length for a should be 1 and the expected length for b should be 3.

4 Answers4

3

a is a string, b is a tuple. You can try something like this:

def length_of_str_or_tuple(obj):
    if(isinstance(obj,basestring)):
        return 1
    return len(obj)

Although what you're doing is really weird and you should probably rethink your approach.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

You can use something like this:

>>> a="abc,cde,ert,ert,eee"
>>> b="a", "b", "c"
>>> 1 if isinstance(a, str) else len(a)
1
>>> 1 if isinstance(b, str) else len(b)
3
>>>

In the above code, the conditional expression uses isinstance to test whether or not item is a string object. It returns 1 if so and len(item) if not.

Note that in Python 2.x, you should use isinstance(item, basestring) in order to handle both unicode and str objects.

Community
  • 1
  • 1
  • is there a way to measure length without using a function since I already have this strings stored inside function. – user2231154 Jul 10 '14 at 00:45
  • Sure, see my edit. I originally put the code in a function just for convenience. –  Jul 10 '14 at 00:46
  • `>>> a="abc,cde,ert,ert,eee" >>> 2 if isinstance(a, str) else len(a) 2` I believe, I am still not seeing correct solution to measure length for string. For tuple, I can do that with str.split(','). – user2231154 Jul 10 '14 at 06:43
  • Why did you replace `1` with `2`? You should keep that as is and only change the name of the variable. The code `1 if isinstance(a, str) else len(a)` is telling Python to return `1` if `a` is a string or `len(a)` if not. –  Jul 10 '14 at 14:31
0

There's a crude way to do this: check which is a string and which a tuple:

x ={}

for item in (a,b):
    try:
        item.find('')
        x[item] = 1
    except:
        x[item] = len(item)

Since a tuple object doesn't have an attribute find, it will raise an exception.

user2963623
  • 2,267
  • 1
  • 14
  • 25
-3

To measure the length of the string:

len(a.split())

for the tuple:

len(list(b))

combine the previous answers to test for tuple or list and you would get what you want, or use:

if type(x) is tuple:    
    len(list(x))    
else:    
    a = x.split("\"")    
    len(a)
rch
  • 101
  • 3
  • What are you trying to accomplish by splitting the string on double quotes? You realize that the quotes used to write a string literal aren't actually in the string, right? – jwodder Jul 10 '14 at 01:27
  • Explanation: So, if you try to split string, and strings are immutable, what is returned is a list of the form: ['abc,cde,ert,ert,eee'], using the len() method you get the length of the list. – rch Jul 10 '14 at 01:42
  • string: "4529,4798,12902788,12020850,12905379,12197558,12904395," doing string.split("\") ['', '4529,4798,12902788,12020850,12905379,12197558,12904395,', ''] len(string.split("\"")) 3 in my case its counting ' " ' as an element. – user2231154 Jul 10 '14 at 06:39
  • This is the result that you describe above from a shell: >>> a = "4529,4798,12902788,12020850,12905379,12197558,12904395," >>> a '4529,4798,12902788,12020850,12905379,12197558,12904395,' >>> len(a.split("\"")) 1 >>> Make sure the dividing character is a "quote". So you have to express it with the escape character "\"". – rch Jul 10 '14 at 10:52
  • 2
    @Unihedron It works because his test string doesn't contain a double quote in it. Throw one in and it breaks: http://ideone.com/Iqx022 – mpen Jul 10 '14 at 15:35
  • You are both right aobut my original answer breaking if there is a literal quote. However, if there is no splitting character, then it should be ok. I Just edited the answer. – rch Jul 11 '14 at 16:53