Python 根据特定条件在列表中间插入值
我有一个名为category的列表,它有以下值Python 根据特定条件在列表中间插入值,python,list,for-loop,Python,List,For Loop,我有一个名为category的列表,它有以下值 `category=['Constant', 'Constant', 'Constant', 'Constant', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Constant', 'Constant', 'Categorical', 'Constant', 'Categorical', 'Categorical', 'Categorica
`category=['Constant', 'Constant', 'Constant', 'Constant', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Constant', 'Constant', 'Categorical', 'Constant', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Categorical', 'Constant', 'Categorical', 'Categorical', 'Categorical']`
我必须浏览这个列表,创建一个新列表,并在值为“常量”的地方插入名为“无”的值。但在我的例子中,我只能在开头插入'None'(对于前4个值),不能在列表中间插入'None'。(例如:我想在第10位、第11位、第13位打印“无”)。你能看看我的代码,告诉我我犯了什么错误吗
distinct = []
for x in category:
if x == 'Constant':
distinct.append('None')
elif x == 'Categorical':
distinctvalue = driver.find_elements_by_xpath("My XPath has list of items")
for dv in distinctvalue:
distval = dv.text
distinct.append(distval)
break
print(distinct)
O/p:我得到了['None'、'None'、'None'、'1027'、'1117'、'1027'、'2'、'2'、'26'、'2'、'363'、'96'、'363'、'339545'、'96']
预期O/p:
['None'、'None'、'None'、'None'、'1027'、'1117'、'1027'、'None'、'None'、'2'、'None'、'363'、'96'、'None'、'363'、'339545'、'96']
从代码的elif块中删除'break'
elif x == 'Categorical':
distinctvalue = driver.find_elements_by_xpath("My XPath has list of items")
for dv in distinctvalue:
distval = dv.text
distinct.append(distval)
break # remove this
不要试图在
elif
中执行整个循环,而是在需要时从列表中获取下一个值
有两种方法可以做到这一点:
按列表索引。
使用索引变量(此处为i
)跟踪您在列表中的位置:
distinct = []
distinctvalue = driver.find_elements_by_xpath("My XPath has list of items")
i = 0 # <== initialise the index variable
for x in category:
if x == 'Constant':
distinct.append('None')
elif x == 'Categorical':
dv = distinctvalue[i] # <== use the index variable to look up next value
i += 1 # <== increment the counter ready for next time
distval = dv.text
distinct.append(distval)
print(distinct)
与:
使用迭代器。
这次我们使用iter
从列表中生成迭代器,并使用next
从该迭代器中获取下一个值:
distinct = []
distinctvalue = iter(driver.find_elements_by_xpath("My XPath has list of items")) # <== get the iterator
for x in category:
if x == 'Constant':
distinct.append('None')
elif x == 'Categorical':
dv = next(distinctvalue) # <== next one
distval = dv.text
distinct.append(distval)
print(distinct)
与:
我试过这个,但它没有打破我的循环。它打印如下:[‘无’、‘无’、‘无’、‘1027’、‘1117’、‘2’、‘1117’、‘1027’、‘2’、‘2’、‘26’、‘2’、‘363’、‘339545’、‘96’、‘363’、‘2’、‘2’、‘1117’、‘1027’、‘2’、‘2’、‘26’、‘2’、‘363’、‘96’、‘363’、‘363’、‘363’、‘363’、‘339545’、‘96’、‘1117’、‘2’、‘1117’、‘2’、‘1117’、‘2’、‘1117’、‘1027’、“,”2“,”2“,”26“,”2“,”363“,”96“,”363“,”339545“,”96“,”1027“,”1117“,”2“,”2“,”2“,”26“,”2“,”363“,”363“,”1027“,”2“,”2“,”2“,”363“,”96“,”363“,…]我想错误在第二个for循环中。当它到达第二个循环时,它会不断迭代值列表,并不断追加值(dv到循环。您应该重写代码,只从列表中附加一个值。您可以共享xpath列表吗?谢谢。现在它运行良好。@Thiyagu很高兴它能为您工作-谢谢您让我知道。
try:
dv = distinctvalue[i]
except IndexError:
break
distinct = []
distinctvalue = iter(driver.find_elements_by_xpath("My XPath has list of items")) # <== get the iterator
for x in category:
if x == 'Constant':
distinct.append('None')
elif x == 'Categorical':
dv = next(distinctvalue) # <== next one
distval = dv.text
distinct.append(distval)
print(distinct)
dv = next(distinctvalue)
try:
dv = next(distinctvalue)
except StopIteration:
break