我正在尝试将名称追加到具有空字符串和空列表的空列表中。我想使用for循环或能够通过friendsNum列表的循环进行迭代,并在字符串括号中插入一个来自peoplenames列表的随机名称,该名称是i[0],然后在空列表中的两个随机名称称为friendnames之后,该列表将在字符串后为i[1], 并一直持续到最后一个列表。
import random
friendsNum = [("",[]),("",[]),("",[]),("",[])]
peopleNames = ["Alvin","Danny","Maria","Lauren"]
friendNames = ("john","matt","will","mario","wilma","shannon","mary","jordan")
newList = friendsNum
tempName = ()
temp = ()
for i in friendsNum:
tempName = random.sample(peopleNames,1)
temp = random.sample(friendNames,2)
newList = i[0].append(tempName)
newList = i[1].append(temp)
在此 for 循环迭代之后,它将如下所示。
friendsNum = [("Johnny",["john","matt"]),
("Zach",["wilma","shannon"]),
("Dawn",["mary","jordan"]),
("Max",["will","john"])]
我不断收到无法从行追加字符串对象的错误
newList = i[0].append(tempName)
newList = i[1].append(temp)
我是否错误地处理了我应该使用的循环?
下面的错误消息
newList = i[0].append(tempName)
AttributeError: 'str' object has no attribute 'append'
海绵宝宝撒
绝地无双
相关分类