Python - 在循环到另一个列表时从一个列表中提取项目

时间:2012-12-03 03:55:32

标签: python for-loop pygame nested-loops

所有

如果有帮助,我可以发布我的其余代码。我有一个pygame版本的小行星,但我正在使用文本文件中的单词直方图。我正在创建一个字典[hist],其中单词为keys,出现次数为值。

当我创建单词实例时,我想使用单词和值。我已经尝试在for w in range(len(hist))循环中粘贴另一个循环,现在递归,但是使用递归我没有得到任何值,而对于另一个循环,我只得到每个w实例的最后一个键和值。

class Word(object):
    def __init__(self, color, pos, radius, speed, heading, term, font, screen):
        self.color = color
        self.x = pos[0]
        self.y = pos[1]
        self.radius = radius
        self.speed = speed
        self.heading = heading
        self.term = term
        self.font = font
        self.screen = screen

    def draw(self, surface):
        pygame.draw.circle(surface, self.color, (int(self.x),int(self.y)), self.radius)
        hist_text = self.font.render(self.term, True, (0, 128, 0))
        self.screen.blit(hist_text, (int(self.x) - hist_text.get_width() // 2, int(self.y) - hist_text.get_height() // 2))

    def move(self):
        self.x += self.speed * math.cos(self.heading)
        self.y += self.speed * math.sin(self.heading)

    def checkBounds(self):
        """ wrap around screen """

        if self.x > width:
            self.x = 0
        if self.x < 0:
            self.x = width
        if self.y > height:
            self.y = 0
        if self.y < 0:
            self.y = height

def first(lst):
    if lst == []:
        return 0
    else:
        print(lst[0])
        return first(lst[1:])   

for ele in hist:
    term_keys = hist.keys()
    sizes = hist.values()

for w in range(len(hist)):
    color = (220,0,0)
    radius = 33
    for ele in sizes:
        radius = 3 * ele
        font = pygame.font.Font(None, 33 * int(ele))
    print radius
    x = 0
    y = 0
    speed = random.randint(1,5)
    heading = random.uniform(0, math.pi*2)
    for ele in term_keys:
        term = ele
    screen = screen
    w = Word(color, (x, y), radius, speed, heading, term, font, screen)
    words.append(w)

2 个答案:

答案 0 :(得分:0)

像许多评论者一样,我真的不清楚你想要什么。如果您要查看的是创建一个用于计算单词出现次数的分类hist,请在文件中使用collections.Counter。一个例子:

>>> t = '''All,
...
... I can post the rest of my code if that helps. I've got a pygame version of A
steroids, but I'm using a histogram of words from a text file. I'm creating a di
ctionary [hist] with the words as keys, and the number of occurrences as values.

...
... When I create my word instances, I want to use the word and the value. I've
tried sticking another loop in the for w in range(len(hist)) loop, and now recur
sion, but with recursion I get no values and with another loop I get only the la
st key and value put in for each instance of w.'''
>>> t = t.split()
>>> collections.Counter(t)
Counter({'the': 7, 'and': 5, 'I': 5, 'of': 5, 'a': 4, 'in': 3, 'with': 3, 'anoth
er': 2, 'as': 2, 'for': 2, "I've": 2, 'get': 2, 'but': 2, 'words': 2, "I'm": 2,
'word': 2, 'my': 2, 'loop': 2, 'Asteroids,': 1, 'code': 1, 'text': 1, 'each': 1,
 'When': 1, 'number': 1, 'rest': 1, 'key': 1, 'put': 1, 'occurrences': 1, 'want'
: 1, 'post': 1, 'if': 1, 'instance': 1, 'use': 1, 'from': 1, 'recursion,': 1, 'v
alue.': 1, 'create': 1, 'loop,': 1, 'file.': 1, 'range(len(hist))': 1, 'to': 1,
'only': 1, 'version': 1, 'pygame': 1, 'w.': 1, 'helps.': 1, 'got': 1, 'tried': 1
, 'creating': 1, 'dictionary': 1, 'that': 1, 'All,': 1, 'keys,': 1, 'histogram':
 1, 'value': 1, '[hist]': 1, 'using': 1, 'now': 1, 'last': 1, 'no': 1, 'instance
s,': 1, 'values': 1, 'can': 1, 'w': 1, 'recursion': 1, 'sticking': 1, 'values.':
 1})

答案 1 :(得分:0)

呃,太笨了。我通过添加计数来实现它

count = 0
for w in range(len(hist)):
    color = (220,0,0)
    x = 0
    y = 0
    size = sizes[count]
    term = term_keys[count]
    speed = random.randint(1,5)
    heading = random.uniform(0, math.pi*2)
    screen = screen
    radius = 5 * size
    font = pygame.font.Font(None, int(size) * 5)
    print radius
    print term
    w = Word(color, (x, y), radius, speed, heading, term, font, screen)
    words.append(w)
    count += 1