[英]How to pass an integer array as argument in an item getter function?
I want to pass an integer array into an itemgetter function. 我想将整数数组传递给itemgetter函数。 How can I do this?
我怎样才能做到这一点?
Below is my code: 下面是我的代码:
// Start of code
start ="1,3,5"
mylist = start.split(',')
key = itemgetter(*start)
or
key = itemgetter(mylist)
print key
You have almost the right idea. 您几乎有正确的想法。 If you're trying to index an iterable, eg a list, then the items should be integers instead of strings:
如果要索引一个可迭代对象(例如列表),则这些项目应为整数而不是字符串:
>>> start = "1,3,5"
>>> mylist = [int(x) for x in start.split(',')]
>>> g = itemgetter(*mylist)
>>> g(range(10))
(1, 3, 5)
>>> g('abcdefghi')
('b', 'd', 'f')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.