10

In my python Script I have:

user = nuke.getInput("Frames Turned On")
userLst = [user]
print userLst

Result:

['12,33,223']

I was wondering How I would remove the ' in the list, or somehow convert it into int?

2
  • 1
    You want to turn three numbers separated by commas into an int? How should that work? Commented Jun 16, 2011 at 21:32
  • @Ignacio: To explain what Nick probably means: If you "remove the '" in the list ['12,33,223'], you get [12,33,223] -- at least this is how I read it... Commented Jun 16, 2011 at 21:40

6 Answers 6

20

Use split() to split at the commas, use int() to convert to integer:

user_lst = map(int, user.split(","))
Sign up to request clarification or add additional context in comments.

Comments

9

There's no ' to remove in the list. When you print a list, since it has no direct string representation, Python shows you its repr—a string that shows its structure. You have a list with one item, the string 12,33,223; that's what [user] does.

You probably want to split the string by commas, like so:

user_list = user_input.split(',')

If you want those to be ints, you can use a list comprehension:

user_list = [int(number) for number in user_input.split(',')]

Comments

1
[int(s) for s in user.split(",")]

I have no idea why you've defined the separate userLst variable, which is a one-element list.

Comments

1
>>> ast.literal_eval('12,33,223')
(12, 33, 223)

4 Comments

Not so good if the user is allowed to enter a single number.
Sure, but that's easy enough to catch.
why not just eval('12,33,223', {}) ? That will only allow the same things as literal_eval do. BTW OP may not want things other than numbers
And things that are not numbers are easy enough to catch.
-1
>>> result = ['12,33,223']
>>> int(result[0].replace(",", ""))
1233233
>>> [int(i) for i in result[0].split(',')]
[12, 33, 233]

Comments

-2

You could use the join method and convert that to an integer:

int(''.join(userLst))    

1233223

3 Comments

This doesn't really answer the question, or address the actual problem.
@Eevee - How does this not convert a string list into an int?
It's a single string in a list, not a list of strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.