如何在python中打印100个随机数的“set”并将它们添加到空集()(how to print 100 random numbers of “set” in python and add them to empty set())
如何在python中打印100个“set”的随机数,意味着我必须从给定范围中取100个随机数并将其添加到空集()中。 我需要Python中的解决方案。我已经尝试过以下方式,但它没有准确地获取100个数字。
import random s=set() for i in range(200): s.add((random.randint(0,101))) print(s) print(len(s))
how to print 100 random numbers of "set" in python, means I have to take 100 random numbers from given range and add it to an empty set(). I need solution in Python.I have tried in following way but its not taking 100 numbers exact.
import random s=set() for i in range(200): s.add((random.randint(0,101))) print(s) print(len(s))
原文:https://stackoverflow.com/questions/50248484
最满意答案
当你定义
x(t)
,由于它依赖于t
它最终成为符号函数(symfun
)而不是符号对象。 然后将这种依赖性转移到矩阵N
,使其成为依赖于t
的符号函数(这解释了为什么它只依赖于t
而不是L
)。>> syms t x(t) L >> N = ... >> whos Name Size Bytes Class Attributes L 1x1 112 sym t 1x1 112 sym x 1x1 112 symfun N 1x1 112 symfun
您可以通过上面的变通方法避免自动转换为
symfun
,也可以在创建矩阵N
时明确定义它,如下所示:>> N = sym(char([ 0, 0, ... 0, 0; 0, 0, ... 0, 0; 1 - 3*(x/L)^2 + 2*(x/L)^3, -x + 2*x^2/L - x^3/(L^2), ... 3*(x/L)^2 - 2*(x/L)^3, x^2/L - x^3/(L^2)]));
这里的技巧是
sym()
和char()
函数的组合使用。 如果您只使用sym()
而不将矩阵转换为字符串,则无法使用。话虽这么说,我个人发现你的第二种方法,你手动填充元素,以便更清晰,更容易阅读。
When you define
x(t)
it ends up as a symbolic function (symfun
) instead of a symbolic object due to its dependency ont
. This dependency is then carried over to your matrixN
, making it a symbolic function dependent ont
(which explains why it is only dependent ont
and notL
).>> syms t x(t) L >> N = ... >> whos Name Size Bytes Class Attributes L 1x1 112 sym t 1x1 112 sym x 1x1 112 symfun N 1x1 112 symfun
You can avoid the automatic conversion to
symfun
by the workarounds you do above, or you can define it explicitly when you create you matrixN
, like this:>> N = sym(char([ 0, 0, ... 0, 0; 0, 0, ... 0, 0; 1 - 3*(x/L)^2 + 2*(x/L)^3, -x + 2*x^2/L - x^3/(L^2), ... 3*(x/L)^2 - 2*(x/L)^3, x^2/L - x^3/(L^2)]));
The trick here is the combined use of the
sym()
andchar()
functions. If you only usesym()
without turning the matrix into a string it won't work.That being said, I personally find your second approach where you manually fill the elements to be more clear and easier to read.
相关问答
更多-
horchler在评论中建议创建适当的变量名称,以便使用sort正确进行词典排序。 我认为这是一个非常好的主意,但遗憾的是他建议的格式字符串不起作用。 它总是填零,最后得到: >> P1 = sym('P0%d_0%d',10) P = [ P01_01, P01_02, P01_03, P01_04, P01_05, P01_06, P01_07, P01_08, P01_09, P01_010] [ P02_01, P02_02, P02_03, P02_04, P02 ...
-
没有点的Matlab符号函数转换用于矩阵运算(Matlab symbolic function conversion without dot for matrix operation)[2021-12-28]
您可以在计算匿名函数时将sym对象转换为字符串: g=@(x)eval(char(f)) 或者,您可以使用以下代码 h=eval(['@(x)' char(f)]) 而不是matlabFunction You can convert the sym object to a string when calculating the anonymous function: g=@(x)eval(char(f)) Alternatively, you can use the following code h= ... -
你可能想尝试Theano 。 它实现了一个与sympy相比并行并且更快的雅可比函数。 以下版本实现了3.88的加速! 现在替代时间不如第二次。 import numpy as np import sympy as sp import theano as th import time def main_sympy(): start_time = time.time() Q = 17 f = sp.symbols(('f{} ' * Q).format(*range(Q))) ...
-
当你定义x(t) ,由于它依赖于t它最终成为符号函数( symfun )而不是符号对象。 然后将这种依赖性转移到矩阵N ,使其成为依赖于t的符号函数(这解释了为什么它只依赖于t而不是L )。 >> syms t x(t) L >> N = ... >> whos Name Size Bytes Class Attributes L 1x1 112 sym t ...
-
在使用符号数学时,不要使用字符串,例如{'M', 's1', 's2'} 。 这已被弃用,并且每个Matlab版本对这种用法的直接支持正在消失。 要实现您的目标,您可以使用: M = sym('M_%d_%d', [2 2], 'real'); syms s1 s2; myExpr = M*[s1;s2]; matlabFunction(myExpr, 'Vars', {M,s1,s2}, 'file', 'myExprFunc.m'); Don't use strings, e.g., {'M', 's ...
-
主要问题 Undefined function or method 'an' for input arguments of type 'double'. 这意味着MATLAB无法识别您的功能。 确保a在名为an.m的单独m文件中an.m ,并且它位于当前工作目录中。 其他问题 我可以看到你的arrayfun语法存在缺陷。 解决当前问题后,我预测您将遇到另一条错误消息: ??? Error using ==> an Too many input arguments. 问题是函数an只接受一个输入参数,但是你在 ...
-
对角化符号矩阵(Diagonalize symbolic matrix)[2022-06-22]
你可以从特征值计算它,但实际上有一种方法可以为你做diagonalize In [13]: M.diagonalize() Out[13]: ⎛ ⎡ __________ ⎤⎞ ⎜ ⎢ ╱ 2 ⎥⎟ ⎜⎡ -2⋅x ... -
尝试这个: lapply(flds, function(x) vector[x]) Try this: lapply(flds, function(x) vector[x])
-
Matlab:用向量和矩阵参数计算符号函数(Matlab: evaluate a symbolic function with vector and matrix arguments)[2022-06-04]
错误在您的函数定义中。 有关如何在MATLAB中正确声明函数的信息,请参阅此链接 。 把它放在一个名为“your_function_name.m”的单独文件中 function result = your_function_name(x, matrix) result = ((2*matrix)^(-1/2))*(x'); 然后你可以像这样调用你的函数: x = [x1 x2]; matrix = [h11 h12; h21 h22]; your_function_name(x, matrix); 或者 ... -
看一下维基百科有关矩阵求逆的文章 。 在考虑矩阵(Is-A)时,它具有非常特殊的形状,您可以使用该文章中给出的一些标识来反转它。 你所要做的就是把它分成块,其中A是你的对角线部分,B是右边的垂直向量,C是底部的水平向量,D是矩阵右下角的单个元素。 你必须这样做的唯一反转是反转A,它是对角的并且非常容易反转,并且(DC * inv(A)* B)是单个数字,因为你的C和B分别是行和列向量。 这可以通过手工或符号工具箱非常容易地完成。 Take a look at this Wikipedia article o ...