没有导入任何模块,我想知道是否可以将字符串列表转换为嵌套的字符串列表。字符串示例列表如下:
public static IntIntPredicate parse(String x) {
// This is going to require a lot of work, but
// there are many questions on this site about how
// to parse expressions such as "(2 + 3) * 9"
}
以下示例输出类似于(注意我想摆脱转义序列,' \ n',因为它表示行):
public static void Main()
{
int? i = 2;
Console.WriteLine( type(i) );
}
public static Type type<T>( T x ) { return typeof(T); }
那就是说,有没有办法可以使用像.split()或.strip()这样的内置函数进行转换?
答案 0 :(得分:2)
test = ['"Hello", "Greetings", "Welcome", "Hi"\n', '"Bye", "Farewell", "Seeya", "Later"\n']
sub_lists = [[sub_element.strip('\n') for sub_element in element.split(',')] for element in test]
print(sub_lists)
答案 1 :(得分:0)
我会做这样的事情:
def batched(input):
batch = []
for item in input:
if item[-1] == '\n':
batch.append(item[:-1])
yield batch
else:
batch.append(item)
if len(batch) > 0:
yield batch
batched_data = list(batched(['"Hello", "Greetings", "Welcome", "Hi"\n', '"Bye", "Farewell", "Seeya", "Later"\n']))