从int到str的转移时划分列表

时间:2018-10-04 19:37:12

标签: python list

所以我有一个看起来像['A',1、3、6,'B',3、5,'C',6、7、3]的列表,我如何每次都将列表划分为子列表有一个新字符串,这样我就可以得到[A',1,3,6] ['B',3,5] ['C',6,7,3]

6 个答案:

答案 0 :(得分:0)

只需使用正则表达式查找字符串字符,然后获取所有符合条件的子字符串

import re
a=['A', 1, 3, 6, 'B', 3, 5, 'C', 6, 7, 3] 
l=re.findall('(\w\d{0,})',''.join(list(map(str,a))))
l=list(map(list,l))
for i in l:
    for index,j in enumerate(i):
        if j.isdigit():
            i[index]=int(j) #change type back to int for all the integers
print(l) #[['A', 1, 3, 6], ['B', 3, 5], ['C', 6, 7, 3]]

答案 1 :(得分:0)

您可以执行此操作,但是可能有更多的Python方式:

my_list = ["A", 1, 3, 6, "B", 3, 5, "C", 6, 7, 3]

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def sort_list(input_list):

    parent_list = []
    child_list = [input_list.pop(0)]

    while (len(input_list)):
        first = input_list.pop(0)

        if is_number(first):
            child_list.append(first)
        else: 
            parent_list.append(child_list)
            child_list = list(first)

    parent_list.append(child_list)

    return parent_list

print(sort_list(my_list))

答案 2 :(得分:0)

您可以通过简单的迭代循环来实现

    PANID       WEEK brandID  diff
1 1100016 2001-03-19      48  first purchase of this brand
2 1100016 2001-07-30      48  ~20 week difference
3 1100016 2001-12-17      48  ~18 week difference
4 1100057 2001-06-11     209  first purchase
5 1100057 2001-06-18     207  no difference since this person bought a different brand
6 1100057 2001-06-18     217  no difference since this person bought a different brand

答案 3 :(得分:0)

{% language 'en' %}
     {% get_current_language as LANGUAGE_CODE %}
     <!-- Current language: {{ LANGUAGE_CODE }} -->
     <h1>{% trans "Welcome to my website" %}</h1>
{% endlanguage %}

{% language 'el' %}
     {% get_current_language as LANGUAGE_CODE %}
     <!-- Current language: {{ LANGUAGE_CODE }} -->
     <h1>{% trans "Welcome to my website" %}</h1>
{% endlanguage %}

这将为您提供由my_list = [’A’, 1, 3, 6, ’B’, 3, 5, ’C’, 6, 7, 3] sub_list = [] count = -1 for i in my_list: if (isinstance(i, str)): count+=1 l = [i] sub_list.append(l) else: if (count is -1): count+=1 sub_list[count].append(i) 分隔的列表列表。

答案 4 :(得分:0)

given = ['A', 1, 3, 6, 'B', 3, 5, 'C', 6, 7, 3]
result = []
current = []
for element in given:
    if type(element)==str:
        if current!=[]:
            result.append(current)
        current = []
    current.append(element)
result.append(current)
print(result)

打印出:

[['A', 1, 3, 6], ['B', 3, 5], ['C', 6, 7, 3]]

答案 5 :(得分:0)

假设顺序不是首选项:您可以使用collections.defaultdict

>>> my_dict = collections.defaultdict(list)
>>> key = ''
>>> for x in range(len(data)):
...     if str(data[x]).isalpha():
...         key = data[x]
...     elif str(data[x]).isnumeric():
...         my_dict[key].append(data[x])
...
>>> my_dict
defaultdict(<class 'list'>, {'C': [6, 7, 3], 'A': [1, 3, 6], 'B': [3, 5]})

假设顺序为首选项:

>>> my_dict = collections.OrderedDict()
>>> key = ''
>>> for x in range(len(data)):
...     if str(data[x]).isalpha():
...         key = data[x]
...         my_dict[key] = []
...     elif str(data[x]).isnumeric():
...         my_dict[key].append(data[x])
...
>>> my_dict
OrderedDict([('A', [1, 3, 6]), ('B', [3, 5]), ('C', [6, 7, 3])])