Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在元素相等的较小列表上拆分列表_Python_Python 3.x - Fatal编程技术网

Python 在元素相等的较小列表上拆分列表

Python 在元素相等的较小列表上拆分列表,python,python-3.x,Python,Python 3.x,在我的列表“A”中,我有数字和“”,所以我想制作一个名为“b”的列表,每个列表都应该有九个数字(如果可能的话),不管它有多少“”。 你知道怎么做吗 A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17'] B = [ ['1', '3, '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17'] ] 这将帮助您: >>

在我的列表“A”中,我有数字和“”,所以我想制作一个名为“b”的列表,每个列表都应该有九个数字(如果可能的话),不管它有多少“”。 你知道怎么做吗

A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [ ['1', '3, '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17'] ]
这将帮助您:

>>> a = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ',        '5', '17']
>>> b=[a[i:i+9] for i in xrange(0,len(a),9)] 
>>> b
[['1', '3', '4', '5', '7', '8', '9', ' ', '13'], ['16', '3', ' ', '5', '17']]
>>> 

这可以通过两个嵌套的while循环完成:

>>> A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
>>> B = []
>>> while A:
...    L = []
...    c = 0
...    while A and c < 9:
...        L.append(A.pop(0))
...        if L[-1].isdigit():
...           c += 1
...    B.append(L) 
... 
>>> B
[['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17']]
>>A=['1','3','4','5','7','8','9','13','16','3','5','17']
>>>B=[]
>>>而A:
...    L=[]
...    c=0
...    当A和c<9时:
...        L.append(A.pop(0))
...        如果L[-1].isdigit():
...           c+=1
...    B.1(L)
... 
>>>B
[['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17']]

当A不为空时,外部的一个循环,而当A不为空时,内部的一个循环,并且附加到当前子列表的纯数字字符串数小于9。只有在找到仅由数字组成的字符串后,计数器才会递增。

深入理解列表是值得的

Python3.x中没有xrange,更确切地说,range(3.x中)与Python2.x中的xrange完全相同

A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [i for i in A[0:9]]  #is cleaner.

虽然我不确定你的目标到底是什么。您是否希望第二个列表(我认为是余数列表)位于同一个变量中?因此,如果列表中有28个元素,则需要三个9元素列表和一个1元素列表?

这是一个有点脏的解决方案,但我认为您可能需要检查isdigit part and pop

def take(lst, n):
    if not lst:
        return ValueError("Empty list, please check the list.")
    items = list(lst)
    new_list = []
    count = 0
    while items:
        item = items.pop(0)
        new_list.append(item)
        if item.isdigit():
            count += 1
        if count >= n:
            yield new_list
            new_list = []
            count = 0
    if new_list:
        yield new_list


A = ['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16', '3', ' ', '5', '17']
B = [ii for ii in take(A, 9)]
#[['1', '3', '4', '5', '7', '8', '9', ' ', '13', '16'], ['3', ' ', '5', '17']]
检查以下各项:


为什么这听起来像我的第一门大学编程课程?您是否尝试过遍历数组并寻找断点?