我有一个像下面这样的巢列表:
public interface JournalReportRepository extends JpaRepository<TxnTransactionLogEntity, String>{
@Query(value = "SELECT new com.bbi.rrs.dto.JournalDto(a.txnReferenceNo, c.accountNo, c.accountName, d.description, a.currencyCode , a.currencyCode, a.txnDate, a.txnAmt, a.smsUserInfo, a.smsUserInfo) "
+ " FROM TxnTransactionLogEntity a, TxnBalanceEntity b, SmsAccountEntity c, SmsNominalAcodeEntity d WHERE a.smsAccount = c.accountNo AND b.smsAccount = c.accountNo and b.smsNominalAcode = d.nominalAcodeId")
public Page<JournalDto> findJournalReportData(Pageable pageable);
我想要一本字典来显示数字显示计数,字典应该如下:
list = [
[1, 2, 4, 5],
[2, 3, 4, 5],
[1, 3, 4, 5, 6],
[1, 2, 3],
]
怎么弄这个?
答案 0 :(得分:3)
首先,可以通过将所有元素(将内部元素从列表中删除)组合到单个列表然后使用Counter:
来展平列表{1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 1}
结果:
Counter
如果您希望该项目为关键字,可能会将每个项目转换为字符串并将其传递给dict(Counter(str(inneritem) for item in list for inneritem in item))
:
>>> a = set(['a', 'b', 'c'])
>>> b = set(['c', 'd', 'e'])
>>> c = a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and 'set'
>>>
Expected output:
c = set(['a','b','c', 'd', 'e'])
答案 1 :(得分:2)
首先将变量从list
更改为其他名称my_list
您需要首先展平您的列表,然后使用计数器计算元素。
from collections import Counter
from itertools import chain
print(dict(Counter(chain(*my_list))))
out:
{1: 3, 2: 3, 4: 3, 5: 3, 3: 3, 6: 1}
如果您想在案例中更改类似数据类型,也可以使用列表理解
print(
dict(
Counter(
[
str(item)
for sub_list in my_list
for item in sub_list
]
)
)
)# or
#print(dict(Counter([str(item) for sub_list in my_list for item in sub_list])))
out:
{'1': 3, '2': 3, '4': 3, '5': 3, '3': 3, '6': 1}
答案 2 :(得分:1)
很容易做到这一点,只需一行代码:
import collections
lst = [
[1, 2, 4, 5],
[2, 3, 4, 5],
[1, 3, 4, 5, 6],
[1, 2, 3],
]
dict(collections.Counter(sum(lst, [])))
请不要使用list
作为您的变量名称,此处我使用lst
代替。
答案 3 :(得分:0)
{'1': 3, '2': 3, '3': 3, '4': 3, '5': 3, '6': 1}
结果:
{{1}}
答案 4 :(得分:0)
实际逻辑只需一行(链接list
并将Counter
应用于其中):
from collections import Counter
from itertools import chain
Counter(chain.from_iterable(list))
PS。考虑将变量名称list
重命名为其他内容,因为它是Python关键字。
答案 5 :(得分:0)
"${FQHN,,}"
答案 6 :(得分:0)
如果我理解正确,你有一个列表列表,并希望添加一个值出现在列表列表中的次数总和(1-6),并希望得到值出现的总和次数作为dict中的值,具体数字(1-6)将成为dict的“关键”。下面是一些代码,可以为您提供所需的输出:
<?php
include('config.php');
if(isset($_POST['submit']))
{
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$mob=$_POST['mob'];
$pass=$_POST['pass'];
$ins="insert into fbsign (fname,sname,mob,pass)values('$fname','$sname','$mob','$pass')";
$ex=$con->query($ins);
if($ex)
{
echo "successfully inserted::";
}
else
{
echo "ERROR::";
}
}
?>
您的输出将是您想要的字典,其中键是数字,值是特定数字出现在列表列表中的次数。输出结果如下:
list_ = [
[1, 2, 4, 5],
[2, 3, 4, 5],
[1, 3, 4, 5, 6],
[1, 2, 3],
]
one = []
two = []
three = []
four = []
five = []
six = []
for num in list_[0]:
if num == 1:
one.append(num)
if num == 2:
two.append(num)
if num == 3:
three.append(num)
if num == 4:
four.append(num)
if num == 5:
five.append(num)
if num == 6:
six.append(num)
for num in list_[1]:
if num == 1:
one.append(num)
if num == 2:
two.append(num)
if num == 3:
three.append(num)
if num == 4:
four.append(num)
if num == 5:
five.append(num)
if num == 6:
six.append(num)
for num in list_[2]:
if num == 1:
one.append(num)
if num == 2:
two.append(num)
if num == 3:
three.append(num)
if num == 4:
four.append(num)
if num == 5:
five.append(num)
if num == 6:
six.append(num)
for num in list_[3]:
if num == 1:
one.append(num)
if num == 2:
two.append(num)
if num == 3:
three.append(num)
if num == 4:
four.append(num)
if num == 5:
five.append(num)
if num == 6:
six.append(num)
list_dict = {}
list_dict['1'] = len(one)
list_dict['2'] = len(two)
list_dict['3'] = len(three)
list_dict['4'] = len(four)
list_dict['5'] = len(five)
list_dict['6'] = len(six)
print(list_dict)
答案 7 :(得分:0)
您可以这样做:
list1 = [
[1, 2, 4, 5],
[2, 3, 4, 5],
[1, 3, 4, 5, 6],
[1, 2, 3],
]
count={}
for i in list1:
for j in i:
if j not in count:
count[j]=1
else:
count[j]+=1
print(count)
输出:
{1: 3, 2: 3, 3: 3, 4: 3, 5: 3, 6: 1}