Question

command cmds.polyInfo (fe=True) output:

[u'FACE     64:     48     49     50     51     52     54     64     74     84     94    104 \n']

How to convert it to a list of this kind?

[48, 49, 50, 51, 52, 54, 64, 74, 84, 94, 104]

I suspect that it is necessary to use re.findall

I would be grateful for any help :)

Was it helpful?

Solution 2

no need to use re module, simply use str.split instead:

In [78]: lst=[u'FACE     64:     48     49     50     51     52     54     64     74     84     94    104 \n']

In [79]: [int(i) for i in lst[0].split(':')[1].split()]
Out[79]: [48, 49, 50, 51, 52, 54, 64, 74, 84, 94, 104]

OTHER TIPS

alist=[u'FACE     64:     48     49     50     51     52     54     64     74     84     94    104 \n']

Using map and join:

map(lambda x:int(x), "".join(alist).split(':')[1].split())

Using list comprehension:

[int(x) for x in "".join(alist).split(':')[1].split()]

Output:

[48, 49, 50, 51, 52, 54, 64, 74, 84, 94, 104]

All you need is a little splitting:

face = cmds.polyInfo(fe=True)[0]
[int(v) for v in face.split(':', 1)[-1].split()]

This takes the output of the cmds.polyInfo() method, takes the first element, takes everything after the first : and splits that up on whitespace. The result is a list of strings (['48', '49', ...]), which the list comprehension then turns into a list of integers instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top