将字符串值连接到相应的变量名称 [英] Connect string value to a corresponding variable name

查看:32
本文介绍了将字符串值连接到相应的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我之前的帖子有某种关系.请参阅此处overlap-of-nested-lists-creates-unwanted-gap

This question has somehow to do with an earlier post from me. See here overlap-of-nested-lists-creates-unwanted-gap

我想我已经找到了一个解决方案,但我不知道如何实现它.

I think that I have found a solution but i can't figure out how to implement it.

首先是相关代码,因为我认为这样更容易解释我的问题.我准备了一个小提琴来显示代码:

First the relevant code since I think it is easier to explain my problem that way. I have prepared a fiddle to show the code:

此处为 PYFiddle

每次迭代都会根据填充ag中的嵌套列表.下一次迭代应该填充 ag 中的下一个嵌套列表,但取决于之前填充的列表的长度.

Each iteration fills a nested list in ag depending on the axis. The next iteration is supposed to fill the next nested list in ag but depending on the length of the list filled before.

实现这一点的一般想法如下:

The generell idea to realise this is as follows:

首先,我会将顶部 for 循环中的每个嵌套列表分配给这样的变量:

First I would assign each nested list within the top for-loop to a variable like that:

    x = ag[0]
    y = ag[1]
    z = ag[2] 

为了识别第一个列表,我需要像这样访问 data_j.我认为访问会这样工作.

In order to identify that first list I need to access data_j like that. I think the access would work that way.

data_j[i-1]['axis']  

data_j[i-1]['axis'] 返回 x,yz 作为字符串

data_j[i-1]['axis'] returns either x,y or z as string

现在我需要获取对应于从 data_j[i-1]['axis'] 返回的轴的列表长度.

Now I need to get the length of the list which corresponds to the axis returned from data_j[i-1]['axis'].

问题是如何将data_j[i-1]['axis']的值"与其对应的x = ag[0]连接起来,y = ag[1]z = ag[2]

The problem is how do I connect the "value" of data_j[i-1]['axis'] with its corresponding x = ag[0], y = ag[1] or z = ag[2]

由于 eval()globals() 是不好的做法,我需要朝正确的方向推进.我找不到解决方案

Since eval() and globals() are bad practice I would need a push into the right direction. I couldn't find a solution

我想我想出了一个办法.我将尝试使用父循环的迭代器 i(参见小提琴),而不是绕道使用实际的轴名称,因为它为 data_j 中的每个元素增加它有点创建了一个 id,我想我可以用它来创建一种方法,将它用于巢的索引以解决正确的列表.

I think I figured out a way. Instead of taking the detour of using the actual axis name I will try to use the iterator i of the parent loop (See the fiddle) since it increases for each element from data_j it kinda creates an id which I think I can use to create a method to use it for the index of the nest to address the correct list.

推荐答案

我设法使用迭代器 i 解决了这个问题.请参阅我原始帖子中的小提琴,以了解我对以下代码所做的操作:

I managed to solve it using the iterator i. See the fiddle from my original post in order to comprehend what I did with the following piece of code:

if i < 0:
    cond = 0
else:
    cond = i
pred_axis = data_j[cond]['axis']      

if pred_axis == 'x':
    g = 0
elif pred_axis == 'y':
    g = 1
elif pred_axis == 'z':
    g = 2   

calc_size = len(ag[g])
n_offset = calc_size+offset

我还没想好为什么 cond 必须是 i 而不是 i-1 但它有效.一旦我弄清楚它背后的逻辑,我就会发布它.

I haven't figured yet why cond must be i and not i-1 but it works. As soon as I figure out the logic behind it I will post it.

EDIT:它不适用于 i 它适用于 i-1.我的相关列表索引从 1 开始.ag[0] 保留给一个常量,如果需要,可以添加该常量以进行进一步计算.因此,由于相关索引从一开始就按 1 的值向上移动,因此我不需要在每次运行中减少迭代器.

EDIT: It doesn't work for i it works for i-1. My indices for the relevant list start at 1. ag[0] is reserved for a constant which can be added if necessary for further calculations. So since the relevant indices are moved up by the value of 1 from the beginning already i don't need to decrease the iterator in each run.

这篇关于将字符串值连接到相应的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆