所以我有一个这样的单词表:
['james,1', 'deb,10', 'bad,1']
因此,列表中的每个元素都包含一个单词和一个数字,其中我最终需要从单独的文本中找到该单词的出现次数,然后添加该数字,但是现在我不知道的是如何解决每个元素用逗号分隔的第一和第二“部分”,以便以后可以在代码中实现以查找单词的外观。例如,我如何解决“惊奇”一词?如果单词“ james”本身在第二个元素中,我知道我可以执行wordlist [1],但是在这种情况下它将如何工作?我是否必须将此列表拆分为嵌套列表或其他内容?如果可以,怎么办?
答案 0 :(得分:1)
如果需要字符串的嵌套元组对:
ml = ['alone,1', 'amazed,10', 'bad,1', 'best,10', 'better,10', 'excellent,10', 'excite,10', 'good,7', 'great,7', 'happy,10', 'hard,5', 'hardest,4', 'hate,1', 'like,7', 'love,10', 'loves,10', 'need,1', 'regret,1', 'sad,1', 'sorry,1', 'thank,5', 'tired,1', 'worst,1']
nums = [(x.split(",")[0],int(x.split(",")[1])) for x in ml]
print(nums)
输出:
('alone', 1), ('amazed', 10), ('bad', 1), ('best', 10), ('better', 10), ('excellent', 10), ('excite', 10), ('good', 7), ('great', 7), ('happy', 10), ('hard', 5), ('hardest', 4), ('hate', 1), ('like', 7), ('love', 10), ('loves', 10), ('need', 1), ('regret', 1), ('sad', 1), ('sorry', 1), ('thank', 5), ('tired', 1), ('worst', 1)]
最好在早期代码后获得字典:
nums = dict(nums)
print(nums)
输出:
{'alone': 1, 'amazed': 10, 'bad': 1, 'best': 10, 'better': 10, 'excellent': 10, 'excite': 10, 'good': 7, 'great': 7, 'happy': 10, 'hard': 5, 'hardest': 4, 'hate': 1, 'like': 7, 'love': 10, 'loves': 10, 'need': 1, 'regret': 1, 'sad': 1, 'sorry': 1, 'thank': 5, 'tired': 1, 'worst': 1}
使用列表推导仅获取整数部分:
ml = ['alone,1', 'amazed,10', 'bad,1', 'best,10', 'better,10', 'excellent,10', 'excite,10', 'good,7', 'great,7', 'happy,10', 'hard,5', 'hardest,4', 'hate,1', 'like,7', 'love,10', 'loves,10', 'need,1', 'regret,1', 'sad,1', 'sorry,1', 'thank,5', 'tired,1', 'worst,1']
nums = [int(x.split(",")[1]) for x in ml]
print(nums)
输出:
[1, 10, 1, 10, 10, 10, 10, 7, 7, 10, 5, 4, 1, 7, 10, 10, 1, 1, 1, 1, 5, 1, 1]
答案 1 :(得分:0)
from tkinter import *
def mainlogin():
# Root
root = Tk()
root.title("Login")
root.geometry("240x110")
root.protocol("WM_DELETE_WINDOW", disable_event)
# Globals
global allow_entry
allow_entry = False
# Login Credentials
my_username = 'abcd'
my_password = '1234'
# Login Function
def login(event):
if my_username == username.get() and my_password == pswd.get():
allow_entry = True
root.quit()
else:
allow_entry = False
wrong_input = Label(root, text="Wrong username or password!")
wrong_input.pack(padx=5, pady=5)
# Enter Key Bind
root.bind("<Return>", login)
# Login entry widgets
username = Entry(root)
username.pack(padx=5, pady=5)
pswd = Entry(root, show="*")
pswd.pack(padx=5, pady=5)
# Buttons
login_button = Button(root, text="Login", command=login)
login_button.pack()
### MAIN LOOP ###
root.mainloop()
def quit_login():
allow_entry = False
root.quit()
def disable_event():
pass
### NAME == MAIN ###
if __name__ == "__main__":
mainlogin()
答案 2 :(得分:0)
您可以在此过程中使用dict
Widget _bottom() {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Container(
color: Colors.amberAccent,
width: double.infinity,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: new List<int>.generate(50, (index) => index + 1)
.map((item) {
return Text(
item.toString(),
style: TextStyle(fontSize: 20),
);
}).toList(),
),
),
),
),
Container(
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'BoTToM',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 33),
),
],
),
),
],
);
}