【问题标题】:How do I get a value randomly from a list in Jython script?如何从 Jython 脚本中的列表中随机获取一个值?
【发布时间】:2012-09-11 12:44:50
【问题描述】:
lists = portTest.lists(arg1, arg2) 

// this returns the lists from webservice in java  
//  public String[] list1; 
//  public String[] list2;public String[] list3;

// i want to get the random element in the list,
// not the first, second or any selected item. 
elementinthelist = lists.list1[0]

如何从列表中生成随机元素

我正在用 Jython 编写测试脚本。我正在使用 Grinder 工具调用服务

【问题讨论】:

    标签: java python jython grinder


    【解决方案1】:

    在 Python 中,使用 random.choice:

    import random
    elementinthelist = random.choice(lists.list1)
    

    【讨论】:

      【解决方案2】:

      在 0(包括)和列表长度(不包括)之间选择一个随机整数。

      在普通的 Java 中,这看起来像

      Random rand = new Random();
      int randIx = rand.nextInt(lists.size());
      Object element = lists.get(randIx);
      

      【讨论】:

      • 谢谢,但我在 jython 中需要它,我需要选择第一个带有随机元素的列表。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-04
      • 2014-09-22
      相关资源
      最近更新 更多