简体   繁体   English

python从特定索引遍历列表中的所有变量

[英]python loop over all vars in list from a specific index

I am new to python.我是 python 新手。 I'm trying to take all list's variables in the following manner:我正在尝试以下列方式获取所有列表的变量:

  1. Im going through the list and find the variable that I want.我浏览列表并找到我想要的变量。
  2. From that variable, I want to copy to a new list all the other list's variables starting from the variable I found + 2.从该变量中,我想将所有其他列表的变量从我找到的变量 + 2 开始复制到一个新列表中。

For Example:例如:

#If I go through this list until I find "0.1.0-730b9a1": 
versions_list= ["0.1.0-3572c21", "0.1.0-730b9a1", "0.0.2", "0.0.1", "0.0.3"]

#I want to be able to take all the rest of the items in the list from "0.1.0-730b9a1" + 2, to a new list.
#Means, my new list should contain only: 
["0.0.1", "0.0.3"]

In my code, when I find that version equals to "revision" in versions.yaml , I try to take all versions from versions_list starting from version + 2 to a new list called final_list :在我的代码中,当我发现该version等于versions.yaml中的“修订版”时,我尝试将所有版本从versions_list开始,从version + 2 到一个名为final_list的新列表:

import yaml

versions_list= ["0.1.0-3572c21", "0.1.0-730b9a1", "0.0.2", "0.0.1", "0.0.3"]
final_list= []
found = 0 
with open('versions.yaml', 'r') as file:
    catalog = yaml.safe_load(file)
                
for version in versions_list:    
    for item, doc in catalog.items():
        for key in doc:
          if key['revision'] == version:
            found = 1
            for item in versions_list[version + 2:]:
              final_list.append(versions_list[item])
    if found: break
print(final_list)

From the error I get, python is thinking that I am trying to concatenate string with int:根据我得到的错误,python 认为我正在尝试将字符串与 int 连接:

Traceback (most recent call last):
  File "mycode.py", line 14, in <module>
    for item in versions_list[version + 2:]:
TypeError: can only concatenate str (not "int") to str

I expect the following output:我期望以下输出:

["0.0.1", "0.0.3"]

The problem you are facing is that for version in versions_list iterates over the string values.您面临的问题是for version in versions_list迭代字符串值。 You would like to access the index of the item which can be achieved by wrapping it in an enumerate :您想访问可以通过将其包装在enumerate中来实现的项目的索引:

for idx, version in enumerate(versions_list):
    ...
    for item in versions_list[idx + 2:]:

You could do the following:您可以执行以下操作:

versions_list= ["0.1.0-3572c21", "0.1.0-730b9a1", "0.0.2", "0.0.1", "0.0.3"]
rev = "0.1.0-730b9a1"
versions_list[versions_list.index(rev)+2:]

It will give ['0.0.1', '0.0.3']它将给出['0.0.1', '0.0.3']

index will give you the position of the element you are looking for, and using slicing of that position (+2) you could get the desired results. index将为您提供您正在寻找的元素的位置,并使用该位置的切片 (+2) 您可以获得所需的结果。

Note that if the element is not in the list it will raise ValueError.请注意,如果元素不在列表中,它将引发 ValueError。 So you should test for it before, something like:因此,您应该先对其进行测试,例如:

versions_list[versions_list.index(rev[:-3])+2:] if rev[:-3] in versions_list else None

I would suggest try/except as an efficient and robust way of doing this:我建议将 try/except 作为一种有效且强大的方法来执行此操作:

versions_list= ["0.1.0-3572c21", "0.1.0-730b9a1", "0.0.2", "0.0.1", "0.0.3"]
rev = "0.1.0-730b9a1"
try:
    idx = versions_list.index(rev)
    result = versions_list[idx+2:]
except ValueError:
    result = []
print(result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM