如何仅连接嵌套列表中的字符串元素

How to join only the string elements inside a nested list

在这里,我有一个包含字符串和整数值的嵌套列表,如何仅将字符串值连接在一起?

my_list = [['Advertising', 'Revenue', 963333, 1013332, 49999, '68.31%\n'],
           ['Total', 'Suburban', 'Revenue', 17882085, 18795466, 913381, '69.55%\n']]

我尝试使用以下代码,但没有得到准确的输出

for i in my_list:
    for j in i:
        if type(j) == str:
            l = '_'.join(j)
            print(l)

预期输出:

[['Advertising_Revenue', 963333, 1013332, 49999, '68.31%\n'],
 ['Total_Suburban_Revenue', 17882085, 18795466, 913381, '69.55%\n']]

谁能帮我解决这个问题, 提前致谢!

您正在分配 l,但没有对其进行任何操作。

您基本上需要两个循环,第一个循环迭代外部列表中的列表,第二个循环迭代内部列表中的值。

result = []
while my_list:
    row = my_list.pop(0)
    cols = []
    strng = []
    while row:
        val = row.pop(0)
        if isinstance(val, str):
            strng.append(val)
        else:
            if strng:
                cols.append('_'.join(strng))
                strng = []
            cols.append(val)
    else:
        cols.append('_'.join(strng))
    result.append(cols)
>>> result
[['Advertising_Revenue', 963333, 1013332, 49999, '68.31%\n'], ['Total_Suburban_Revenue', 17882085, 18795466, 913381, '69.55%\n']]

保留一个buffer s,追加buffer s,看到int就清空

my_list = [['Advertising', 'Revenue', 963333, 1013332, 49999, '68.31%\n'],
           ['Total', 'Suburban', 'Revenue', 17882085, 18795466, 913381, '69.55%\n']]
ll = []
for l in my_list:
    s, l1 = [], []
    for e in l:
        if type(e) == str:
            s.append(e)
            continue
        if s:
            l1.append('_'.join(s))
            s = []
        l1.append(e)
    if s:
        l1.append('_'.join(s))
    ll.append(l1)
print(ll)
my_list = [['Advertising', 'Revenue', 963333, 1013332, 49999, '68.31%\n'],
           ['Total', 'Suburban', 'Revenue', 17882085, 18795466, 913381, '69.55%\n']]

result = []
for sublist in my_list:
    result_sublist = []
    prev_is_string = False
    for item in sublist:
        cur_is_string = isinstance(item, str)
        if prev_is_string and cur_is_string:
            result_sublist[-1] += "_" + item
        else:
            result_sublist.append(item)
        prev_is_string = cur_is_string
    result.append(result_sublist)

print(result)

尝试:

    my_list = [['Advertising', 'Revenue', 963333, 1013332, 49999, '68.31%\n'],
           ['Total', 'Suburban', 'Revenue', 17882085, 18795466, 913381, '69.55%\n']]

res = []
continuous_strings = []
nested_list = []
for lst in my_list:
    for item in lst:
        if isinstance(item, str):
            continuous_strings.append(item)
        else:
            if continuous_strings:
                new_item = '_'.join(continuous_strings)
                nested_list.append(new_item)

            nested_list.append(item)
            continuous_strings = []

    nested_list.append('_'.join(continuous_strings))
    continuous_strings = []
    res.append(nested_list)
    nested_list = []

print(res)

输出:

[['Advertising_Revenue', 963333, 1013332, 49999, '68.31%\n'], ['Total_Suburban_Revenue', 17882085, 18795466, 913381, '69.55%\n']]