如何在列表中找到相同的值并将新列表组合在一起?
How can I find same values in a list and group together a new list?
来自这个列表:
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
我正在尝试创建:
L = [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]
任何被发现相同的值都被分组到它自己的子列表中。
到目前为止,这是我的尝试,我想我应该使用 while
循环?
global n
n = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] #Sorted list
l = [] #Empty list to append values to
def compare(val):
""" This function receives index values
from the n list (n[0] etc) """
global valin
valin = val
global count
count = 0
for i in xrange(len(n)):
if valin == n[count]: # If the input value i.e. n[x] == n[iteration]
temp = valin, n[count]
l.append(temp) #append the values to a new list
count +=1
else:
count +=1
for x in xrange (len(n)):
compare(n[x]) #pass the n[x] to compare function
你太复杂了。
您要做的是:对于每个值,如果它与最后一个值相同,则将其附加到最后一个值的列表中;否则,创建一个新列表。你可以直接把英文翻译成 Python:
new_list = []
for value in old_list:
if new_list and new_list[-1][0] == value:
new_list[-1].append(value)
else:
new_list.append([value])
如果您愿意更抽象一点,例如使用 itertools
中的分组函数,还有更简单的方法可以做到这一点。不过这个应该很容易理解。
如果您确实需要使用 while
循环执行此操作,您可以将任何 for
循环转换为 while
循环,如下所示:
for value in iterable:
do_stuff(value)
iterator = iter(iterable)
while True:
try:
value = next(iterator)
except StopIteration:
break
do_stuff(value)
或者,如果您知道可迭代对象是一个序列,则可以使用稍微简单一些的 while
循环:
index = 0
while index < len(sequence):
value = sequence[index]
do_stuff(value)
index += 1
但这两者都会使您的代码可读性降低、Pythonic 更少、更复杂、效率更低、更容易出错,等等。
您可以使用itertools.groupby
along with a list comprehension
>>> l = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
>>> [list(v) for k,v in itertools.groupby(l)]
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
这可以分配给变量 L
,如
L = [list(v) for k,v in itertools.groupby(l)]
from itertools import groupby
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
print([list(j) for i, j in groupby(N)])
输出:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
旁注:在不需要需要时避免使用全局变量。
另一个不依赖 itertools 的稍微不同的解决方案:
#!/usr/bin/env python
def group(items):
"""
groups a sorted list of integers into sublists based on the integer key
"""
if len(items) == 0:
return []
grouped_items = []
prev_item, rest_items = items[0], items[1:]
subgroup = [prev_item]
for item in rest_items:
if item != prev_item:
grouped_items.append(subgroup)
subgroup = []
subgroup.append(item)
prev_item = item
grouped_items.append(subgroup)
return grouped_items
print group([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
# [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
有人提到 N=[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 1]
它将得到 [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5], [1]]
换句话说,当列表的数字不整齐或者是乱七八糟的列表时,它是不可用的。
所以我有更好的答案来解决这个问题。
from collections import Counter
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
C = Counter(N)
print [ [k,]*v for k,v in C.items()]
您可以使用 numpy 来做到这一点:
import numpy as np
N = np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
counter = np.arange(1, np.alen(N))
L = np.split(N, counter[N[1:]!=N[:-1]])
这种方法的优点是当你有另一个与 N 相关的列表并且你想以相同的方式拆分它时。
来自这个列表:
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
我正在尝试创建:
L = [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]
任何被发现相同的值都被分组到它自己的子列表中。
到目前为止,这是我的尝试,我想我应该使用 while
循环?
global n
n = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5] #Sorted list
l = [] #Empty list to append values to
def compare(val):
""" This function receives index values
from the n list (n[0] etc) """
global valin
valin = val
global count
count = 0
for i in xrange(len(n)):
if valin == n[count]: # If the input value i.e. n[x] == n[iteration]
temp = valin, n[count]
l.append(temp) #append the values to a new list
count +=1
else:
count +=1
for x in xrange (len(n)):
compare(n[x]) #pass the n[x] to compare function
你太复杂了。
您要做的是:对于每个值,如果它与最后一个值相同,则将其附加到最后一个值的列表中;否则,创建一个新列表。你可以直接把英文翻译成 Python:
new_list = []
for value in old_list:
if new_list and new_list[-1][0] == value:
new_list[-1].append(value)
else:
new_list.append([value])
如果您愿意更抽象一点,例如使用 itertools
中的分组函数,还有更简单的方法可以做到这一点。不过这个应该很容易理解。
如果您确实需要使用 while
循环执行此操作,您可以将任何 for
循环转换为 while
循环,如下所示:
for value in iterable:
do_stuff(value)
iterator = iter(iterable)
while True:
try:
value = next(iterator)
except StopIteration:
break
do_stuff(value)
或者,如果您知道可迭代对象是一个序列,则可以使用稍微简单一些的 while
循环:
index = 0
while index < len(sequence):
value = sequence[index]
do_stuff(value)
index += 1
但这两者都会使您的代码可读性降低、Pythonic 更少、更复杂、效率更低、更容易出错,等等。
您可以使用itertools.groupby
along with a list comprehension
>>> l = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
>>> [list(v) for k,v in itertools.groupby(l)]
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
这可以分配给变量 L
,如
L = [list(v) for k,v in itertools.groupby(l)]
from itertools import groupby
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
print([list(j) for i, j in groupby(N)])
输出:
[[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
旁注:在不需要需要时避免使用全局变量。
另一个不依赖 itertools 的稍微不同的解决方案:
#!/usr/bin/env python
def group(items):
"""
groups a sorted list of integers into sublists based on the integer key
"""
if len(items) == 0:
return []
grouped_items = []
prev_item, rest_items = items[0], items[1:]
subgroup = [prev_item]
for item in rest_items:
if item != prev_item:
grouped_items.append(subgroup)
subgroup = []
subgroup.append(item)
prev_item = item
grouped_items.append(subgroup)
return grouped_items
print group([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
# [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5]]
有人提到 N=[1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 1]
它将得到 [[1], [2, 2], [3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5, 5], [1]]
换句话说,当列表的数字不整齐或者是乱七八糟的列表时,它是不可用的。
所以我有更好的答案来解决这个问题。
from collections import Counter
N = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
C = Counter(N)
print [ [k,]*v for k,v in C.items()]
您可以使用 numpy 来做到这一点:
import numpy as np
N = np.array([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
counter = np.arange(1, np.alen(N))
L = np.split(N, counter[N[1:]!=N[:-1]])
这种方法的优点是当你有另一个与 N 相关的列表并且你想以相同的方式拆分它时。