Python:如何将所有可能的组合并入两个列表中?

时间:2019-11-29 07:20:30

标签: python concatenation

list1=['A','B']

list2=[1,2,3]

如何做到这一点,以使输出按此顺序显示所有可能的组合,并且没有任何括号?

A1

A2

A3

B1

B2

B3

3 个答案:

答案 0 :(得分:1)

您可以使用itertools.product

from itertools import product
list1 = ['A', 'B']
list2 = [1, 2, 3]
for e1,e2 in product(list1, list2):
    print(e1+str(e2))

输出:

A1
A2
A3
B1
B2
B3

答案 1 :(得分:0)

您可以使用:

list3 = [a+b for a in list1 for b in map(str, list2)]
print(*list3, sep = "\n") 
A1
A2
A3
B1
B2
B3

答案 2 :(得分:0)

您可以使用:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
   <form>
  <div class="form-group row">
    <label for="staticEmail" class="col-sm-2 col-2 col-form-label">Email</label>
    <div class="col-sm-10 col-10">
      <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="email@example.com">
    </div>
  </div>
  <div class="form-group row">
    <label for="inputPassword" class="col-sm-2 col-2 col-form-label">Password</label>
    <div class="col-sm-10 col-10">
      <input type="password" class="form-control" id="inputPassword" placeholder="Password">
    </div>
  </div>
</form>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>