[英]how to append a list in python with for loop
I want to append my_list 我想附加my_list
list2 = ['1','2','3','4','5']
my_list = []
for i in list2:
my_list.append(i)
print(my_list)
This will put the list2 to my_list. 这会将list2放入my_list。
The result is 结果是
['1', '2', '3', '4', '5']
but i want only the value '2' and '5' ) 但我只想要值'2'和'5')
So something like this: 所以像这样:
[ '2', '5']
Have tried 试过
for i in list2:
my_list.append(i[1:4])
Any Idea? 任何想法?
仅将一种条件与list comprehension
结合使用:
my_list = [item for item in list2 if item == '2' or item == '5']
It depends how are deciding what elements of list2 should be added to my_list, which you have not mentioned: 这取决于如何确定应将list2的哪些元素添加到my_list中,而您没有提到:
Right now you can do what @MihaiAlexandru-Ionut suggested. 现在,您可以按照@ MihaiAlexandru-Ionut的建议进行操作。
or: 要么:
list2 = ['1','2','3','4','5']
my_list = []
my_list.append(list2[1])
my_list.append(list2[4])
print(my_list)
# or
my_list = []
my_list = [list2[1], list2[4], ]
print(my_list)
Here's a short way of doing it. 这是一个简短的方法。 But beware though it will break if there are repeating elements in the list.
但是请注意,如果列表中有重复的元素,它将中断。
list2 = ['1','2','3','4','5']
my_list = []
want = ['2', '5']
my_list = [list2[list2.index(i)] for i in list2 for item in want if i == item] # will fail if elements are not unique.
the last line is equivalent to this 最后一行与此等效
my_list = [item for i in list2 for item in want if i == item] # much better than using index method.
And here's the expanded form. 这是扩展形式。
list2 = ['1','2','3','4','5']
my_list = []
want = ['2', '5']
for i in list2:
for item in want:
if i == item:
my_list.append(list2[list2.index(i)])
#my_list.append(item)
print(my_list)
May be like that 可能是这样
list2 = ['1','2','3','4','5']
target_idexes = [2, 5]
my_list = []
for i in list2:
my_list.append(i) if int(i) in target_idexes else 0
print(my_list) # ['2', '5']
or if in list2 not only digits: 或如果在list2中不仅只有数字:
list2 = ['1','2','3','4','5']
target_idexes = [2, 5]
my_list = []
for i in list2:
my_list.append(i) if list2.index(i) in target_idexes else 0
print(my_list) # ['3'] because indexing start from 0 and 5 is out of range
The easiest and fastest way is to use an conditional statement for the specific value that you are searching inside the loop. 最简单,最快的方法是对循环中要搜索的特定值使用条件语句。
if i == 2 or i == 5:
new_list.append(i)
The drawback of this approach is, that if you need to expand the range of values that you want retrieve you will need to write a longest condition if i == 1 or i == 5 ... or i == N:
, that's not only bad to see but a bad programming practice because the code is hard to mantain. 这种方法的缺点是,如果需要扩展要检索的值的范围,
if i == 1 or i == 5 ... or i == N:
,需要写最长的条件,即因为很难维护代码,所以不仅看到不好,而且编程习惯也很差。
A better way is to have a list with the values that you want retrieve and check if the actual element is this list before adding it to the new list. 更好的方法是使用一个列表,其中包含要检索的值,并在将实际元素添加到新列表之前检查该元素是否为该列表。
list2 = ['1','2','3','4','5']
wanted = ['2','5'] #what I search
my_list = []
for value in list2:
if value in wanted: #when value is what a I want, append it
my_list.append(value)
However if you want to add the elements by their position, not to find every occurrences of a specific value, you can use a list of integers and loop over it to add the wanted elements. 但是,如果要按元素的位置添加元素,而不是查找每个出现的特定值,则可以使用整数列表并对其进行循环以添加所需的元素。
positions = [1,4] #indicates the positions from which I want to retrieve elements
new_list = [list[p] for p in positions] #using list comprehension for brevity
Last thing that I would like to add is that in python you can't execute 我要添加的最后一件事是在python中无法执行
my_list.append(i[0,4])
because python when looks at [0,4], will interpret it like you are passing a tuple ( because of the comma ) and will rise the following error TypeError: list indices must be integers or slices, not tuple
. 因为python在查看[0,4]时,将像传递元组一样解释它( 由于逗号 ),并且会引发以下错误
TypeError: list indices must be integers or slices, not tuple
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.