redaiconglin 发表于 2022-4-27 17:41:29

请教各位老师

我的代码是这样
a =['b','c']
b =

j = []
def s():
    for i in a:
      for each in b:
          j.append(str(each)+i)
    return j
if __name__=='__main__':
    d =
    h =[]
    for g in d:
      h.extend(s())
    print(h)
能达到的效果是这样
['1b', '2b', '3b', '4b', '56b', '1c', '2c', '3c', '4c', '56c', '1b', '2b', '3b', '4b', '56b', '1c', '2c', '3c', '4c', '56c', '1b', '2b', '3b', '4b', '56b', '1c', '2c', '3c', '4c', '56c']
如何做才能达到这样的效果
['1b', '2b', '3b', '4b', '56b', '1b', '2b', '3b', '4b', '56b', '1b', '2b', '3b', '4b', '56b', '1c', '2c', '3c', '4c', '56c', '1c', '2c', '3c', '4c', '56c', '1c', '2c', '3c', '4c', '56c']
请各位老师帮帮我谢谢了

jackz007 发表于 2022-4-27 17:56:57

本帖最后由 jackz007 于 2022-4-27 18:00 编辑

def s(j , c , b):
    for _ in range(2):
      for each in b:
            j . append(str(each) + c)

if __name__=='__main__':
    a , b , h = ['b','c'] , , []
    for x in a:
      s(h , x , b)
    print(h)

isdkz 发表于 2022-4-27 17:57:34

a =['b','c']
b =

j = []
def s(count=1):
    for i in a:
      for each in count * b:
          j.append(str(each)+i)
    return j
if __name__=='__main__':
    h = s(3)
    print(h)

isdkz 发表于 2022-4-27 18:05:05

a, b, s = ['b','c'], , lambda count=1:
print(s(3))

redaiconglin 发表于 2022-4-27 19:29:17

isdkz 发表于 2022-4-27 18:05


感谢老师的讲解。谢谢

redaiconglin 发表于 2022-4-27 19:29:52

jackz007 发表于 2022-4-27 17:56


感谢老师的讲解谢谢了
页: [1]
查看完整版本: 请教各位老师