Assign the result of a loop to a variable in Python

前端 未结 6 1813
予麋鹿
予麋鹿 2021-01-20 11:48

Consider a list I want to parse using a for :

friends = [\"Joe\", \"Zoe\", \"Brad\", \"Angelina\", \"Zuki\", \"Thandi\", \"Paris\"]
for i in fri         


        
6条回答
  •  逝去的感伤
    2021-01-20 12:18

    I would use a dictionary instead, as I too spent a while looking into this, and determined that a dictionary would be easy enough.

        friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
        dict= {}
        for i in friends:
           dict[i] = i
    
        print(dict)
        print(dict['Zuki'])
        dict['Zuki'] = "Tim Smith"
    
        print(dict['Zuki'])
    

    The Other option would be to just call the number:

        print(friends[0])
    

    As for automatic assignment I haven't found a way to do it.

提交回复
热议问题