'use function to the items in list
originally, beaker is instance and the function below is possible.
beaker.replace(z=10)
I made a list of beakers
bk = [beaker1, beaker2, beaker3]
and I want all beaker to give function 'replace' like
beaker1.replace(z=10) beaker2.replace(z=10) beaker3.replace(z=10)
so I made it brief but it does not work
bk = [beaker1, beaker2, beaker3]
for item in bk :
item.replace(z=10)
but it says
item.replace(z=10)
AttributeError: 'list' object has no attribute 'replace'
Solution 1:[1]
As it said, list
has no attribute replace
, but string
has
bk = [beaker1, beaker2, beaker3]
bk = [10 if i == beaker1 else i for i in bk]
You don't have to write z = 10
as in Java or other languages. Only when you do want to bind 10
to z
. Instead you can do it like this.
Solution 2:[2]
Have you tried printing out item
that you are looping to be sure? It said that item
is a list like:
bk = [beaker1, beaker2, beaker3]
for item in bk :
print(item)
print(type(item))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Stephen Ostermiller |
Solution 2 | Javad Nikbakht |