更改列表中的文字
我正在尝试更改字符串列表中的价值。
我想将这些名称从上一个,第一个m 更改为首先,同时将它们保留为列表类型(即不要转换为数据框架,然后返回 列表
看起来像这样
current_list = ["Sanchez, Rick", "Rick, Killer", "Smith, Morty S O L", "Smith, Summer J", "Clockberg Jr., Revolio", "van Womg, Peter", "Lynn-Marie, Sam", "Parker II, Peter"]
。
transformed_list = ["Rick Sanchez", "Killer Rick", "Morty Smith", "Summer Smith", "Revolio Clockberg", "Peter Womg", "Sam Lynn-Marie", "Peter Parker"]
我的当前
如果列表是DF,我会做的。
transformed_list = current_list.apply(
lambda x: x.split()[2] + ", " + x.split()[0] + " " + x.split()[1]
if len(x.split()) == 3
else x.split()[1] + ", " + x.split()[0]
)
I'm attempting to change values in my list of strings.
I want to change these names in my list from Last, First M to First Last while keeping these as a list type (ie don't transform to a data frame then back to a list.
My current list looks like this.
current_list = ["Sanchez, Rick", "Rick, Killer", "Smith, Morty S O L", "Smith, Summer J", "Clockberg Jr., Revolio", "van Womg, Peter", "Lynn-Marie, Sam", "Parker II, Peter"]
This is what I want my finished list to look like.
transformed_list = ["Rick Sanchez", "Killer Rick", "Morty Smith", "Summer Smith", "Revolio Clockberg", "Peter Womg", "Sam Lynn-Marie", "Peter Parker"]
I learned using a lambda function works with dataframes but this won't work because lists don't have an attribute apply to them.
This is what I would have done if the list was a df. I feel like it's something similar but I'm not sure.
transformed_list = current_list.apply(
lambda x: x.split()[2] + ", " + x.split()[0] + " " + x.split()[1]
if len(x.split()) == 3
else x.split()[1] + ", " + x.split()[0]
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用列表理解。在
“,”
上拆分每个字符串,然后在反向中重新加入([:: -1]
)订单。要仅获取姓氏的第一部分,您可以做这样的事情(尽管您需要分别对待“ Van Womg” - 我不确定临时解决方案是否会在木板)。
You can use a list comprehension. Split each string at
", "
, then rejoin in reversed ([::-1]
) order.To get only the first part of the last name, you can do something like this (though then you'd need to treat the "van Womg" separately – I'm not sure if an ad-hoc solution will do the trick across the board).
首先,您必须为列表的每个部分分配一个钥匙,例如sanchez为0,瑞克(Rick)为1。然后,当分配输出时,只需将A -1放在第三个(即[0,0,-1]) ),这应该扭转订单并将其保留在列表中。
First you have to assign a key to each part of the list, for instance Sanchez would be 0 and Rick would be 1. Then when assigning your output simply place a -1 on the third (ie. [0,0,-1] ) and that should reverse the order and keep them in the list.
这可以工作吗?
Would this work?
您可以使用列表理解和inthuilt string.split()方法来执行此操作。拆分方法在给定字符周围拆分一个字符串,您可以按照所需的顺序重新加入它们。
[s.split(',',for current_list中的s]
将为您提供列表,其中我们已将EG
更改为“ Sanchez,Rick”
将其更改为['sanchez','rick']
我们可以在另一个列表中迭代此列表中的理解,以给我们我们想要的东西:
我们使用加入方法加入两个字符串
[b,a] < /code>带有空间'',我们将A和B设置为中间列表中的字符串。
You could do this with a list comprehension and the inbuilt string.split() method. The split method splits a string around the given characters, you can rejoin them in the order you want.
[s.split(', ' for s in current_list]
Will give you a list of lists, where we've changed e.g.
"Sanchez, Rick"
into['Sanchez', 'Rick']
We can then iterate over this list in another list comprehension to give us what we want:
We use the join method to join the two strings
[b, a]
with a space ' ', and we have set a and b to be the strings in our intermediate list.