简体   繁体   English

从Python转换为C List函数

[英]Converting from Python to C List functions

L = [[" " for i in range(10)] for j in range(10)]

for i in range (10):
    L[9][i]="*"
for i in range (10):
        L[8][i]="1"
for i in range (10):
        L[7][i]="*"
for i in range (10):
        L[6][i]="3"
for i in range (10):
        L[5][i]="*"

print(L)
print()

def Check_Lines(l):
    for i in range (10):
        x=l[i].count("*")
        if x == 10:
            print ("LINE IS FULL")
            del l[i]
            l.reverse()
            l.append([" "," "," "," "," "," "," "," "," "," "])
            l.reverse()


Check_Lines(L)
print (L)

I wrote the above function in Python as I am more familiar with the python language. 我用Python编写了上述函数,因为我对python语言更加熟悉。 What it does is searches a 10 by 10 list and if a single row is filled with * then it would delete it and put a new empty row at the top. 它的作用是搜索10 x 10的列表,如果单行用*填充,则它将删除它,并在顶部放一个新的空行。

I know c does not have the list functions I used. 我知道c没有我使用的列表函数。 Is there any easy way of going about what I am doing? 有什么简单的方法可以处理我的工作吗?

In C unfortunately that problem would be pretty difficult. 不幸的是,在C中,这个问题将非常困难。 Would require you to allocate a memory block for your matrix. 将要求您为矩阵分配一个内存块。 And when you wanted to "remove" a row you'd have to move everything up and free the extra memory at the bottom of the matrix. 而且,当您要“删除”一行时,您必须将所有内容向上移动并释放矩阵底部的额外内存。

If you can use C++ this is much more easily done with STL containers. 如果可以使用C ++,则使用STL容器可以轻松得多。 You could have a vector of vectors. 您可以有一个向量向量。 Removing a row would be a call to erase: 删除一行将导致擦除操作:

l.erase(pos)  // pos is an iterator at the row you'd like to remove

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

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