简体   繁体   English

如何使用python中的列表索引在for循环中进行字符串插值

[英]How to do string interpolation in a for loop using the indices of a list in python

I have a list and I want to put its elements inside a for loop string like this: 我有一个列表,我想将其元素放入for循环字符串中,如下所示:

my_list = ["Germany", "England", "Spain", "France"]
for this in that:
    do stuff
    print(my_list[0]+ some other stuff)

The output should be like: 输出应类似于:

Germany + some other stuff
England + some other stuff
Spain + some other stuff
France + some other stuff

How can I loop the indices for string interpolation as well? 我该如何循环索引以进行字符串插值?

Thanks! 谢谢!

Edit: The loop is a bit different. 编辑:循环有点不同。 It's more like this: 更像是这样:

for foo in bar:
    another_bar = []
    for x, y in foo:
        do stuff
        a = object.method()
        another_bar.append(my_list[0]+a)

I need to put the strings of lists into the 2nd layer of nested loop. 我需要将列表的字符串放入嵌套循环的第二层。 Can't use zip here. 在这里不能使用zip。

I believe you are assuming that that is the same lenght as my_list . 我相信你的假设that相同lenght为my_list If so, you can use zip to iterate over the two containers in parallel. 如果是这样,您可以使用zip来并行遍历两个容器。

my_list = ["Germany", "England", "Spain", "France"]
my_other_list = [' is great at football', ' has good years in rugby', ' has Renaldo', ' is, well...']

def foo(bar):
    return bar + '!'

for country, bar in zip(my_list, my_other_list):
    other = foo(bar)
    print(country + other)

# Output:
# Germany is great at football!
# England has good years in rugby!
# Spain has Renaldo!
# France is, well...!

You can use the built-in function zip() . 您可以使用内置函数zip() zip allows to process each list in parallel: zip允许并行处理每个列表:

Make an iterator that aggregates elements from each of the iterables. 创建一个迭代器,以聚合每个可迭代对象中的元素。

Returns an iterator of tuples, where the i -th tuple contains the i-th element from each of the argument sequences or iterables. 返回一个元组的迭代器,其中第i个元组包含每个参数序列或可迭代对象中的第i个元素。 The iterator stops when the shortest input iterable is exhausted. 当最短的可迭代输入耗尽时,迭代器将停止。 With a single iterable argument, it returns an iterator of 1-tuples. 使用单个可迭代参数,它将返回1元组的迭代器。 With no arguments, it returns an empty iterator. 没有参数,它将返回一个空的迭代器。

my_list = ["Germany", "England", "Spain", "France"]
for country, this in zip(my_list, that):
    # do stuff
    print(country + that)

If your list are of different size, you can use itertoos.zip_longest : 如果列表大小不同,则可以使用itertoos.zip_longest

Make an iterator that aggregates elements from each of the iterables. 创建一个迭代器,以聚合每个可迭代对象中的元素。 If the iterables are of uneven length, missing values are filled-in with fillvalue. 如果可迭代项的长度不均匀,则将缺失值填充为fillvalue。 Iteration continues until the longest iterable is exhausted. 迭代一直持续到最长的可迭代耗尽为止。

from itertools import zip_longest

my_list = ["Germany", "England", "Spain", "France"]
for country, this in zip_longest(my_list, that):
    # do stuff
    print(country + that)

I hope this can help you. 希望对您有所帮助。

for index, this in enumerate(that):
    do stuff
    print(my_list[index]+ some other stuff)

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

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