简体   繁体   English

如何在项目getter函数中将整数数组作为参数传递?

[英]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.

相关问题 如何将整数系列作为参数传递给python函数? - How to pass an integer series as argument to a python function? 如何将 function 作为参数传递 - How to pass a function as an argument Python:如何将多个参数传递给属性 getter? - Python: How to pass more than one argument to the property getter? 通过odeint函数求解ODE时如何传递数组作为参数(在Python中) - How to pass an array as argument (in Python) when solving ODE by odeint function 如何将默认的numpy数组参数传递给pybind11中的函数? - How to pass a default numpy array argument to a function in pybind11? 如何将 meshgrid 作为参数传递给 function,它只允许 python 中的数组 - How to pass meshgrid as an argument to a function which only allows an array in python Perl + Python - How to pass array from perl as an argument to a python function - Perl + Python - How to pass array from perl as an argument to a python function 如何将整个 numpy 数组作为 python 中的参数传递给用户定义的 function? - How to pass whole numpy array as argument in python to a user defined function? 如何在ctypes中生成字符数组并将其指针作为函数参数传递 - How to produce a character array in ctypes and pass its pointer as a function argument 如何将参数传递给信号中的函数? - How to pass an argument to function in a signal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM