Home > Blockchain >  use function to the items in list
use function to the items in list

Time:05-06

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=tg_h-ra_BH gr_th/2)
AttributeError: 'list' object has no attribute 'replace'

CodePudding user response:

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))

CodePudding user response:

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]

Ahhhh, you dont have to write z=10 as java or other languages. Only when you do wanna bing 10 to z you can do like this

  • Related