[英]Rearrange / restructure a list of strings based on a value of part of the string
I have a list of.csv files that I'm trying to rearrange in a specific way.我有一个 .csv 文件列表,我正在尝试以特定方式重新排列这些文件。 Let's say my original list looks like this:
假设我的原始列表如下所示:
["Component 1 - Cable 1.csv", "Component 1 - Cable 2.csv", "Component 2 - Cable 1.csv", "Component 3 -Cable 1.csv"]
I would like the list to look like this instead, but can't figure out how to achieve it:我希望列表看起来像这样,但不知道如何实现它:
[["Component 1 - Cable 1.csv", "Component 1 - Cable 2.csv"], ["Component 2 - Cable 1.csv"], ["Component 3 - Cable 1.csv"]]
Use itertools.groupby
:使用
itertools.groupby
:
from itertools import groupby
res = []
for _, g in groupby(l, key=lambda x:x.split('-')[0]):
res.append(list(g))
Or one-liner:或单线:
res = [list(g) for _, g in groupby(l, key=lambda x:x.split('-')[0])]
Output of both: Output 两者:
[['Component 1 - Cable 1.csv', 'Component 1 - Cable 2.csv'],
['Component 2 - Cable 1.csv'],
['Component 3 -Cable 1.csv']]
As an alternative to itertools.groupby you can use a dictionary which may be easier for newbies as follows.作为 itertools.groupby 的替代方法,您可以使用对新手来说可能更容易的字典,如下所示。
l = ["Component 1 - Cable 1.csv", "Component 1 - Cable 2.csv", "Component 2 - Cable 1.csv", "Component 3 -Cable 1.csv"]
# Option 1: Use of dictionary
newdata = {}
for x in l:
group = x.split('-')[0]
if group not in newdata: # Add list to dictionary for missing keys
newdata[group] = []
newdata[group].append(x)
print (list(newdata.values()))
# Option 2: Simply by using set default to remove conditional key check
newdata = {}
for x in l:
group = x.split('-')[0] # group based upon string before hyphen
newdata.setdefault(group, []).append(x) # removes need for conditional by providing a default value for missing keys
print (list(newdata.values()))
# Option 3: Simplify further by using default dict
from collections import defaultdict
newdata = defaultdict(list) # creates dictionary where missing keys return a empty list
for x in l:
group = x.split('-')[0] # group based upon string before hyphen
newdata[group].append(x) # accumulate groupings together as value
# in dictionary
print (list(newdata.values()))
All three options output所有三个选项 output
[['Component 1 - Cable 1.csv', 'Component 1 - Cable 2.csv'], ['Component 2 - Cable 1.csv'], ['Component 3 -Cable 1.csv']]
Assuming that you have missed a bracket between first and second elements of the output list and your expected output is:假设您错过了 output 列表的第一个和第二个元素之间的括号,并且您预期的 output 是:
[['Component 1 - Cable 1.csv'], ['Component 1 - Cable 2.csv'], ['Component 2 - Cable 1.csv'], ['Component 3 -Cable 1.csv']]
you can simply use:你可以简单地使用:
l = ["Component 1 - Cable 1.csv", "Component 1 - Cable 2.csv", "Component 2 - Cable 1.csv", "Component 3 -Cable 1.csv"]
l = [[i] for i in l]
Here i is just some variable and you are applying a function over all elements in the given list.这里 i 只是一些变量,您正在对给定列表中的所有元素应用 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.