【问题标题】:How to create code that can change itself with a var [duplicate]如何创建可以使用 var 改变自身的代码 [重复]
【发布时间】:2020-10-19 00:25:21
【问题描述】:

可能是一个愚蠢的问题,但我什至不知道如何在搜索中提问。一直在寻找如何做到这一点,这似乎是一些非常基本的东西,我不知何故只是在某些时候掩盖了!

所以,假设我有一组列表:

List_1 = [1,2,3]
List_2 = [4,5,6]
List_3 = [7,8,9]
List_4 = [10,11,10]

我希望能够创建类似的东西:

SelectList = 0

for items in List_x:
         List_x.append(y)
         SelectList = SelectList + 1
         etc etc

最后,这种事情使用的术语是什么?

编辑:

从 cmets 看来,我所要求的已经是可能的,但不是我想的那样。

我所拥有的是一些非常非常长的代码,只需按照以下方式排列即可:

for item in list_1:
    thing1 = dict1.get('item')
    test1[1].append(dict1.get('Bla'))


for item in list_2:
    thing1 = dict2.get('item')
    test2[1].append(dict2.get('Bla'))


for item in list_3:
    thing3 = dict3.get('item')
    test1[3].append(dict3.get('Bla'))

并希望有一种方法可以生成这样的:


for item in list_x:
    thingx = dictx.get('item')
    thingx = dictx.get('item') 
    x = x + 1

【问题讨论】:

  • 我不明白你所说的“类似”的代码是什么意思。使用代码时会发生什么?
  • 你是在尝试做this吗?这有时被称为“动态变量”或“变量变量”。不过,只需将这些列表包装在另一个列表中,然后迭代外部列表。
  • 任何时候你想使用名称为List_1List_2 的变量并使用List_x 访问它们时,你应该使用一个列表。这就是他们的目的。
  • 几乎总是正确的解决方案是使用字典。 List[0] = [1,2,3]

标签: python python-3.x loops


【解决方案1】:

我不知道您在寻找什么术语,因为您似乎只想遍历列表。为此,您需要将列表放入列表中。像这样:

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]
list_4 = [10, 11, 10]

all_lists = [list_1, list_2, list_3, list_4]

for list_x in all_lists:
    for item in list_x:
         print(item)

这将打印所有列表中的所有项目。

或者,如果你想让它更接近你的代码:

list_1 = [1, 2, 3]
list_2 = [4, 5, 6]
list_3 = [7, 8, 9]
list_4 = [10, 11, 10]

all_lists = [list_1, list_2, list_3, list_4]

selected_list = 0
while selected_list < len(all_lists):
    for item in all_lists[selected_list]:
         print(item)
         selected_list = selected_list + 1

【讨论】:

  • 我认为在我的脑海中,我有点过于执着于编写 List_x 并让 x 成为唯一改变的东西,而不是必须列出每个列表,如果你知道我的意思的话。上述方式意味着我必须事先写出每个列表,而不仅仅是将 x 作为某种可以动态更改的字符串或 var。所以想象它不仅仅是第一个列表,而是很多 List_x Parts_x Names_x
【解决方案2】:

您可能想要使用一个新的function,它将一个列表作为参数。这样您就不必按名称识别列表。

def myfunc(mylist):
    for item in mylist:
        pass

正如之前有人提到的,您可以将列表存储在另一个列表中,或者如果您想命名它们,请使用哈希映射/字典。

mydict = dict()
mydict['1'] = [1, 2, 3]
mydict['2'] = [4, 5, 6]
mydict['name'] = [7, 8, 9]

您可以循环访问这些键,或者通过名称获取它们。

for key in mydict:
    myfunc(mydict[key])

或按名称

selected = '1' # could be 2 or name or whatever
myfunc(mydict[selected])

【讨论】:

    【解决方案3】:

    一个改变自己的程序可能很棘手且难以阅读,并且应该在没有替代或最简单的方法来完成任务时完成,我认为实现你想要的最好方法是使用 dict:

    lists = dict(List_1 = [1,2,3], List_2 = [4,5,6], List_3 = [7,8,9], List_4 = [10,11,10])
    

    然后您可以使用以下方法对其进行迭代:

    for k, v in lists.items():
        # Do whatever you need. v is the list and k is the name of this list. 
    

    如果您需要按字母数字排序,您可以执行以下操作:

    for k in sorted(lists.keys()):
       # Do whatever you need, you get the current list with lists[k]
    

    如果足够清楚,请告诉我。

    【讨论】:

    • 是的,我明白你的意思。在这种情况下,我为仅多次按数字更改的项目创建了相同的循环,这就是我寻找此快捷方式的原因。
    • 是的,我刚刚看到您编辑的内容,我最好的建议是重构您的代码,将所有重复变量作为字典或列表存储在数据结构中。它不仅可以避免你编写自修改代码的需要,还可以大大提高可读性。我知道重构可能看起来很痛苦和可怕,但技术债务更糟糕,即使它是一个小项目。
    • 感谢您的反馈,我明白您的意思,我会这样做。感谢您抽出宝贵时间帮助我!
    猜你喜欢
    • 2016-09-30
    • 2018-05-17
    • 2019-07-31
    • 2019-02-27
    • 2021-12-30
    • 2018-03-07
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多