gpt4 book ai didi

python - 使用 if 语句搜索嵌套列表

转载 作者:行者123 更新时间:2023-11-28 20:30:04 25 4
gpt4 key购买 nike

我正在尝试从嵌套列表中取出值低于 50 的元素以及相应的值以显示它们。我尝试制作它,但它什么也没给我。

代码如下:

newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
deptInfo = [[]]

for row in range(0, len(newList)) :
deptInfo.append([newList[row], deptNums[row]])
print(deptInfo)

belowFifty = []
for items in deptInfo:
if (50 > deptNums[row]):
belowFifty.append(newList[row],deptNums[row])
print(belowFifty)

最佳答案

您没有在第二个 for 循环中迭代变量“行”。变量 'row' 的范围以第一个 for 循环结束。更合适的代码:

newList = ["payroll", "accounting", "security", "office", "sales"]
deptNums = [10 * index for index in range(1, 16)]
deptInfo = [[]]

for row in range(0, len(newList)) :
deptInfo.append([newList[row], deptNums[row]])
print(deptInfo)

belowFifty = []
for item, number in zip(newList, deptNums):
if 50 > number:
belowFifty.append([item, number])
print(belowFifty)

关于python - 使用 if 语句搜索嵌套列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58725638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com