简体   繁体   English

如何以“ pythonic”方式将单独的字符串添加到列表中的各种字符串?

[英]How do you add separate strings to various strings in a list in a 'pythonic' way?

I have a list of strings. 我有一个字符串列表。 I need to add a string to all of them, and then a second string to only two of them. 我需要向所有字符串添加一个字符串,然后仅向其中两个字符串添加第二个字符串。 I am trying the following: 我正在尝试以下方法:

[s + ' help ' for s in [ ['me '].extend(['with ' + t for t in ['things', 'please']]) ] ]

What I would expect is the following: 我期望的是以下内容:

['me help ', 'with things help ', 'with please help ']

Nothing happens when I try to run this in iPython. 当我尝试在iPython中运行此程序时,没有任何反应。 It seems to have a problem with using the extend method on a 'newly created' list. 在“新创建的”列表上使用扩展方法似乎存在问题。 I am unsure, and relatively new to python. 我不确定,对于python来说还比较陌生。

EDIT: 编辑:

To be more explicit, in the above example, the original list would be: 更明确地说,在上面的示例中,原始列表为:

['me', 'things', 'please']

I need to add 'help ' to all of them, and 'with ' to only the latter two. 我需要对所有这些都添加“帮助”,并且仅对后两个添加“ with”。

And I get an error when running the above line of code: 运行上面的代码行时出现错误:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Second Edit: 第二编辑:

I am more focused on the location of the elements. 我更专注于元素的位置。 I want a string added to every element of the list, excluding the first, and then a second string added to the entire new list. 我想将一个字符串添加到列表的每个元素中,但不包括第一个元素,然后将第二个字符串添加到整个新列表中。 My above approach was a first stab at what it should be (in the most 'pythonic' way), but it does not work. 我的上述方法是首先刺探其应有的状态(以最“ pythonic”的方式),但它不起作用。 I understand why it does not work, but I am unsure how to rewrite the above line into something that will work. 我知道为什么它行不通,但是我不确定如何将上述行改写成行之有效的方法。

In case you want to index the elements in the list you can use this: 如果要对列表中的元素建立索引,可以使用以下方法:

 s=['me', 'things', 'please']
   ['with ' + x + ' help' if x!=s[0] else x+' help' for x in s]

Unless you define a function, which extends a list and returns the result: 除非您定义一个函数,该函数将扩展列表并返回结果:

def lext(l, lx):
    l.extend(lx)
    return l

(which python already does): (python已经做到了):

import itertools
lext = itertools.chain

and employ it this way: 并以这种方式使用它:

[s + ' help ' for s in lext(['me '], ['with ' + t for t in ['things', 'please']])]

you have to do it step by step: 您必须逐步进行:

inp = ['me', 'things', 'please']
out1 = [ s + ' help' for s in inp]
out2 = [ out1[0] ]
out2.extend(('with ' + s for s in out1[1:]))

Welcome to the wonderful world of procedures and functions, aka. 欢迎来到过程和功能的美好世界。 commands and expressions :) 命令和表达式:)

If it's string match based: 如果基于字符串匹配:

['with ' + t + ' help' if t != 'me' else t + ' help' for t in ['me', 'things', 'please']]

This will create a list of strings based if an item is me or not. 这将基于项目是否为me创建一个字符串列表。

Produces: 产生:

['me help', 'with things help', 'with please help']

If it's positional: 如果位置正确:

If you always know the position, this might help: 如果您始终知道职位,这可能会有所帮助:

x = ['me', 'things', 'please']
word_pos = 1
['{} help'.format(i) for i in x[:word_pos]] + ['with {} help'.format(i) for i in x[word_pos:]]

Which produces: 产生:

['me help', 'with things help', 'with please help']

Either one can be a one-liner, but I'd strongly suggest expanding these as they quickly get fudgy to work with. 任一种都可以,但我强烈建议您扩展这些功能,因为它们很快就会变得不方便使用。

This is a possible solution to achieve exactly what you wrote you expect: 这是一种可能的解决方案,可以完全实现您所期望的内容:

items = ['me', 'things', 'please']
new_items = []

for index, value in enumerate(items):
    # Prepend to every item but the first
    if i > 0:
        v = "with %s" % v

    # Append to every item
    v = "%s help " % v

    new_items.append(v)

print(new_items)  # outputs: ['me help ', 'with things help ', 'with please help ']

This appends help to every item, and prepends with to every but the first item. 这将help添加到每个项目,并with到除第一个项目之外的所有项目。

I think I figured this out. 我想我明白了。 Thanks all for your help, but this is (most simply) what I was trying to get at: 谢谢大家的帮助,但这(最简单的)是我想要得到的:

[s + ' help ' for s in  ['me '] + ['with ' + t for t in ['things', 'please']]   ]

which produces 产生

['me  help ', 'with things help ', 'with please help ']

It was just the extend method that I needed to replace with + to concatenate the list. 这只是我需要用+替换来连接列表的扩展方法。

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

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