Given+line+=+'From+stephen.marquard@uct.ac.za+Sat+Jan+5+09Given+:14:16+2008';+words+=+line.split(),
时间: 2024-01-01 10:07:01 浏览: 102
根据提供的引用内容,给出以下回答:
给定的字符串是"From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008",我们想要将其拆分成单词。可以使用split()函数来实现这一目标。
```python
line = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
words = line.split()
print(words)
```
输出结果为:
['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008']
相关问题
1、生成一个由1-10的平方组成的列表,并且返回的序列中不存在偶数项。 2、将"From stephen.marquard@uct.ac.za Sat Jan 5 09:14:162008”中的”uct.ac.za”切割并提取出来。
好的,我来回答你的问题:
1. 生成一个由1-10的平方组成的列表,并且返回的序列中不存在偶数项。
可以使用以下代码生成符合要求的列表:
```python
lst = [i**2 for i in range(1, 11) if i%2!=0]
print(lst)
```
输出结果为:`[1, 9, 25, 49, 81]`
2. 将"From stephen.marquard@uct.ac.za Sat Jan 5 09:14:162008”中的”uct.ac.za”切割并提取出来。
可以使用以下代码实现:
```python
email = "From stephen.marquard@uct.ac.za Sat Jan 5 09:14:162008"
domain = email.split('@')[1].split()[0]
print(domain)
```
输出结果为:`uct.ac.za`
Write a program to read through a mail log, build a histogram using a dictionary to count how many messages have come from each.Enter file name: mbox-short.txt {'gopal.ramasammycook@gmail.com': 1, 'louis@media.berkeley.edu': 3, 'cwen@iupui.edu': 5, 'antranig@caret.cam.ac.uk': 1, 'rjlowe@iupui.edu': 2, 'gsilver@umich.edu': 3, 'david.horwitz@uct.ac.za': 4, 'wagnermr@iupui.edu': 1, 'zqian@umich.edu': 4, 'stephen.marquard@uct.ac.za': 2, 'ray@media.berkeley.edu': 1} Download the mbox-short.txt file from the txtfiles folder.
Here's a Python program that reads through a mail log, builds a histogram using a dictionary to count how many messages have come from each:
```python
fname = input("Enter file name: ")
try:
fhand = open(fname)
except:
print("File not found:", fname)
exit()
counts = dict()
for line in fhand:
if line.startswith("From "):
words = line.split()
email = words[1]
counts[email] = counts.get(email, 0) + 1
print(counts)
```
This program prompts the user to enter the file name, opens the file, and then reads through each line. If a line starts with "From ", it splits the line into words and extracts the email address (which is always the second word in this format of log). It then uses a dictionary to keep a count of how many messages have come from each email address. Finally, it prints out the histogram as a dictionary.
阅读全文