这里的结果是当我在我的系统中安装pip时。
xc@xc:~$ sudo apt install python3-pip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libexpat1-dev libjs-jquery libjs-sphinxdoc libjs-underscore l
我在python中遇到了一个问题,要读取的2d数组的输入格式是
3 # number of rows and columns of a square matrix
1 2 3 # first row
3 4 6 # second row
4 6 3 # third row
如何从控制台读取2d数组,如上面所示
Python版本: 3.6
IDE : Spyder (Python 3.6)
我有一个函数,它以numpy数组(A)作为输入。根据数学计算,这个数组可以是一个2d的数组,也可以是一个三维的数组。有一个整数m,它可以是任意数,除非数组是2D,否则m的值总是0。我想把A的声音传递给另一个函数。因为A既可以是3D的,也可以是2D的,所以我尝试了3D风格的切片。
def fun(A):
... some code
ans = fun2(A[:,:,m]) #The value of m is 0 if A is 2D
当A是2D时,这给了我一个IndexError
IndexError: too many indices for array
如果A是2D的话,我
我试图通过以下方式在python 3.4.3上打印内容:
a=1
b=2
print("text text %2d text text %2d", %(a,b))
但是,当我试图运行这段简单的代码时,在最后一个"%“符号”无效语法“上会出现一个标记:
File "<stdin>", line 1
print("text text %2d text text %2d", %(a,b))
^
SyntaxError: invali
因此,当我输入儿童、成人和老年人或3D和2D以外的任何内容时,我希望出现一条错误消息,说“对不起,无效输入”。然后我想让它再次要求用户输入ticketType和movieType,但是它想到了
"File "python", line 33, in <module>
File "python", line 27, in buyOneTicket
UnboundLocalError: local variable 'ticketCost' referenced before assignment"
。
print (
下面这段简单代码在Matlab中的pythonic实现是等价的。
Matlab:
B = 2D array of integers as indices [1...100]
A = 2D array of numbers: [10x10]
A[B] = 0
其工作得很好,例如对于B[i]=42,它找到要设置的列5的位置2。在Python中,它会导致错误: out of bound is logical。然而,为了将上面的Matlab代码转换成Python,我们正在寻找Python的方法。还请考虑更高维度的问题,例如:
B = 2D array of integers as indices [1
我知道要在Python中生成列表,可以使用如下命令:
l = [i**2 for i in range(5)]
而不是像这样使用for循环:
l = []
for i in range(5):
l.append(i**5)
有没有一种方法可以在不使用for循环的情况下做2D列表:
map = [[]]
for x in range(10):
row = []
for y in range(10):
row.append((x+y)**2)
map.append(row)
在Python中有没有其他表示2D数组的替代方案?
我正在尝试用我的2D Delaunay三角剖分来制作一本字典,类似于:{node_index : list of nodes }。我使用cgal python绑定来计算一组2D点云上的Delaunay三角剖分。
不幸的是,我没有在python中找到足够的文档,说明如何从三角剖分中获取每个节点的邻居。
如果你能帮我解决这个问题,我将非常感激。
我已经成功地用Cython将numpy数组发送到c函数。每当我在c中有一个接受int *的函数,并且在python中有一些变量时,就叫它arr,也就是1Dnumpy数组,我只使用&arr[0]调用函数,它就能工作。
现在我感兴趣的是接收一个用c函数创建的2D数组。我该怎么做?下面是我尝试过的。
// this is the c code that returns a 2D array
int **make_array() {
int nrows = 3;
int ncols = 5;
int **ret = malloc(nrows * sizeof(int *));
我在Java中有一个字符串,如下所示:
String s = "{{\"2D\", \"array\"}, {\"represented\", \"in a string\"}}"
如何将其转换为实际的数组?如下所示:
String[][] a = {{"2D", "array"}, {"represented", "in a string"}}
(我正在寻找的是一个有点像python的eval()的解决方案)
我正在使用python脚本从两个2D数组中提取公共元素/值。我知道我们可以在两个列表中提取共同的元素。
示例:
这里,a和b是两个2D数组:
a = [[1,2],[3,4],[5,6]...]
b = [[7,8],[1,2],[87,65],[3,4]...]
输出应该是另一个2D数组:
c = [[1,2],[3,4]]
如何做到这一点?
Python有一些很好的特性,比如共轭,这对于一个项目非常有用。
我尝试在一个特定的虚拟环境中通过pip安装,但是pip search nodebox只提供了:
NodeBox - Simple application for creating 2-dimensional
graphics and animation using Python code
nodebox-color - Color classes for python
NodeBox-for-OpenGL
我正在尝试用python从字典中创建一个2D数组。
mydictionary={
'a':['review','read','study'],
'b':['read'],
'c':['review','dictate']}
我想要一个显示匹配项数的2D数组(即比较键和它们的值,并将匹配值存储在2D数组中)。
Output Format:
a b c
___|___________________
a |
我需要用python写一个程序,从txt文件中读取一个2d列表,然后在一定的时间内(即2秒)做一些事情。
3 3
ULR
RRL
UUD
这就是txt文件的内部结构。
with open(argv[1]) as f:
n,m=f.read().split()
n=int(n)
m=int(m)
a=[[char for char in line]for line in f]
这是我用来将数据解析成2d列表的代码。
我的问题是它太慢了。该程序的主要算法的时间复杂度为O(n^2),我用c编写了相同的程序来测试相同的输入。为了以999x999数组作为输入成功终止,c程序需要大约5秒,而pytho