Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—有没有一种方法可以使用枚举来获取(str,index)而不是(index,str)?_Python_Enumerate - Fatal编程技术网

Python—有没有一种方法可以使用枚举来获取(str,index)而不是(index,str)?

Python—有没有一种方法可以使用枚举来获取(str,index)而不是(index,str)?,python,enumerate,Python,Enumerate,比如说, test = ["test1", "test2", "test3"] print ([(i, test) for i, test in enumerate(test)]) >> [(0, 'test1'), (1, 'test2'), (2, 'test3')] 有没有办法代替获取[('test',0),('test2',1),('test3',2)]?使用: test = ["test", "test2", "test3"] print ([(test1, i) for

比如说,

test = ["test1", "test2", "test3"]
print ([(i, test) for i, test in enumerate(test)])
>> [(0, 'test1'), (1, 'test2'), (2, 'test3')]
有没有办法代替获取[('test',0),('test2',1),('test3',2)]?

使用:

test = ["test", "test2", "test3"]
print ([(test1, i) for i, test1 in enumerate(test)])
我确实修正了你开头代码中的一个小错误。我将
I,test
更改为
I,test1

我将
(I,test1)
切换到
(test1,I)

使用:

test = ["test", "test2", "test3"]
print ([(test1, i) for i, test1 in enumerate(test)])
我确实修正了你开头代码中的一个小错误。我将
I,test
更改为
I,test1


我将
(I,test1)
切换到
(test1,I)

除了明显的genexpr/listcomp包装外:

# Lazy
((x, i) for i, x in enumerate(test))

# Eager
[(x, i) for i, x in enumerate(test)]
您可以使用
map
future\u builtins.map
在Py2上)和
operator.itemgetter
在C层进行反转,以提高速度:

from future_builtins import map  # Only on Py2, to get Py3-like generator based map

from operator import itemgetter

# Lazy, wrap in list() if you actually need a list, not just iterating results
map(itemgetter(slice(None, None, -1)), enumerate(test))

# More succinct, equivalent in this case, but not general reversal
map(itemgetter(1, 0), enumerate(test))

除了明显的genexpr/listcomp包装外:

# Lazy
((x, i) for i, x in enumerate(test))

# Eager
[(x, i) for i, x in enumerate(test)]
您可以使用
map
future\u builtins.map
在Py2上)和
operator.itemgetter
在C层进行反转,以提高速度:

from future_builtins import map  # Only on Py2, to get Py3-like generator based map

from operator import itemgetter

# Lazy, wrap in list() if you actually need a list, not just iterating results
map(itemgetter(slice(None, None, -1)), enumerate(test))

# More succinct, equivalent in this case, but not general reversal
map(itemgetter(1, 0), enumerate(test))

您可以简单地切换列表理解语句中的变量

test = ["test", "test2", "test3"]
print ([(test,i) for (i,test) in enumerate(test)])
结果:

[('test', 0), ('test2', 1), ('test3', 2)]

您可以简单地切换列表理解语句中的变量

test = ["test", "test2", "test3"]
print ([(test,i) for (i,test) in enumerate(test)])
结果:

[('test', 0), ('test2', 1), ('test3', 2)]

i
切换
test
<代码>[(t,i)用于枚举(测试)中的i,t]切换
测试
i
<代码>[(t,i)for i,t in enumerate(test)]
如果要解包和重新打包,只需按相反顺序重新打包并跳过切片…哦,是的。。。这会更有意义。更新。如果你正在解包和重新打包,只需按相反顺序重新打包并跳过切片…哦,是的。。。这会更有意义。更新。