简体   繁体   English

在 for 循环中关联列表中的可迭代对象和项目

[英]Associate iterables and items from a list in a for loop

Is it possible to associate certain iterables in a loop with certain items from a list?是否可以将循环中的某些可迭代对象与列表中的某些项目相关联? I have two lists to start with ( totalpages and arguments ) and I need to build up certain URL's.我有两个列表开始( arguments totalpages ,我需要建立某些 URL。

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for i in totalpages:
  pages = list(range(0, x+100, 100))
  print(pages)
  for page, argument in zip(pages, arguments):
    urls = 'http://URL'+str(page)+argument
    urllst.append(urls)

urllst

I would like urllst to be like:我希望urllst是这样的:

[
'http://URL0argument1',
'http://URL100argument1',
'http://URL200argument1',
'http://URL300argument1',
'http://URL0argument2'
]

Let me write this answer to express my opinion on using index here.写下这个答案,表达一下我对这里使用索引的看法。

You were very close to the solution, but zip 'ped wrong lists finally.您非常接近解决方案,但zip最后输入了错误的列表。 Here's what should work:这是应该起作用的:

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for x, argument in zip(totalpages, arguments):
    for page in range(0, x+100, 100):
        url = f'http://URL{page}{argument}'
        urllst.append(url)

print(urllst)

This iterates over pairs (page_number, argument) made from two initial lists by taking items corresponding to the same indices.这通过获取对应于相同索引的项目来迭代由两个初始列表组成的对(page_number, argument) I switched to f-string to make string concatenation a bit prettier.我切换到 f-string 以使字符串连接更漂亮一些。

Here's a question about comparing index-based, enumerate -based and zip solutions for such kind of problems.这是一个关于比较基于索引、基于enumeratezip解决此类问题的问题。

To ensure that input lists are of equal size, you can use zip(totalpages, arguments, strict=True) - this requires python version 3.10 or newer.为确保输入列表的大小相等,您可以使用zip(totalpages, arguments, strict=True) - 这需要 python 版本 3.10 或更新版本。

Finally, if you're appending in a loop, you're probably missing an optimisation opportunity: list comprehension would be faster, especially on long inputs.最后,如果你在一个循环中追加,你可能会错过一个优化机会:列表理解会更快,尤其是在长输入上。

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = [
    f'http://URL{page}{argument}'
    for x, argument in zip(totalpages, arguments)
    for page in range(0, x+100, 100)
]

print(urllst)

With short zip (to aggregate elements from each of the iterables) + itertools.chain.from_iterable (to treat consecutive inner sequences as a single sequence) approach:使用 short zip (聚合来自每个可迭代对象的元素)+ itertools.chain.from_iterable (将连续的内部序列视为单个序列)方法:

import itertools

urllst = list(itertools.chain.from_iterable(
    [f'http://URL{p}{arg}' for p in range(0, page + 100, 100)] 
    for page, arg in zip(totalpages, arguments)))

print(urllst)

The output: output:

['http://URL0argument1', 'http://URL100argument1', 'http://URL200argument1', 'http://URL300argument1', 'http://URL0argument2']

Printing pages after it has been instantiated as a list of numbers will not give you the proper output. It is simpler to just go through each number in a modified nested for-loop:在将页面实例化为数字列表后打印页面不会为您提供正确的 output。通过修改的嵌套 for 循环中的每个数字仅 go 更简单:

totalpages = [300, 0]
arguments = ['argument1', 'argument2']
urllst = []

for i in range(len(totalpages)):
  curr_arg = arguments[i]
  for x in range(0, totalpages[i]+100, 100):
    urls = 'http://URL'+str(x)+curr_arg
    urllst.append(urls)

print(urls)

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

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