所以我在Python中有两个列表:
import random
list_1 = ['1','2','3']
list_2 = ['4','5','6']
num = random.choice(list_1 or list_2)
这似乎不起作用。如何从列表1或列表2中获得随机数?
答案 0 :(得分:1)
您可以串联列表:
cs = 3
或选择一个列表,然后选择一个字符:
'"C:\your_path\Northwind.mdb"
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private bindingSource1 As New BindingSource()
Private dataAdapter As New OleDbDataAdapter()
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your_path\Northwind.mdb"
Dim selectCommand As String
Dim connection As New OleDbConnection(connectionString)
selectCommand = "Select * From MyExcelTable ORDER BY ID"
Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)
With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
End With
Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Dim data As New DataSet()
data.Locale = System.Globalization.CultureInfo.InvariantCulture
DataGridView1.DataSource = Me.bindingSource1
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, btnUpdate.Click
Dim table As New DataTable()
Me.bindingSource1 = Me.DataGridView1.DataSource
table = Me.bindingSource1.DataSource
Me.dataAdapter.Update(table)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click, btnClose.Click
Me.Close()
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged, TextBox1.Click
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\your_path\Northwind.mdb"
Dim selectCommand As String
Dim connection As New OleDbConnection(connectionString)
'selectCommand = "Select * From MyExcelTable where Fname = '" & TextBox1.Text & "'"
'"SELECT * FROM Customers WHERE Address LIKE '" & strAddressSearch & "%'"
'or ending with:
'"SELECT * FROM Customers WHERE Address LIKE '%" & strAddressSearch & "'"
selectCommand = "Select * From MyExcelTable where Fname Like '%" & TextBox1.Text & "%'"
Me.dataAdapter = New OleDbDataAdapter(selectCommand, connection)
With DataGridView1
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader
End With
Dim commandBuilder As New OleDbCommandBuilder(Me.dataAdapter)
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.bindingSource1.DataSource = table
Dim data As New DataSet()
data.Locale = System.Globalization.CultureInfo.InvariantCulture
DataGridView1.DataSource = Me.bindingSource1
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Aqua
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
End Class
答案 1 :(得分:0)
简短,但可以作为参考...
import random
name = ['I love ','I have ','I hate ','I want ','I buy ','I like ','I see ']
second = ['banana','lemon','water','cat','soap','man','shopping','pen','mouse']
population = list(zip(name, second))
ox_list = []
for a in range(20):
samples = random.sample(population, 1)
samples = str(samples).strip('[]')
ox_list.append(samples.replace("', '", ''))
for o in set(ox_list):
print (o.replace("')",'').replace("('",''))
I have lemon
I want cat
I love banana
I like man
I buy soap
I hate water
I see shopping
答案 2 :(得分:-1)
使用 random.sample 从两个列表中进行选择。如果您想从一个列表或另一个列表中单独选择,您可以使用 mod % 并翻转偶数和奇数,其中一个列表是偶数,一个列表是奇数,然后随机采样。
name = ['I love ','I have ','I hate ','I want ','I buy ','I like ','I see ']
second = ['banana','lemon','water','cat','soap','woman','shopping','pen','mouse']
result=[]
for i in range(10):
a=random.sample(name,1)
b=random.sample(second,1)
result.append(a[0]+ b[0])
print(result)
#[result.append(random.sample(name,1)[0]+random.sample(second,1)[0]) for i in
range(10)]
print(result)
输出:
['I buy pen', 'I buy cat', 'I like woman', 'I have water', 'I hate water', 'I want water', 'I buy water', 'I see banana', 'I love woman', 'I buy woman']
result=[]
for i in range(10):
a_num=random.sample(range(10000),1)
if a_num[0]%2:
result.append(random.sample(name,1))
else:
result.append(random.sample(second,1))
print(result)
输出:
[['mouse'], ['I see '], ['banana'], ['cat'], ['I buy '], ['soap'], ['woman'], ['I like '], ['soap'], ['cat']]