简体   繁体   English

组合 python 中的数字和字符串列表

[英]Combining list of numbers and strings in python

As an R user, I know very little about python.作为一个R用户,我对 python 知之甚少。 I am using python moviepy to pick up a long list of photos to generate a video in RStudio notebook .我正在使用python moviepy来挑选一长串照片以在RStudio notebook中生成视频。 What I did previously was to use R to generate the list of photos.我之前所做的是使用R来生成照片列表。

R code: R 代码:

v_list <- c(paste0("v_", c(1:10, rep(10, 5), rep(11, 10)), ".jpg"))

and then in python chunk to convert this list to python, v_list = r.v_list .然后在python块中将此列表转换为 python, v_list = r.v_list

I wonder if there is an easy way to generate the list directly in python.我想知道是否有一种简单的方法可以直接在 python 中生成列表。 It appears there are many questions on this topic.看来这个话题有很多问题。 Through those answers, I managed to produce the following code:通过这些答案,我设法生成了以下代码:

Python: Python:

v_list = ["v_" + str(x) + ".jpg" for x in range(1, 10)]+["v_" + str(x) + ".jpg" for x in [10]*5] + ["v_" + str(x) + ".jpg" for x in [11]*10]

My question: is it possible to make this code simpler?我的问题:是否有可能使这段代码更简单?

How about怎么样

v_list = [f"v_{x}.jpg" for x in list(range(1, 10)) + [10] * 5 + [11] * 10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM