在列表中生成一个字典

时间:2015-11-06 14:19:45

标签: python-2.7

所以这是我的问题,我有一个由程序随机生成的字符列表。 我希望每个角色对彼此都有感情。

到目前为止,这是我的代码:

import random

# create characters

class CHARACTER:
    def __init__(self, name, surname, age, sexe, pragmatism,
        courage, intelligence, shyness, happiness, fear, hunger,
        comfort, feeling):
        self.name = name
        self.surname = surname
        self.age = age
        self.sexe = sexe
        self.pragmatism = pragmatism
        self.courage = courage
        self.intelligence = intelligence
        self.shyness = shyness
        self.happiness = happiness
        self.fear = fear
        self.hunger = hunger
        self.comfort = comfort
        self.feeling = feeling


characters = [] # list that will contain all the characters
feelingslist = []
sexe = ["Male", "Female"]

mname = ["John", "Mike", "Albert", "Henry", "Patrick", "Francis",
"Robert", "Simon", "Charles", "Charlie", "Connor", "Adam", "Blake", 
"Steven", "Edward", "Andrew", "Joe", "Gregory", "Brian", "Anthony", 
"Frank", "Billy", "Boris", "Edgar", "Elliott", "Erik", "Liam", "Kyron", 
"Ned", "Neil", "Ricky", "Ross", "Rich", "Roy", "Preston", "William", 
"Vladimir", "Zach", "Wyatt", "Tylor", "Thomas", "Spike"]

fname = ["Alessia", "Ally", "Bridgette", "Callie", "Jessica", 
"Debora", "Diana", "Elizabeth", "Clair", "Fran", "Hannah", 
"Helene", "Marie", "Laura", "Leslie", "Leyla", "Kiley", 
"Margaret", "Lola", "Maryjane", "Megan", "Rose", "Sofia", 
"Samantha", "Teresa", "Yolanda", "Teri", "Vicky", "Tricia", 
"Rose", "Rita", "Nita", "Paola", "Penelope", "Polly", 
"Nathalie", "Melody", "Morgane"]

lname = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Miller", 
"Moore", "Taylor", "Thomas", "White", "Harris", "Thompson", "Garcia", 
"Allen", "Lewis", "Hall", "Young", "Clark", "Hill", "Lopez", "Carter", 
"Turner", "Collins", "Evans", "Campbell", "Nelson", "Parker", "Green", 
"Black", "Green", "Lee", "Martin"]


# the code below create 10 male and 10 female characters
# and puts them in the characters list.


for i in range(20):
    character = CHARACTER(random.choice(mname),
    random.choice(lname), random.randint(1,10), 
    random.choice(sexe), random.randint(1,10), random.randint(1,10),
    random.randint(1,10), random.randint(1,10),
    random.randint(1,10), random.randint(1,10),
    random.randint(1,10), random.randint(1,10), {})

    characters.append(character)
    i += 1

# assigning sexe to characters

for character in characters:
    if character.sexe == "Female":
        character.name = random.choice(fname)

# feelings

for character in characters:
    for character in characters:
        character.feeling[id(character)] = 5

# for each character in characters:
# add one feeling for each character

# printing characters

for character in characters:
    print character.name
    print character.surname
    print character.sexe
    print id(character)
    print character.feeling
    print " "

现在我的问题是程序只为每个字符添加一个条目:

莫嘉娜 加西亚 女 44880880 {44880880:5}

所以每个角色都有一个感觉词典和#34;为自己而不是为其他人。 我希望每个词典在程序中为每个其他角色都有一个条目(所以每个角色对所有其他角色都有意见)

3 个答案:

答案 0 :(得分:1)

在设置感受的循环中,您使用相同的变量(character)来控制两个循环;它们需要是不同的变量。

例如:

for char1 in characters:
    for char2 in characters:
        char1.feeling[id(char2)] = 5

答案 1 :(得分:0)

好吧我终于解决了这个问题,以防万一有相同问题的人在这里读到这个主题是我的解决方案:

characters_len = len(characters)

for character in characters:
    for i in range(characters_len):
        character.feeling[id(characters[i])] = 5

感谢您的回答并让我走上正确的道路

答案 2 :(得分:-1)

所以我应该这样做:

characters_len = len(characters)

for character in characters:
    for i in range(characters_len):
        character.feeling[id(character)] = 5
        i+=1