python - 当我在 Python Tutor 上运行代码时,我的代码一直显示错误:“太多无法解包”。如何修复此错误?
问题描述
这是我的代码:
big_str = "[41.386263640000003, -81.494450689999994]\t6\t2011-08-28 19:02:28\tyay. little league world series!\n[42.531041999999999, -82.90854831]\t6\t2011-08-28 19:02:29\ti'm at holiday inn express suites & hotel roseville mi (31900 little mack ave., at masonic blvd., roseville) \n[39.992309570000003, -75.131119729999995]\t6\t2011-08-28 19:02:29\t@_tweetthis what dorm r you in?\n[54.104106119999997, 28.336019929999999]\t6\t2011-08-28 19:02:29\t@andykozik круто !!!\n[25.787949600000001, -80.132949600000003]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw"
# This breaks up a string of tweets into lists
def break_into_records(big_mess):
rows = big_mess.split('\n')
records = []
for row in rows:
records.append(row.split('\t'))
for i in range(len(records)):
records[i][0] = records[i][0].replace(' ','').strip('[]').split(',')
return records
# This will loop over that list, and call a draw
# every time a word pops up
# as well as give you a word count
def count_words(word, data_structure):
count = 0
for loc, id, time, message in data_structure:
if word in message:
drop_pin(loc) # we pass cords to drop pin, no work to do
count += message.count(word)
return count
def drawGpsPoint(x):
y = drop_pin(x)
return y
def drop_pin(location):
# this function needs to map the GPS cord to an x/y
# so it can draw
# but it has the GPS locs.
lat, lon = location
lat, lon = float(lat), float(lon)
x = 500 - ((lat + 180) * 500.0/360)
y = 500 - ((lon + 180) * 500.0/360)
return lat, lon
a = break_into_records(big_str)
for x in a:
drawGpsPoint(x)
print("All Tweets Loaded")
r = raw_input("Enter search word: ")
每次到达 drop_pin(location) 函数中的 lat, lon 时都会显示错误。在通过该行运行之前,我是否需要先分离变量?
解决方案
你break_into_records
没有做你所期望的。第一次打电话时drop_pin(location)
,您的位置是
[['41.386263640000003', '-81.494450689999994'], '6', '2011-08-28 19:02:28', 'yay. little league world series!']
请注意,我location
在尝试分配之前通过打印出来确定了这一点。
“解包的值太多”错误是由于尝试将其分配给lat, long
.
您可能只想返回break_into_records
函数的第一部分,或者您可能只想对这些字符串的第一部分进行操作。
推荐阅读
- rest - 为需要为每个请求保存数据的 REST API 的端点建模
- amazon-web-services - 配置 AWS API 网关时是否需要 NAT 网关?
- postgresql - 为什么我的 luminus 应用不能在 Heroku 上运行?
- reactjs - React 路由器在 URL 栏中不起作用,但它仍然适用于标签
- unicode - Ocaml:Unicode 字符串长度
- javascript - 循环遍历两个对象数组以循环匹配 id。然后根据自定义值返回一个新的自定义对象数组
- pytorch - PyTorch 训练的模型可以在 GPU 和 TPU 之间转移吗?
- sql-server - 创建一个 Powershell 脚本以禁用大多数 SQL Server 登录
- c - 每次进入循环时,如何从文本文件中读取下一行?
- python - 不确定为什么我在使用搁置时遇到回溯问题