[英]Concatenate each string element of one list to all elements of another list
year = ['2019', '2020', '2021', '2022']
week = [str(i).zfill(2) for i in range(1,53)]
I'm trying to join/concatenate each string element of year list to all elements of week list as follows我正在尝试将年份列表的每个字符串元素加入/连接到周列表的所有元素,如下所示
['201901'
'201902'
'201903'
.
.
.
.
.
'202250'
'202251'
'202252']
I can achieve this using following code我可以使用以下代码实现这一点
week = [str(i).zfill(2) for i in range(1,53)]
year = [str(i) for i in range(2019,2023)]*len(range(2019,2023))*52
year.sort()
print({i:year.count(i) for i in year})
li = []
for yr in set(year):
li.append([i + j for i, j in zip([k for k in year if yr in k], week)])
yr_week_id = sorted(list(np.concatenate(li)))
print(yr_week_id)
Is there any simpler solution to this problem?这个问题有没有更简单的解决方案?
You don't really need the week list.你真的不需要周列表。 You can just calculate the week numbers as you go along.
您可以像 go 一样计算周数。
You may not even need the year list if the range of years can be expressed with range()如果年份范围可以用range()表示,您甚至可能不需要年份列表
year = ['2019', '2020', '2021', '2022']
output = []
for yy in year:
output.extend([f'{yy}{ww:02d}' for ww in range(1, 53)])
print(output)
Output: Output:
['201901',
'201902',
...
'202251',
'202252']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.