# 如何在python中划分列表？How to divide a list in python?

``````if dictionary.get(p1, character) is dictionary.get(p4, character)
``````

## 2 个回复2

### ===============>>#1 票数：2

``````list1=[[1, 2, 2, 1], [3, 4, 3]]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[[1, 1], [2, 2], [3, 3], [4]]
``````

### 例1

``````list1=[['hello', 'hello', 'hello', 'what'], ['i', 'am', 'i']]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[['what'], ['hello', 'hello', 'hello'], ['i', 'i'], ['am']]
``````

### 例2

``````list1=[[1,2,3,2,1],[9,8,7,8,9],[5,4,6,4,5]]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[[1, 1], [2, 2], [3], [8, 8], [9, 9], [7], [4, 4], [5, 5], [6]]
``````

### ===============>>#2 票数：1 已采纳

``````from collections import Counter

def solve(lst):
counters = map(Counter, lst)
return [ [uniq]*c[uniq] for seq, c in zip(lst, counters)
for uniq in unique_everseen(seq)]
``````

``````>>> print(solve([[1, 2, 2, 1], [3, 4, 3]]))
[[1, 1], [2, 2], [3, 3], [4]]
>>> print(solve([['hello', 'hello', 'hello', 'what'], ['i', 'am', 'i']]))
[['hello', 'hello', 'hello'], ['what'], ['i', 'i'], ['am']]
>>> print(solve([[1,2,3,2,1],[9,8,7,8,9],[5,4,6,4,5]]))
[[1, 1], [2, 2], [3], [9, 9], [8, 8], [7], [5, 5], [4, 4], [6]]
``````

`unique_everseen`配方的代码：

``````from itertools import filterfalse

def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
if key is None:
for element in filterfalse(seen.__contains__, iterable):
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
yield element
``````

## 未解决问题？本站智能推荐：

4回复

3回复

1回复

4回复

1回复

1回复

5回复

### 开始使用Python函数和列表

*快速编辑我正在使用python 3.4 我刚刚开始在课堂上学习python，尽管它似乎比C ++更易于处理，但有些事情我还无法解决。 我需要做一个小函数，它接受以法郎为单位的某些温度，并将其转换为摄氏度，然后以列表形式放回去。 我拥有大部分代码，我相信它应该可以工作。 但是，我
1回复

1回复

3回复