英文:
Applying multiple 'for' in one list
问题
我需要在一个列表中应用多个'for',以使用先前创建的列表。
我还需要将最后一个列表中的值四舍五入到第二位小数,但存在我不理解的问题。
这是代码的一部分:
koef_ob_dvig = 39
v_list = [0, 20, 40, 60, 80, 100, 120, 140, 160, 170]
n = [i * koef_ob_dvig for i in v_list]
Ne_list = [round((x / 3600 * mekh_kpd) * (G * koef_dor_sopr + k * F * (x ** 2) / 13), 2) for x in v_list]
n_tabl = [0, 20, 40, 60, 80, 100, 120]
Ne_tabl = [0, 20, 50, 73, 92, 100, 92]
n_graph = [round((max(n)/100 * x1), 2) for x1 in n_tabl]
Ne_graph = [round((max(Ne_list)/100 * x2), 2) for x2 in Ne_tabl]
#进一步的问题出现
Mk_tabl = [(30 * x3 / 3.14 for x3 in Ne_graph) / x4 for x4 in n_graph]
#这部分是正确的 (30 * x3 / 3.14 for x3 in Ne_graph)
#但是,当我尝试将每个接收的元素除以n_graph列表中的相应元素时,会出现错误
希望这能帮助你解决问题。
英文:
I need to apply multiple 'for' in one list to use previously created lists.
I also need to round the values in the last list to the second digit, but there is a problem that I don't understand.
Here is part of the code:
koef_ob_dvig = 39
v_list = [0, 20, 40, 60, 80, 100, 120, 140, 160, 170]
n = [i * koef_ob_dvig for i in v_list]
Ne_list = [round((x / 3600 * mekh_kpd) * (G * koef_dor_sopr + k * F * (x ** 2) / 13), 2) for x in v_list]
n_tabl = [0, 20, 40, 60, 80, 100, 120]
Ne_tabl = [0, 20, 50, 73, 92, 100, 92]
n_graph = [round((max(n)/100 * x1), 2) for x1 in n_tabl]
Ne_graph = [round((max(Ne_list)/100 * x2), 2) for x2 in Ne_tabl]
#Further problems arise
Mk_tabl = [(30 * x3 / 3.14 for x3 in Ne_graph) / x4 for x4 in n_graph]
# This part works correctly (30 * x3 / 3.14 for x3 in Ne_graph)
# But when I try to divide each received element into the corresponding element of the n_graph list an error occurs
答案1
得分: 0
我不太清楚您对这段代码的具体目标是什么,但我认为可能有两种情况。
如果您的目标是对Mk_tab1变量执行与Ne_graph中的每个元素与n_graph中对应元素的计算,那么可以考虑如下的示例数据:
#考虑这个基本示例
#假设您只想将Ne_graph的每个元素除以n_graph的每个元素:
Ne_graph = [a, b, c, d, e]
n_graph = [1, 2, 3, 4, 5]
...
Mk_tab1 = [a, b/2, c/3, d/4, e/5]
如果这是您想要的,那么最好的方法是创建一个函数,该函数以两个相同长度的列表作为输入,运行一个for循环,并返回相同长度的所需计算结果;类似这样的:
def myfun(list1, list2):
if len(list1) != len(list2):
#抛出错误或执行其他操作
else:
result = [list1[i] / list2[i] for i in range(len(list1)]
return result
Mk_tab1 = myfun(Ne_graph, n_graph)
如果您实际想要的是对Ne_graph中的每个元素与n_graph中的每个元素进行计算,例如:
#考虑相同的基本示例
#如果您想对一个列表中的每个元素与另一个列表中的每个元素进行操作
#那么结果将是一个二维列表;这意味着您需要两次使用列表推导:
Ne_graph = [a, b, c, d, e]
n_graph = [1, 2, 3, 4, 5]
...
Mk_tab1 = [[a, b, c, d, e],
[a/2, b/2, c/2, d/2, e/2],
[a/3, b/3, c/3, d/3, e/3],
[a/4, b/4, c/4, d/4, e/4],
[a/5, b/5, c/5, d/5, e/5]]
那么可以使用以下方式实现:
Mk_tab1 = [[x/y for x in Ne_graph] for y in n_graph]
英文:
I don't quite understand clearly what your objective is for this particular piece of code, but I think it's either one of 2 things.
Forgetting about the specific calculations for a second, and assuming that your goal for the Mk_tab1 variable is to perform calculations on each element in Ne_graph with each corresponding element in n_graph: <br> ie, with this example data below:
#consider this basic example
#pretend that you wanted to just divide each element of Ne_graph by each element of n_graph:
Ne_graph = [a, b, c, d, e]
n_graph = [1,2,3,4,5]
...
Mk_tab1 = [a,b/2,c/3,d/4,e/5]
If that's what you wanted, then the best way to go about that would be to create a function that takes 2 lists of equal length as an input, runs a for loop, and returns a list of the same length with the required calculations; something like this:
def myfun(list1,list2):
if len(list1) != len(list2):
#throw error, do something else
else:
result = [list1[i] / list2[i] for i in range(len(list1))]
return result
Mk_tab1 = myfun(Ne_graph,n_graph)
If what you actually want is to run calculations of every element in Ne_graph with every element in n_graph: <br> for example:
#consider the same basic example
#if you wanted to operate on each element in one list with each element of the other list
#then the result would be a 2D list; meaning you need to list comp twice:
Ne_graph = [a,b,c,d,e]
n_graph = [1,2,3,4,5]
...
Mk_tab1 = [[a,b,c,d,e],
[a/2,b/2,c/2,d/2,e/2],
[a/3,b/3,c/3,d/3,e/3],
[a/4,b/4,c/4,d/4,e/4],
[a/5,b/5,c/5,d/5,e/5]]
Then that could be accomplished something like this:
Mk_tab1 = [[x/y for x in Ne_graph] for y in n_graph]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论