Py学习  »  Python

用for in语句替换列表中的项(Python)

vae • 5 年前 • 1448 次点击  

我是一个新的Python用户,我想知道如何用str变量替换列表中的每个整数?

my code (what is wrong here?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56197
 
1448 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Jordan Brière
Reply   •   1 楼
Jordan Brière    5 年前

我想你想要这样的东西:

int_list = [2, 33, 4, 55, 5, 56, 34, 6, 6, 76, 78657, 234, 3, 4]
New = []

for N in int_list:
    New.append(str(N) + ('small' if N < 30 else 'large'))
print(New)

结果:

['2small', '33large', '4small', '55large', '5small', '56large', '34large', '6small', '6small', '76large', '78657large', '234large', '3small', '4small']