『壹』 怎麼對python中列表類型(List)進行分組
什麼是「對列表類型進行分組」?。。。
要按什麼規則分組?。。。
『貳』 python 列表分組問題
a=[{'id':12345,'name':'aaa'},{'id':3434,'name':'bbb'},{'id':3434,'name':'aaa'},{'id':9808,'name':'bbb'}]
name={}
for tmp in a:
if tmp['name'] in name:
tmp_list = name[tmp['name']]
tmp_list.append(tmp)
name[tmp['name']]= tmp_list
else:
name[tmp['name']]=[]
name[tmp['name']]=[tmp]
『叄』 用python計算表格數據,分組
不知道理解題意是否正確,見如下代碼:
group1={}
group2={}
foriteminraw_items_list:
ifitem['prov1']notingroup1.keys():
group1[item['prov1']]=list()
else:
group1[item['prov1']].append((item['count'],item['value']))
ifitem['prov2']notingroup2.keys():
group2[item['prov2']]=list()
else:
group2[item['prov2']].append((item['count'],item['value']))
#
forgingroup1.keys():
value_list=group1[g]
count=0.0
value=0.0
forvinvalue_list:count+=v[0]
forvinvalue_list:value+=v[0]/count*v[1]
print'Averageofgroup1-%sis:%f'(g,value/len(value_list))
#averageofgroup2
#...
『肆』 python字典分組
$python
Python2.7.2+(default,Jul202012,22:12:53)
[GCC4.6.1]onlinux2
Type"help","right","credits"or"license"formoreinformation.
>>>importcollections
>>>dic=dict([
...(('a',1),[1,2,3,4]),
...(('b',1),[5,6,7,8]),
...(('c',2),[1,2,3,4]),
...])
>>>counter=collections.Counter((x[1]forxindic.iterkeys()))
>>>counter
Counter({1:2,2:1})
>>>
『伍』 怎麼用python隨機生成一組整數,把它們按照奇數和偶數來進行分組
『陸』 怎麼對python中列表類型進行分組
如下,將不同的類型及值放到字典中
例如列表lst有int,list,tuple,dict,str,float等類型。
lst = [1,2,3,'54',45.0,'784','string',[1,2,3],(3,6,7),{"no1":1,"no2":2}]
#定義dict_lstype,來對列表lst進行分組
dict_lstype={}
for i in lst:
type_i = type(i)
#如果i的類型在字典中已經存在,則進行追加;如果不存在,則新增一個類型的列表
if type_i in dict_lstype:
dict_lstype[type_i].append(i)
else:
dict_lstype[type_i] = [i]
print dict_lstype
『柒』 python分組處理信息
這個是列表裡面套字典,只要一個for循環就可以解決了
for item in list:
if item[name]==『aaa』:
list1.append[item]
elif item[name]==『bbb』:
list2.append[item]
『捌』 Python 中 dataframe.groupby按多條件分組怎麼做
df.groupby(['YEAR','MONTH','DAY','HOUR'])['TITLE'].apply(lambda x:x.tolist())
或
df.groupby(['YEAR','MONTH','DAY','HOUR'])['TITLE'].apply(lambda x:','.join(x.tolist()))
『玖』 python 里groupby進行分組統計,為什麼老實反饋numpy的事
『拾』 python 字元分組
按照你的思路,以/為分割條件
使用字元串的find方法
S.find(substr, [start, [end]])
#返回S中出現substr的第一個字母的標號,如果S中沒有substr則返回-1。start和end作用就相當於在S[start:end]中搜索
查找到/ask/之後出現的第一個/
這樣可以得到兩個/之間的這個單詞,也就是分類的名字
計數問題可以使用python字典來進行,key不會重復,相同的key對value+1
keywords={}
start_idx=5
withopen('C:\Users\admin\Desktop\a.txt')asf:
forlineinf:
idx=line.find('/',start_idx)
ifidx!=-1:
key=line[start_idx:idx]
ifkeywords.has_key(key):
keywords[key]+=1
else:
keywords[key]=1
printkeywords
以上代碼僅供參考