How can I do assignments in a list comprehension?

2020-01-25 01:04发布

I want to use the assignment operator in a list comprehension. How can I do that?

The following code is invalid syntax. I mean to set lst[0] to an empty string '' if it matches pattern:

[ lst[0] = '' for pattern in start_pattern if lst[0] == pattern ]

Thanks!

6条回答
ら.Afraid
2楼-- · 2020-01-25 01:35

The Python language has distinct concepts for expressions and statements.

Assignment is a statement even if the syntax sometimes tricks you into thinking it's an expression (e.g. a=b=99 works but is a special syntax case and doesn't mean that the b=99 is an expression like it is for example in C).

List comprehensions are instead expressions because they return a value, in a sense the loop they perform is an incident and the main point is the returned list.

A statement can contain expressions but an expression cannot contain statements.

That said however list item assigment to is internally converted to a method call (to allow creation of list-like objects) and method calls are expressions. Therefore you can technically use list item assignment in an expression:

[ lst.__setitem__(0, '') for pattern in start_pattern if lst[0] == pattern ]

This is however considered bad because it harms readability and how easy is to read source code is the main focus in the Python language. You should write instead for example...

for pattern in start_pattern:
    if lst[0] == pattern:
        lst[0] = ''

that by the way thanks to the in operator is equivalent to the even more readable

if lst[0] in start_pattern:
    lst[0] = ''

List comprehensions are used for their return value and they make a loop internally... If what you want is the loop then just write a loop... whoever will read the code trying to understand what it does would appreciate that a lot (and whoever includes yourself in a few weeks).

查看更多
Evening l夕情丶
3楼-- · 2020-01-25 01:41

It looks like you are confusing list comprehension with looping constructs in Python.

A list comprehension produces -- a list! It does not lend itself to a single assignment in an existing list. (Although you can torture the syntax to do that...)

While it isn't exactly clear what you are trying to do from your code, I think it is more similar to looping over the list (flow control) vs producing a list (list comprehension)

Loop over the list like this:

for pattern in patterns:
   if lst[0] == pattern: lst[0]=''

That is a reasonable way to do this, and is what you would do in C, Pascal, etc. But you can also just test the list for the one value and change it:

if lst[0] in patterns: lst[0] = ''

Or, if you don't know the index:

i=lst.index[pattern]
lst[i]=''

or, if you have a list of lists and want to change each first element of each sublist:

for i, sublst in enumerate(lst):
    if sublst[i][0] in patterns: sublist[i][0]=''

etc, etc, etc.

If you want to apply something to each element of a list, then you can look at using a list comprehension, or map, or one of the many other tools in the Python kit.

Personally, I usually tend to use list comprehensions more for list creation:

 l=[[ x for x in range(5) ] for y in range(4)]  #init a list of lists...

Which is more natural than:

l=[]
for i in range(4):
   l.append([])
   for j in range(5):
      l[i].append(j)      

But to modify that same list of lists, which is more understandable?

This:

l=[['new value' if j==0 else l[i][j] for j in range(len(l[i]))] for i in range(len(l))]

or this:

for i,outter in enumerate(l):
    l[i][0]='new value'               

YMMV

Here is a great tutorial on this.

查看更多
我命由我不由天
4楼-- · 2020-01-25 01:50

In short: you don't. List comprehensions are for generating lists, not modifying existing lists. If you want to modify a list, use a for loop, as that's what they're for.

The Pythonic way to write that code would be something like:

for pattern in start_pattern:
    if lst[0] == pattern:
        lst[0] = ''
        #the following assumes that pattern will never be ''
        #if that's possible, you can ignore this bit
        break

However, if you really, really want to do assignment inside one, and don't mind every Python programmer who ever deals with your code hating it for all eternity, there are a few functions you can use:

  • If the variable you want to assign to is a global, then you can do

        globals().update(var=value)
    
  • If the variable you want to assign to is a mutable sequence or a map (such as a list or a dict)

        list.__setitem__(index, value)
    
查看更多
家丑人穷心不美
5楼-- · 2020-01-25 01:58

Python 3.8 will introduce Assignment Expressions.

It is a new symbol: := that allows assignment in (among other things) comprehensions.

It will introduce a lot of potential savings w.r.t. computation/memory, as can be seen from the following snippet of the above linked PEP (formatting adapted for SO):

Syntax and semantics

In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.

The value of such a named expression is the same as the incorporated expression, with the additional side-effect that the target is assigned that value:

  1. Handle a matched regex

    if (match := pattern.search(data)) is not None:
        # Do something with match
    
  2. A loop that can't be trivially rewritten using 2-arg iter()

    while chunk := file.read(8192):
        process(chunk)
    
  3. Reuse a value that's expensive to compute

    [y := f(x), y**2, y**3]
    
  4. Share a subexpression between a comprehension filter clause and its output

    filtered_data = [y for x in data if (y := f(x)) is not None]
    

This is already available in the recently releases alpha version (not recommended for production systems!). You can find the release schedule for Python 3.8 here.

查看更多
看我几分像从前
6楼-- · 2020-01-25 01:58

If what you ment in the question is:

[ lst[0] = '' for lst in listOfLists if lst[0] == pattern ]

or for a list of patterns

[ lst[0] = '' for lst in listOfLists if lst[0] in patterns ]

This can actually be done easily

[ [''] + lst[1:] for lst in listOfLists if lst[0] == pattern ]

or for a list of patterns again

[[''] + lst[1:] for lst in listOfLists if lst[0] in patterns ]
查看更多
老娘就宠你
7楼-- · 2020-01-25 02:01

Maybe it isn't exactly what you're looking for, but I believe that it is worth to present this scenario.

Suppose that you have a list of dictionaries, like this:

>>> fruits
[{'name': 'apple', 'quantity': 5}, {'name': 'banana', 'quantity': 4}]

With a normal list comprehension, you might find the object that you're looking for:

>>> [d for d in fruits if d['name'] == 'apple'] 
[{'name': 'apple', 'quantity': 5}]

As a result, you have a list with a single element, thanks to the if condition above.

Therefore, you can index the only element, accessing one of the dictionary keys and assigning a value:

>>> [d for d in fruits if d['name'] == 'apple'][0]['quantity'] += 1

Here the result:

>>> fruits
[{'name': 'apple', 'quantity': 6}, {'name': 'banana', 'quantity': 4}]
查看更多
登录 后发表回答