生成器确保列表中两个项目之间的最小距离

时间:2014-04-13 14:55:53

标签: python generator

我试图创建一个获取文件内容的脚本,将其存储到容器中,然后伪随机地从容器中取一行。

该文件包含歌曲歌词,主题标签和链接,以/ t分隔,但我试图使代码对任何更改都有弹性 - 在行中添加或删除元素。

问题在于伪随机生成器,我无耻地复制,并且不太懂。 我知道问题是矩阵不是可以清洗的类型,而在生成器中我使用set()来散列它。 如何修复矩阵的生成器代码?据说,它应该随机选择一行,但避免再次选择相同的行。

这是代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import random, collections, time

# Pseudo-random generator
def choice_gen(choices, min_dist):
    last_choices = collections.deque(maxlen=min_dist)
    choices = set(choices)
    while 1:
        c = random.choice(list(choices - set(last_choices)))
        last_choices.append(c)
        yield c

# Organizes the contents of the file in matrix 
# <Song lyric> <hashtag> <link>
songs_table = []
with open("songs.txt") as f:
    for txtline in f:
        song_data= txtline.split('\t')
        songs_table.append(song_data)

# Prints a pseudo-random row of the matrix
for song_data in choice_gen(songs_table,2):
    print "{}".format(song_list)
    time.sleep(2)

# With dictionary, only 2 values per song though,
# the script runs without issues here
# <Lyric> <hashtag>
"""     
song_dict = {}
with open("songs.txt") as f:
    for txtline in f:
        (key, val) = txtline.split('\t')
        song_dict[key] = val

for line in choice_gen(song_dict.items(),2):
        print "{}".format(line)
        time.sleep(2)
"""

1 个答案:

答案 0 :(得分:1)

list个对象是可变的,因此不可清除。使用不可变的元组,因此可以使用:

songs_table = []
with open("songs.txt") as f:
    for txtline in f:
        song_data= tuple(txtline.split('\t'))
        songs_table.append(song_data)

快速演示:

>>> some_nested_list = [['foo', 'bar', 'baz'], ['spam', 'ham', 'eggs']]
>>> set(some_nested_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> set(tuple(el) for el in some_nested_list)
set([('foo', 'bar', 'baz'), ('spam', 'ham', 'eggs')])