Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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中测试列表成员身份并同时获取索引_Python_List_Indexing_Idioms - Fatal编程技术网

在Python中测试列表成员身份并同时获取索引

在Python中测试列表成员身份并同时获取索引,python,list,indexing,idioms,Python,List,Indexing,Idioms,写以下内容似乎很愚蠢: L = [] if x in L: L[x] = something else: L[x] = something_else 这不执行两次x的查找吗?我尝试使用index(),但当找不到该值时会出现错误 理想情况下,我想说: if x is in L, save that index and: ... 我可以理解,这可能是一个初级python习惯用法,但它似乎不适合搜索。谢谢。您的意思是想setdefault(key[,default]) 您的意思是想s

写以下内容似乎很愚蠢:

L = []

if x in L:
  L[x] = something
else:
  L[x] = something_else
这不执行两次x的查找吗?我尝试使用index(),但当找不到该值时会出现错误

理想情况下,我想说:

if x is in L, save that index and:
  ...

我可以理解,这可能是一个初级python习惯用法,但它似乎不适合搜索。谢谢。

您的意思是想
setdefault(key[,default])


您的意思是想
setdefault(key[,default])

如果您有一个列表,您可以使用
索引
,捕获抛出的
值错误

yourList = []
try:
    i = yourList.index(x)
except ValueError:
    i = None
然后可以测试i的值:

if i is not None:
    # Do things if the item was found.
如果您有一个列表,您可以使用
索引
,捕获抛出的
值错误

yourList = []
try:
    i = yourList.index(x)
except ValueError:
    i = None
然后可以测试i的值:

if i is not None:
    # Do things if the item was found.

另一个选项是try/except:

d = {}
try:
    d[x] = something_else
except KeyError:
    d[x] = something
结果与代码相同


编辑:好的,快速移动的目标。列表的习惯用法相同,但异常(索引器)不同。

另一个选项是try/except:

d = {}
try:
    d[x] = something_else
except KeyError:
    d[x] = something
结果与代码相同


编辑:好的,快速移动的目标。列表的习惯用法相同,但异常(索引器)不同。

我想你的问题让很多人感到困惑,因为你把语法混为一谈了。 如果:

这里您要查找的是列表中的值,不是键,也不是dict中的值:

if x in L: 
然后您使用x似乎是一个键,但在列表中它是一个int()索引,如果L中的x:不测试索引是否在L中,但值是否在L中:

L[x]=value
因此,如果要查看值是否位于列表中,请执行以下操作:

L = []                # that's a list and empty; and x will NEVER be in an empty list.
if x in L:            # that looks for value in list; not index in list
                      # to test for an index in a list do if len(L)>=x
   idx = L.index(x)
   L[idx] = something   # that makes L[index]=value not L[key]=value
else:
   # x is not in L so you either append it or append something_else
   L.append(x)
如果您使用:
L[x]=something
加上
如果L中的x:
那么只有这些值的列表才有意义:
L=[0,1,2,3,4,…]
L=[1.0,2.0,3.0,…]

但我想说:

L = []
L.extend(my_iterable)
coder0 = 'farr'
coder1 = 'Mark Byers'
if coder0 not in L:
    L.append(coder1)

奇怪的逻辑

我想你的问题把很多人弄糊涂了,因为你把语法混为一谈了。 如果:

这里您要查找的是列表中的值,不是键,也不是dict中的值:

if x in L: 
然后您使用x似乎是一个键,但在列表中它是一个int()索引,如果L中的x:不测试索引是否在L中,但值是否在L中:

L[x]=value
因此,如果要查看值是否位于列表中,请执行以下操作:

L = []                # that's a list and empty; and x will NEVER be in an empty list.
if x in L:            # that looks for value in list; not index in list
                      # to test for an index in a list do if len(L)>=x
   idx = L.index(x)
   L[idx] = something   # that makes L[index]=value not L[key]=value
else:
   # x is not in L so you either append it or append something_else
   L.append(x)
如果您使用:
L[x]=something
加上
如果L中的x:
那么只有这些值的列表才有意义:
L=[0,1,2,3,4,…]
L=[1.0,2.0,3.0,…]

但我想说:

L = []
L.extend(my_iterable)
coder0 = 'farr'
coder1 = 'Mark Byers'
if coder0 not in L:
    L.append(coder1)

奇怪的逻辑

你使用的是一本字典,它们没有像列表那样的索引或排序。标题是列表,我想这本字典是一个打字错误。你的问题对字典有一定的意义。现在它只是毫无意义…它是相当混乱,不是吗?不幸的是,我无法删除它,所以我试图修复它。你使用的是一本字典,它们没有索引,也没有列表那样的排序。标题是“列表”,我想这本字典是一个打字错误。你的问题对字典有一定的意义。现在它只是毫无意义…它是相当混乱,不是吗?不幸的是,我无法删除它,所以我试图挽救它。
d[x]=其他东西
永远不会生成keyrerror。它只会分配/覆盖该值。检索值时生成KeyError,而不是设置它。我同意@warvariuc。对于dict类型,不会在此处引发KeyError异常。此外,除之外的其他内容应位于
下方。如果这个例子是一个列表,其中
L=[]
你会在
except
子句中再次得到一个异常,因为
L[x]
仍然会引发一个异常。
d[x]=其他的东西
永远不会生成KeyError。它只会分配/覆盖该值。检索值时生成KeyError,而不是设置它。我同意@warvariuc。对于dict类型,不会在此处引发KeyError异常。此外,除
之外的其他内容应位于
下方。如果该示例是一个列表,其中
L=[]
您将在
except
子句中再次获得异常,因为
L[x]
仍将引发异常。