我在更新嵌套的defaultdict中的列表时遇到问题。
这是我的代码:
a = ['20160115', 'shadyside medical building', 1, 'Review']
b = ['20160115', 'shadyside medical building', 1, 'Video']
c = ['20160215', 'shadyside medical building', 1, 'Video']
d = ['20160215', 'medical building', 1, 'Video']
f = [a,b,c,d]
nested_dict = defaultdict(dict)
for date,keyword,pos,feature in f:
nested_dict[keyword].update({feature : [pos]})
nested_dict[keyword].update({feature : [pos]})
这是输出:
{'shadyside medical building':
{'Review': [1],
'Video': [1]},
'medical building':
{'Video': [1]}}
所需的输出是:
{'shadyside medical building':
{'Review': [1],
'Video': [1,1]},
'medical building':
{'Video': [1]}}
请注意,视频的第二项已添加到视频列表中。
答案 0 :(得分:6)
你没有嵌套任何defaultdict
,所以这样做:
nested_dict = defaultdict(lambda: defaultdict(list))
和
nested_dict[keyword][feature].append(pos)
答案 1 :(得分:0)
您还可以创建一个无限嵌套的defaultdict:
public static void main(String[] args) {
Banking[] uaccounts;
uaccounts = new Banking[100]; // setting up array
int arrcount = 0; // setting counter for array
int menu = 1; // Creating a variable to loop menu
while (menu == 1) { //Creating the loop for the menu
System.out.println("|=================================================|"); // drawing the menu
System.out.println("| |");
System.out.println("| MAIN MENU |");
System.out.println("| 1. Create Account (BANK MANAGER RESTRICTED) |");
System.out.println("| 2. Check Account Balance |");
System.out.println("| 3. Deposit Into Account |");
System.out.println("| 4. Withdraw From Account |");
System.out.println("| 5. Quit |");
System.out.println("| |");
System.out.println("|=================================================|");
int uinput = Integer.parseInt(JOptionPane.showInputDialog("Please select an option from above(1 - 4):"));
if (uinput == 1) { // initiating menu option 1
String full_name = JOptionPane.showInputDialog("Please enter clients full name:"); // creating some variables
String address = JOptionPane.showInputDialog("Please enter the clients address:");
String password = JOptionPane.showInputDialog("Please enter the clients password:");
int accid = RandomInt(100000, 999999);
System.out.println("Customers Account ID:" + accid);
double balance = Double.parseDouble(JOptionPane.showInputDialog("Please enter the client's starting balance:"));
uaccounts[arrcount] = new Banking(full_name, address, accid, balance, password); // creating objectg in array
arrcount = arrcount + 1; //adding to counter for next time
}
if (uinput == 2) { // initiating menu option 2
int acc_id_input = Integer.parseInt(JOptionPane.showInputDialog("Please enter your account ID:"));
String password_input = JOptionPane.showInputDialog("Please enter your password:");
int arrcount2 = arrcount - 1;
LoginUser(acc_id_input, password_input, uaccounts, arrcount2);
}
public static void LoginUser(int account_id, String password, Banking[] uaccounts, int arrcount) {
boolean found = false;
int accIndex = 0;
for (int i = 0; i < arrcount - 1; i++) {
if ((uaccounts[i].accid == account_id) && (uaccounts[i].password.equals(password))) {
found = true;
accIndex = i;
break;
} else {
found = false;
}
}