Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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 - Fatal编程技术网

Python 列表列表:在整个列表中查找值

Python 列表列表:在整个列表中查找值,python,Python,我正在使用Python编写一个程序,查看列表列表并更改值 在我的列表列表中,我有3个,我想找到它们的索引。现在我只能让它在第一排工作。我想让它在“数字”中的任何列表中找到3 下面是一些用于冲洗泥浆的示例代码: numbers = [ [3, 3, 3, 5, 3, 3, 3, 3, 6], [8, 0, 0, 0, 4, 7, 5, 0, 3], [0, 5, 0, 0, 0, 3, 0, 0, 0], [0, 7, 0, 8, 0, 0, 0, 0, 9],

我正在使用Python编写一个程序,查看列表列表并更改值

在我的列表列表中,我有3个,我想找到它们的索引。现在我只能让它在第一排工作。我想让它在“数字”中的任何列表中找到3

下面是一些用于冲洗泥浆的示例代码:

numbers = [
    [3, 3, 3, 5, 3, 3, 3, 3, 6],
    [8, 0, 0, 0, 4, 7, 5, 0, 3],
    [0, 5, 0, 0, 0, 3, 0, 0, 0],
    [0, 7, 0, 8, 0, 0, 0, 0, 9],
    [0, 0, 0, 0, 1, 0, 0, 0, 0],
    [9, 0, 0, 0, 0, 4, 0, 2, 0],
    [0, 0, 0, 9, 0, 0, 0, 1, 0],
    [7, 0, 8, 3, 2, 0, 0, 0, 5],
    [3, 0, 0, 0, 0, 8, 0, 0, 0],
    ]
a = -1
while a:
    try:
        for row in numbers:
            a = row[a+1:].index(3) + a + 1
            print("Found 3 at index", a)
    except ValueError:
        break
当我运行它时,我得到:

Found 3 at index 0
Found 3 at index 1
Found 3 at index 2
Found 3 at index 4
Found 3 at index 5
Found 3 at index 6
Found 3 at index 8
这表明它正在工作,但仅在第一行


谢谢

如果您只想获取行索引,请使用迭代
numbers
,并使用以下方法测试
3
是否在列表中:

对于行索引和列索引,迭代两个列表:

for index, row in enumerate(numbers):
    for col, value in enumerate(row):
        if value == 3:
            print "3 found in row %i at position %i" % (index, col)
如果只想在新列表中获取索引,可以使用:

请尝试以下操作:

[[i for i, v in enumerate(row) if v == 3] for row in numbers]
这将产生一个列表列表,其中内部列表中的每个条目都是原始列表对应行中的3的索引:

[[], [8], [5], [], [], [], [], [3], [0]]
您说过要查找3,但您的代码似乎要查找0,您想要哪个

您可以这样使用它:

threes = [[i for i, v in enumerate(row) if v == 3] for row in numbers]
for row, indices in enumerate(threes):
    for col in indices:
        print "3 found in row %d, column %d" % (row, col)

下面是一个小代码片段,让您开始学习:

>>> for i, row in enumerate(numbers):
        if 3 in row:
            print i, row.index(3)   

1 8
2 5
7 3
8 0

>>> numbers[1][8]
3
>>> numbers[2][5]
3
>>> numbers[7][3]
3
>>> numbers[8][0]
3

与其显示信息,不如建立一个数据结构,为我们提供每个
3
的“坐标”:

x = [
    (r, c)
    for r, row in enumerate(numbers)
    for c, cell in enumerate(row)
    if cell == 3
]
我们可以很容易地验证它:

assert(all(numbers[r][c] == 3 for r, c in x))
但是,如果您想替换值,那么尝试建立此列表,然后使用它手动返回并替换内容是愚蠢的。更干净,只需直接产生所需的输出。也就是说,“如果对应的原始值为
3
,则列表的值为
None
(为了论证起见),否则为原始值)”

那拼写像

[[None if cell == 3 else cell for cell in row] for row in numbers]

尝试使用scipy/numpy来完成此操作

import scipy as sp
matrix = your_matrix #write your matrix here. I left it.
x,y = sp.where(matrix == 3)
for i in range(len(x)):
    print x[i],y[i]

您想查找
3
还是
0
?在
行进行切片的目的是什么?我不知道你说的“工作”是什么意思。您的代码肯定不仅仅是在检查第一行,但可能不是您想要的方式。用手(必要时用铅笔和纸)检查代码,看看Python在做什么。确保仔细跟踪
a
的值。我所说的“工作”是指它只在第一行“做应该做的事”。它肯定不会查看整个列表。请更清楚地说明您的代码应该做什么。并验证即使在第一行它也能按预期工作。@cbbcbail:它不查看整个列表的唯一原因是您告诉它提前停止。它看起来比第一排还多。请,请检查您的代码(假设您是Python解释器)并跟踪
a
是什么。请注意,
a
的值将影响
行[a+1:]
。我希望在这样做的同时从中受益并学到一些东西。你能解释一下你的代码吗?只是解释一下每个部分的功能以及为什么它会这样做。谢谢只有三行。只需使用它一点就可以了解它的功能:-)enumerate()在行上循环,并将行索引返回为i。
if 3 in row
测试行中是否有三个。印刷品印出来了。
行索引(3)
计算3的索引。这一切都和它说的差不多。真的一点也不神奇:-)哦,有没有办法知道它在哪个“行”或列表中?是的,这就是i变量所代表的。这不完全是我要找的。我需要一种方法来运行我为“行”运行的代码,但我希望它能为所有行运行。@cbbcbail-请参阅我的编辑,其中显示了如何使用结果。嵌套理解为I+1。cbbcbail,仔细分析@F.J的最初答案是值得的。我喜欢——它与文档有一个链接。:)@sarnold-我删除了它,因为我意识到我链接到的页面是针对弃用的集合模块的,而不是内置的集合类型。正确的文档没有引用
\uuuu eq\uuuu()
,这是我的答案与其他答案之间的唯一区别,因此它有点毫无意义。链接到文档总是让答案更好,但我同意:)我想从中受益并学到一些东西。你能解释一下你的代码吗?我链接到了相应的文档。我建议读一下,解释得很好。
[[None if cell == 3 else cell for cell in row] for row in numbers]
import scipy as sp
matrix = your_matrix #write your matrix here. I left it.
x,y = sp.where(matrix == 3)
for i in range(len(x)):
    print x[i],y[i]