如何从Python列表中返回嵌套的频率列表?
从卡片列表-返回具有相同编号的卡片的嵌套列表,然后将其余卡片放入单独的列表中如何从Python列表中返回嵌套的频率列表?,python,python-3.x,Python,Python 3.x,从卡片列表-返回具有相同编号的卡片的嵌套列表,然后将其余卡片放入单独的列表中 EXAMPLE: ['6C', '7C', '7D', '8S', '6D'] 返回['7C',7D'],['6C','6D'],['8S']] 我尝试过使用while循环,但无法理解 谢谢大家! 这里。试试这个 from itertools import groupby a = ['6C', '7C', '7D', '8S', '6D'] a.sort() final_list = [] for i, j in g
EXAMPLE: ['6C', '7C', '7D', '8S', '6D']
返回['7C',7D'],['6C','6D'],['8S']]
我尝试过使用while循环,但无法理解
谢谢大家! 这里。试试这个
from itertools import groupby
a = ['6C', '7C', '7D', '8S', '6D']
a.sort()
final_list = []
for i, j in groupby(a, key=lambda x:x[0]):
final_list.append(list(j))
print(final_list)
在这里。试试这个
from itertools import groupby
a = ['6C', '7C', '7D', '8S', '6D']
a.sort()
final_list = []
for i, j in groupby(a, key=lambda x:x[0]):
final_list.append(list(j))
print(final_list)
我知道这不是一个最佳的解决方案,但是
a = ['6C', '7C', '7D', '8S', '6D']
item = []
ls = []
for i in range(len(a)):
if a[i][0] in ls:
continue
else:
ls.append(a[i][0])
temp = []
temp.append(a[i])
for j in range((i+1), len(a)):
if a[i][0] == a[j][0]:
temp.append(a[j])
else:
continue
item.append(temp)
print(item)
我知道这不是一个最佳的解决方案,但是
a = ['6C', '7C', '7D', '8S', '6D']
item = []
ls = []
for i in range(len(a)):
if a[i][0] in ls:
continue
else:
ls.append(a[i][0])
temp = []
temp.append(a[i])
for j in range((i+1), len(a)):
if a[i][0] == a[j][0]:
temp.append(a[j])
else:
continue
item.append(temp)
print(item)
一种可能的解决方案是对列表进行排序,然后使用字符串的整数部分对列表进行分组,然后使用公共整数元素将项目分组在一起
from itertools import groupby
li = ['6C', '7C', '7D', '8S', '6D']
#Sort the list based on the integer part of the string
li = sorted(li, key=lambda x:int(x[0]))
#Group the list based on the integer part of the string
res = [list(group) for _, group in groupby(li, key=lambda x:int(x[0]))]
print(res)
输出将是
[['6C', '6D'], ['7C', '7D'], ['8S']]
一种可能的解决方案是对列表进行排序,然后使用字符串的整数部分对列表进行分组,然后使用公共整数元素将项目分组在一起
from itertools import groupby
li = ['6C', '7C', '7D', '8S', '6D']
#Sort the list based on the integer part of the string
li = sorted(li, key=lambda x:int(x[0]))
#Group the list based on the integer part of the string
res = [list(group) for _, group in groupby(li, key=lambda x:int(x[0]))]
print(res)
输出将是
[['6C', '6D'], ['7C', '7D'], ['8S']]
另一种不使用itertools groupby的方法
l = ['6C', '7C', '7D', '8S', '6D']
result, temp = [], []
l = sorted(l, key=lambda x: x[0])
counter = l[0][0]
for i in l:
if i[0] == counter:
temp.append(i)
else:
result.append(temp)
temp = [i]
counter = i[0]
if temp:
result.append(temp)
print(result)
另一种不使用itertools groupby的方法
l = ['6C', '7C', '7D', '8S', '6D']
result, temp = [], []
l = sorted(l, key=lambda x: x[0])
counter = l[0][0]
for i in l:
if i[0] == counter:
temp.append(i)
else:
result.append(temp)
temp = [i]
counter = i[0]
if temp:
result.append(temp)
print(result)
请告诉我们您试图回答的问题,并在循环过程中使用
包含您的代码。查看如何使用itertools.groupby
“…在单独的列表中休息…”如果您的列表中也有“9K”。。。这与“8S”是属于同一个列表还是属于单独的列表?[在groupby中i,j的列表(j)(已排序(['6C',7C',7D',8S',6D']),lambda x:x[0])]
请在循环时使用向我们展示您的问题尝试并包含您的代码。查看使用itertools.groupby
“…在单独的列表中休息…”如果您的列表中也有“9K”。。。这是和“8S”放在同一个列表中还是放在一个单独的列表中?[列表(j)代表i,j在groupby中(排序(['6C','7C','7D','8S','6D','8d',],lambda x:x[0])]
[列表(j)代表i,j在groupby中(排序(['6C','7C','7D','8S','6D','6D']),lambda x:x[0])
。我们可以这样做。我只是觉得如果我一步一个脚印的话会更容易理解。:-)<代码>[列表(j)中的i,j(分组依据(排序(['6C','7C','7D','8S','6D']),lambda x:x[0])]
当然。我们可以这样做。我只是觉得如果我一步一个脚印的话会更容易理解。:-)