列表a中删除在列表b中元素
It may happen, that you might have to delete some elements from your list. Below we have listed some methods to do the same:
可能会发生,您可能必须从列表中删除某些元素。 下面我们列出了一些可以做到这一点的方法:
pop( )
函数: (pop( )
function:)
This method requires the index number of the element that you want to delete. For example, if you want to delete the 5th element of the list, then just do it like:
此方法需要您要删除的元素的索引号。 例如,如果要删除列表的第5个元素,则只需执行以下操作:
>>> myList.pop(4)
Because the 4th index is the 5th element, as the index numbers start from 0
.
因为第4个索引是第5个元素,所以索引编号从0
开始。
del
关键字: (del
keyword:)
This also requires the index number.
这也需要索引号。
>>> del myList[4]
This will delete the 5th element of the list. We can combine this with slicing to delete sequence of elements together, like if we want to delete all elements from index 3 to 6, then:
这将删除列表的第5个元素。 我们可以将其与切片结合使用,以一起删除元素序列,例如,如果要删除索引3到6中的所有元素,则:
>>> del myList[3:7]
This will remove all elements from index 3 to 6.
这将从索引3到6中删除所有元素。
remove( )
函数: (remove( )
function:)
This function, instead of index number, requires the value that has to be deleted. For example,
此函数(而不是索引号)需要删除的值 。 例如,
>>> myList = ['Apple', 'Orange', 'Apple', 'Guava']
>>> myList.remove('Apple')
This will remove the first 'Apple' element from the list. Other elements with value 'Apple' won't be deleted. i.e., our list will be:
这将从列表中删除第一个“ Apple”元素。 值“ Apple”的其他元素不会被删除。 即,我们的清单将是:
>>> print (myList);
['Orange', 'Apple', 'Guava']
['橙色','苹果','番石榴']
将列表转换为字符串 (Convert a List to String)
If you want to convert your list into a string to print it as output, you can do it easily.
如果要将列表转换为字符串以将其打印为输出,则可以轻松实现。
不使用任何循环 (Without using any loop)
Here is the code, if you do not want to use for
or while
loop for coverting list to string.
如果不想使用for
或while
循环来覆盖列表到字符串,请使用for
代码。
mylist = ['butter', 'jam', 'curd']
myStr = ', '.join(mylist)
print (myStr);
butter, jam, curd
黄油,果酱,凝乳
If you have a list of int
values, then
如果您有一个int
值列表,则
mylist = [1, 11, 111]
myStr = str(mylist).strip('[]')
print (myStr);
1, 11, 11
1、11、11
使用for
循环 (Using for
loop)
If we have a list of int
values,
如果我们有一个int
值列表,
mylist = [1, 11, 111]
myStr = ''.join(str(e) for e in mylist)
print (myStr)
1, 11, 11
1、11、11
列表的更多功能 (More functions for Lists)
1. insert(int, item)
(1. insert(int, item)
)
This function is used to insert values, anywhere in the list. The first argument of the list takes the index where the items will be inserted and second is the value that has to be inserted. For example,
此函数用于在列表中的任何位置插入值。 列表的第一个参数获取将要插入项目的索引,第二个参数是必须插入的值。 例如,
>>> myList = ['Python', 'C++', 'Java', 'Ruby', 'Perl']
>>> myList.insert(1, 'JavaScript')
>>> print (myList)
['Python', 'JavaScript', 'C++', 'Java', 'Ruby', 'Perl']
['Python','JavaScript','C ++','Java','Ruby','Perl']
Notice how the value 'JavaScript' got inserted at index the 1
.
注意如何在索引1
处插入值'JavaScript' 。
2. reverse()
(2. reverse()
)
As obvious by the name, it is used to reverse the order of the elements in the list.
顾名思义,它用于反转列表中元素的顺序。
>>> myList.reverse()
3. sort()
(3. sort()
)
One of the most important functions in real life applications is sort()
. Sorting arranges all the elements of the List in ascending or descending order.
现实生活中最重要的功能之一是sort()
。 排序按升序或降序排列列表的所有元素。
>>> myList.sort()
If the list consists of numbers then this will make the sequence in ascending order, if the list consists of strings then the elements will be sorted in lexicographic ascending order.
如果列表由数字组成,则这将按升序排列序列;如果列表由字符串组成,则将按字典序升序对元素进行排序。
What if you want to sort in descending order?
如果要按降序排序怎么办?
>>> myList.sort().reverse()
This will first sort the elements in ascending order and then we have used the reverse()
function to reverse the list.
这将首先以升序对元素进行排序,然后我们使用了reverse()
函数来反转列表。
翻译自: https://www.studytonight.com/python/deleting-list-elements-list-functions
列表a中删除在列表b中元素