更改列表中的文字

发布于 2025-02-06 08:23:31 字数 903 浏览 5 评论 0原文

我正在尝试更改字符串列表中的价值。

我想将这些名称从上一个,第一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

○闲身 2025-02-13 08:23:31

您可以使用列表理解。在“,”上拆分每个字符串,然后在反向中重新加入([:: -1])订单。

[" ".join(n.split(", ")[::-1]) for n in current_list]

要仅获取姓氏的第一部分,您可以做这样的事情(尽管您需要分别对待“ Van Womg” - 我不确定临时解决方案是否会在木板)。

[" ".join([x.split()[0] for  x in n.split(", ")][::-1]) for n in current_list]

You can use a list comprehension. Split each string at ", ", then rejoin in reversed ([::-1]) order.

[" ".join(n.split(", ")[::-1]) for n in current_list]

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).

[" ".join([x.split()[0] for  x in n.split(", ")][::-1]) for n in current_list]
街道布景 2025-02-13 08:23:31

首先,您必须为列表的每个部分分配一个钥匙,例如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.

回忆那么伤 2025-02-13 08:23:31
transformed_list = [x.split()[2] + ", " + x.split()[0] + " " + x.split()[1]
                    if len(x.split()) == 3 else x.split()[1] + ", " + x.split()[0] 
                    for x in current_list]

这可以工作吗?

transformed_list = [x.split()[2] + ", " + x.split()[0] + " " + x.split()[1]
                    if len(x.split()) == 3 else x.split()[1] + ", " + x.split()[0] 
                    for x in current_list]

Would this work?

茶底世界 2025-02-13 08:23:31

您可以使用列表理解和inthuilt string.split()方法来执行此操作。拆分方法在给定字符周围拆分一个字符串,您可以按照所需的顺序重新加入它们。

[s.split(',',for current_list中的s]

将为您提供列表,其中我们已将EG 更改为“ Sanchez,Rick”将其更改为['sanchez','rick']

我们可以在另一个列表中迭代此列表中的理解,以给我们我们想要的东西:

[' '.join([b, a]) for a, b in [s.split(', ' for s in current_list]]

我们使用加入方法加入两个字符串[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:

[' '.join([b, a]) for a, b in [s.split(', ' for s in current_list]]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文