在这篇文章中,我们将为您详细介绍Python:如何更改列表的每个子列表中每个元素的数据类型在这种情况下:int到str的内容。此外,我们还会涉及一些关于Python-删除在另一个子列表中存在的子列表、
在这篇文章中,我们将为您详细介绍Python:如何更改列表的每个子列表中每个元素的数据类型在这种情况下: int 到 str的内容。此外,我们还会涉及一些关于Python - 删除在另一个子列表中存在的子列表、Python - 根据索引更改列表中的多个元素、python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔、python – 如何在列表中创建列表,其中每个未来列表由列表中的空格分隔的知识,以帮助您更全面地了解这个主题。
本文目录一览:- Python:如何更改列表的每个子列表中每个元素的数据类型在这种情况下: int 到 str
- Python - 删除在另一个子列表中存在的子列表
- Python - 根据索引更改列表中的多个元素
- python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔
- python – 如何在列表中创建列表,其中每个未来列表由列表中的空格分隔
Python:如何更改列表的每个子列表中每个元素的数据类型在这种情况下: int 到 str
你可以试试列表理解
[[str(j) for j in i] for i in list1]
# or
# [list(map(str,i)) for i in list1]
[['1','2','3'],['4','5','6']]
,
使用嵌套列表理解
list1 = [[1,2,3],[4,5,6]]
[[str(x) for x in y] for y in list1]
会回来
[['1','6']]
,
是的,你可以像这样使用 map 函数:
def convert(x=[]):
return [str(i) for i in x]
>>list(map(convert,x))
[['1','6']]
,
请在 Python 中查看 mutable/immutable objects
for i in list1: # i is a list(mutable)
for j in i: # j is an int(immutable)
j = str(j) # j reference to new string object
int 是不可变的(创建后不允许更改),所以这一行
j = str(j)
创建了一个由 j 引用的新字符串对象,列表 i 中的对象保持不变。
list、dict、set、byte 数组都是可变对象。
可以像这样更改列表中的对象值:
for i in list1: # i is a list(mutable)
for j in range(len(i)): # this time,j is index of list i
i[j] = str(i[j]) # change the list values
结果,
list1
[['1','6']]
Python - 删除在另一个子列表中存在的子列表
Python 是一种广泛使用的软件,它有许多不同的使用目的和执行不同任务的多种功能。 python 的一个有用的功能是列表功能,它有助于收集和存储不同的数据,但很多时候用户在删除另一个子列表中已经存在的子列表时会遇到问题。因此,在本文中,我们将学习如何删除其他子列表中已存在的不同子列表。
为了清楚地理解这个问题,我们举一个例子,我们必须删除数据已经存在于另一个子列表中的子列表。
示例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] #All the sublist whose data is already present in other sublist are to be removed
输出
名称为 [Shyam,John] 和 [David,Stefan] 的子列表已在其他子列表中具有相同的数据,因此这些额外的子列表将被删除。输出应如下所示:
new_list = [[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
现在我们将了解删除子列表中已存在的子列表的不同方法。
立即学习“Python免费学习笔记(深入)”;
点击下载“修复打印机驱动工具”;
这里我们提到了不同的可能方法:
列表理解
删除其他子列表中存在的所有子列表的最简单方法是借助列表理解。检查列表中存在的所有子列表,并将那些不存在于任何其他子列表中的子列表复制到新列表中。我们举个例子来更清楚地理解:
示例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] New_list = [sublist for sublist in duplicate_list if not any(set(sublist) <= set(other) for other in duplicate_list if sublist is not other)] #We first check all the lists of the duplicate list through the any() function and then we check for any repeatation with the help of <= operator
输出
代码完成后,我们将打印上述代码的输出。上述代码的输出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
所有额外的子列表都被删除,因此我们编写了正确的代码来删除子列表中已经存在的子列表。
定义函数
解决该问题的另一种方法是创建一个全新的单独函数来过滤掉其他子列表中存在的所有子列表。这可以通过为函数定义一个条件并使其相应地运行来完成。
示例
def is_sublist(sublist, other): #is_sublist is the function defined return set(sublist) <= set(other) #the functions checks 2 sublists at a time and if the sublist already exists then it returns with `true` feedback and does not consider the extra sublist duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] new_list = [sublist for sublist in duplicate_list if not any(is_sublist(sublist, other) for other in duplicate_list if sublist is not other)]
输出
上述代码的输出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
所有额外的子列表都被删除,因此我们编写了正确的代码来删除所有额外的子列表。
比较每个列表
这是一种非常复杂的方法,用于删除已存在于另一个子列表中的子列表。在此方法中,所有子列表都会相互比较,并将不重复的子列表复制到新列表中。我们可以借助以下示例来理解这一点:
示例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] #A copy of duplicate list is created to avoid any errors in the original file new_list = duplicate_list[:] #Check each sublist present in the new_list for sublist in duplicate_list: for other in new_list: # Checking of presence of sublist present in other sublist is done if sublist != other and set(sublist).issubset(other): #issubset is used to check presence of one sublist in another sublist # Remove all the repeating sublist new_list.remove(sublist) break #break is used to stop the loop so that it does not keep checking continuosly print(new_list)
输出
上述代码的输出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
当列表太长并且包含大量元素较多的子列表时,此方法更合适。
设置操作
在此操作中,在设置操作的帮助下删除其他子列表中存在的子列表。在这种方法中,我们可以将列表中的每个子列表转换为一个集合,并且借助不同的操作,我们可以删除其他子列表中存在的所有子列表。我们通过下面的例子可以更清楚地理解:
示例
duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]] new_list = [] for sublist in duplicate_list: is_subset = False for other in duplicate_list: if sublist != other and set(sublist).difference(set(other)) == set(): #The difference operation is used to calculate the difference betwen two sets is_subset = True #When the sublist is present in another sublist the result of is_subset will be true break #Once the result is found to be true, the loop is broke and all the other sublist are copied into the new_list if not is_subset: new_list.append(sublist) print(new_list)
输出
上述代码的输出如下:
[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
其他子列表中存在的所有子列表均已删除。
结论
删除另一个子列表中已经存在的子列表的问题是用户经常面临的问题,很多时候它会导致消耗用户大量的时间。因此,可以使用上一篇文章中建议的不同方法快速删除另一个子列表中存在的所有子列表。
以上就是Python - 删除在另一个子列表中存在的子列表的详细内容,更多请关注php中文网其它相关文章!
Python - 根据索引更改列表中的多个元素
如果您有一个索引列表,并且您有 x
,您可以遍历并更新相应索引处的值:
rt = [10,20,30,40]
rtInd = [2,3]
x = 10
for i in rtInd:
rt[i] += x
# result:
# rt = [10,40,50]
,
如果 rt
是 numpy
数组,您可以使用索引本身的 list
(或 numpy.array
)进行索引(必须不是一个 tuple
,要清楚,因为 tuple
用于多维访问,而不是一组要修改的索引):
# Setup
rt = numpy.array([10,40],dtype=np.int64)
rtInd = [2,3] # Or numpy.array([2,3])
x = 123 # Arbitrary value picked
# Actual work
rt[rtInd] += x
这会将工作推送到 numpy
内的单个操作(可能会提高性能)。
也就是说,numpy
是一个非常重量级的依赖项;如果您还没有依赖 numpy
,我会坚持使用 M Z's answer 中描述的简单循环。
python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔
sum1= 0 sum2= list[0] for i in range(1,len(list)): num= sum1 sum1= sum2+ list[i] sum2= max(num,sum2) print(max(sum2,sum1))
只有当k = 1 [求和数之间只有一个元素]时,这个代码才有效,如何通过动态编程改变k值来改善它.其中k是求和数之间的元素数.
例如:
list = [5,6,4,1,2] k = 1
答案= 11#5 4 2
list = [5,2] k = 2
答案= 8#6 2
list = [5,3,10,2] k = 1
答案= 15#5 10
解决方法
import collections def max_sum_separated_by_k(iterable,k): best = collections.deque([0]*(k+1),k+1) for item in iterable: best.appendleft(max(item + best[-1],best[0])) return best[0]
这使用O(k)空间和O(N)时间.所有deque操作,包括向一端附加值(并从另一端隐式删除一个以保持长度限制)和从末尾读取,都是O(1).
如果您希望算法返回最大子序列(而不仅仅是其总和),您可以将deque的初始化更改为以空列表而不是0开始,然后追加max([item] best [-1],最佳[0],key = sum)在循环体中.但这样效率会相当低,因为它会在整个地方添加O(N)操作.
python – 如何在列表中创建列表,其中每个未来列表由列表中的空格分隔
row = list(input()) print(row) ['1','2','3',' ','4','5','6','7','8','9',' ']
所以我需要在下面创建“行”列表.该列表分为基于空格的子列表:
[['1','3'],['4','6'],['7','9']]
解决方法
myinput = '123 456 789' row = list(map(list,myinput.split())) print(row) [['1','9']]
或者,使用列表理解:
row = [list(i) for i in myinput.split()]
今天的关于Python:如何更改列表的每个子列表中每个元素的数据类型在这种情况下: int 到 str的分享已经结束,谢谢您的关注,如果想了解更多关于Python - 删除在另一个子列表中存在的子列表、Python - 根据索引更改列表中的多个元素、python – 列表元素的最大总和,每个列表元素由(至少)k个元素分隔、python – 如何在列表中创建列表,其中每个未来列表由列表中的空格分隔的相关知识,请在本站进行查询。
本文标签: