【问题标题】:pull out element data random range from list从列表中提取元素数据随机范围
【发布时间】:2013-12-17 14:44:57
【问题描述】:

我想提取列表元素数量随机范围(从 5 到 15)。 例如, 我有包含以下字符串的文本

locasari2
jyprin
a0007a
hdki22
binarykorea
onlineforum
vobank1
chsb4322
gaiber62
wjun2104
inaekkim
zcbm22
happy_sex
ckdgns0524
lhe0925
chong4787
multy26
ver_test
danaecco
paoo
kea2209
ybyng234
smrush
kimksh2596
winproto
cs8489
aek5262
bktan12
puripink04
qkdlfjf99
nyj4154
joara5778
keepro
sswpsh72
tom770303
ckdanrl0757
himart26
lco3924
heloword
jking15

我想从 5 到 15 随机提取一个元素列表,所以我想得到什么结果 是,

locasari2,jyprin,a0007a,hdki22,binarykorea
onlineforum,vobank1,chsb4322,gaiber62,wjun2104,inaekkim,zcbm22
ckdgns0524,lhe0925,chong4787,multy26,ver_test,danaecco
paoo,kea2209,ybyng234,smrush,kimksh2596,winproto,cs8489,aek5262,bktan12,puripink04,qkdlfjf99,nyj4154
joara5778,keepro,sswpsh72,tom770303,ckdanrl0757,himart26,lco3924,heloword

这是我到目前为止所做的,但问题之一是有时可以正确提取数量元素,但有时会提取超过 15 个单词的数据

out = ''
handle = open('fx -01.txt').read()

for i, line in enumerate(handle.split('\n')):
    out +=  line + ','
    rndind = random.randint(5,15)
    if (i + 1) %  rndind == 0 :
    out = out.split(',')
    print len(out)
    print out
    out = ''

对不起,我的英语不好。

【问题讨论】:

    标签: python list random


    【解决方案1】:

    使用itertools.islice,您不需要读取整个文件。

    import itertools
    import random
    
    with open('fx-01.txt') as f:
        while True:
            n = random.randint(5, 15)
            elements = [line.strip() for line in itertools.islice(f, n)]
            # itertools.islice(f, n): to fetch `n` lines from file.
            if not elements:
            # if len(elements) < 5: # Use this if you want drop trailing <5 lines.
                break
            print(','.join(elements))
    

    【讨论】:

      【解决方案2】:

      您想使用上下文管理器。

      要遍历这个,假设您正在处理一个小的结果列表(尽管文件可能很大),并且选择列表的内存不是问题,我们将首先收集列表中的项目然后循环随机选择其中的项目。

      import random
      choice_list = []
      with open('fx -01.txt', 'rU') as handle:
          first = 5
          last = 15
          for i in xrange(last):
              selection = handle.next()
              if first <= i <= last:
                  choice_list.append(selection)
      
      number_of_random_choices = 10
      for i in xrange(number_of_random_choices):
          print random.choice(choice_list)
      

      上下文管理器确保您在出现错误时自动关闭文件,因此您不会将文件锁定打开。

      open 为您提供了一个迭代器,模式标志中的U(用于通用)确保您在换行符上进行拆分,无论您的平台、Windows 还是 Unix。

      然后我们避免将整个文件甚至迭代器具体化到内存中的列表中,而只选择我们想要从中进行随机选择的列表。演示循环通过它 10 次。每个选择都将独立于之前的选择,这意味着每个选择都可以选择多次。

      将它们全部放在随机位置,即随机将它们随机放置:

      random.shuffle(choice_list)
      print choice_list
      

      会给你一个随机洗牌的列表。

      【讨论】:

      • 如果我运行一次,上面的代码运行良好,但我想循环,所以我在下面进行了修改,但遇到了一些错误“AttributeError:'str'对象没有属性'next'”句柄=打开( 'fx -01.txt').read() for line in handle: for i in xrange(random.randint(5,15)): random_selection = handle.next() print random_selection
      猜你喜欢
      • 1970-01-01
      • 2015-09-22
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2018-05-15
      • 1970-01-01
      相关资源
      最近更新 更多