简体   繁体   English

在列表中用逗号分隔元素

[英]Separate elements divided by comma in a list

I have a list of strings that contains elements of the type我有一个包含该类型元素的字符串列表

List=['name1,vol', 'name1,price','name2, vol', 'name2,price'.... ] 

I would like to extract a list only of "names" which are the parts that actually change as the second components in each element have a fix pattern (here:vol, price).我想仅提取“名称”列表,这些名称是实际更改的部分,因为每个元素中的第二个组件具有固定模式(此处:卷,价格)。 Notice that the "names" can obviously have different length.请注意,“名称”显然可以具有不同的长度。 To sum up, I'd like to extract something like:总而言之,我想提取如下内容:

List_names=['name1', 'name2' ] 

How can I do that?我怎样才能做到这一点?

What if I have something of the type:如果我有以下类型的东西怎么办:

List_tricky=[('name1', 'vol'), ('name1', 'price'),('name2', 'vol'), ('name2', 'price').... ] 

Something like this?像这样的东西?

List=['name1,vol', 'name1,price','name2, vol', 'name2,price']

names = []

for string in List:

    name = string.split(',')[0]

    names.append(name)

print(names)

For your 'tricky' case, you can try:对于“棘手”的情况,您可以尝试:

# initialize variables:
names = []

# iterate over each point (tuple):
for point in List:

    # get name:
    name = point[0]

    # append to list:
    names.append(name)

print(names)

与@Daniel Sokol 的回答类似的逻辑,您可以使用单行:

list2 = [x.split(',')[0] for x in List]

You could turn it into a dict then back into a list using str.split .您可以将其转换为 dict,然后使用str.split重新转换为列表。 (No loop required as it does it efficiently for you) Use functools.partial to apply the split to each string instead of a lambda: (不需要循环,因为它可以有效地为您执行)使用functools.partial将拆分应用于每个字符串而不是 lambda:

from functools import partial
list(dict(map(partial(str.split, sep=','), List)))

This works for either input but way more simple for the list of tuples:这适用于任一输入,但对于元组列表更简单:

>>> l = ['name1,vol', 'name1,price','name2, vol', 'name2,price'.... ]
>>> list(dict(map(partial(str.split, sep=','), List)))
['name1', 'name2']

>>> l = [('name1', 'vol'), ('name1', 'price'),('name2', 'vol'), ('name2', 'price').... ] 
>>> list(dict(l))
['name1', 'name2']

To add on top of @Alireza Tajadod's already wonderful answer, you might want to apply conversion to a set, then back to a list to remove any possible duplication items, as suggested by @Cryptoharf84 in the comments.要添加@Alireza Tajadod 已经很好的答案,您可能希望将转换应用于集合,然后返回到列表以删除任何可能的重复项,正如@Cryptoharf84 在评论中所建议的那样。

names_list = list(set([entry.split(',')[0] for entry in List]))

The same logic with list comprehension can be applied to the trickier case.与列表理解相同的逻辑可以应用于更棘手的情况。

names_list_2 = list(set([entry[0] for entry in List_tricky]))

To make list comprehension more explicit, you can also do the following:为了使列表理解更加明确,您还可以执行以下操作:

names_list_3 = list(set([name for name, _ in List_tricky]))

The _ indicates that we are discarding the second value of the unpacked tuple. _表示我们正在丢弃解包元组的第二个值。

Sets are useful because converting a list with duplicate elements into a set effectively removes any duplications.集合很有用,因为将具有重复元素的列表转换为集合可以有效地消除任何重复。

As a tip, look for naming conventions in python.作为提示,请在 python 中查找命名约定。 But never name variables starting with upper case, nor with existing class names.但是永远不要以大写开头的变量命名,也不要以现有的类名开头。

I will try something like:我会尝试类似的事情:

list_names = [s.split(',')[0].strip() for s in List]
list_unique_names(set(list_names))

split returns a list of "chunks" of the original string, and strip to remove whitespaces on beginning/end of the resulting string. split 返回原始字符串的“块”列表,并删除结果字符串开头/结尾的空格。

I will change your data structure to dict instead of list我会将您的数据结构更改为dict而不是list

d={'name1': ('vol', 'price'),'name2': ('vol', 'price'), .... }

In order to get just the names: d.keys()为了只得到名字: d.keys()

You can also use a .map() function:您还可以使用.map()函数:

# Case 1: List
all_names = map(lambda x :a.split(',')[0], List)

# Case 2: List_tricky
all_names = [i[0] for i in List_tricky]

# After the code is the same
unique_names = set(all_names)
List_names = list(unique_names)
print(List_names)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM