简体   繁体   English

如何在python中使用枚举增加行位置

[英]How can I increase row position using enumerate in python

I want to be able to find indices of certain characters in a matrix without importing numpy.我希望能够在不导入 numpy 的情况下找到矩阵中某些字符的索引。

For example I have a matrix consists of .例如我有一个矩阵由 . or O at random或 O 随机

['.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', 'O', 'O', '.', '.']
['.', '.', 'O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']
['.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.', '.', '.']
['.', '.', '.', '.', 'O', '.', '.', 'O', '.', '.', '.', '.', '.', '.']

I've come close to my solution using enumerate我已经接近使用 enumerate 的解决方案

for y, e in enumerate(content_matrix):
                for x, ee in enumerate(e):
                    if 'O' in ee:
                        print(x, y)

I want my output to be (0,7),(0,10),(0,11),(1,2),(2,7).... and so on...我希望我的输出是 (0,7),(0,10),(0,11),(1,2),(2,7).... 等等...

However, in the code I have above, it gives me但是,在我上面的代码中,它给了我

0 7
0 10
0 11
0 2
0 7
0 4
0 7

These sets of numbers.这组数字。 (Ignore the formatting for those numbers) (忽略这些数字的格式)

The x value(row value) is always 0 when it should be going up with the nth row.当 x 值(行值)应该与第 n 行一起上升时,它始终为 0。

What can I do to fix this problem?我能做些什么来解决这个问题? Sorry for the awful wording in advance.很抱歉提前发表了糟糕的措辞。

It's possible that you have something wrong with the declaration of you matrix?您的矩阵声明可能有问题吗?

I tred to store you matrix as:我试图将您的矩阵存储为:

content_matrix = [['.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', 'O', 'O', '.', '.'],
['.', '.', 'O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', 'O', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', 'O', '.', '.', 'O', '.', '.', '.', '.', '.', '.']]

And your code provide me this output (that seems right to me):你的代码为我提供了这个输出(这对我来说似乎是正确的):

7 0
10 0
11 0
2 1
7 2
4 3
7 3

Also, the output you provided does not seem consistent with the order in which x and y are printed.此外,您提供的输出似乎与 x 和 y 的打印顺序不一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM