# 合葬代码: 文字组合 +编号
str = '''
今天,明天,后天
知道,不知道
女孩子,Python
'''
rows = [row for row in str.split('\n') if row]
# print(rows)
list1 = []
for row in rows:
list1.append(row.split(','))
# print(list1)
list2 = [(x, y, z) for x in list1[0] for y in list1[1] for z in list1[2]]
# print(list2)
for i in list2:
print(' '.join(i))
# 让a,b,c竖排编号(1)(2)(3)
s = '''a,b,c'''
for index, t in enumerate(s.split(',')):
print(f"({index+1}).{t}")
# print 打印
'''
(1). a
(2). b
(3). c
'''

这家里段文字组合和编号①②③的代码,怎么让他们合在一起?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 梦幻精灵_cq 2022-11-24 00:01关注
用三层for嵌套,可解决您的问题。
代码
text = ''' 今天,明天,后天 知道,不知道 女孩子,Python ''' a, b, c = map(lambda x: x.split(','), text[1:-1].split('\n')) k = 1 temp = [] for person in c: for speak in b: for day in a: s = f"{day}{speak}{person}" print(f"({k}) {s}") temp.append(s) k += 1 temp.sort(key=lambda x: x[:2]) print() for k,i in enumerate(temp): print(f"{k+1}. {i}")
代码运行效果截屏
前为直接打印,后为列表排序(自定义打印规则)后打开。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用