假设我的词典包含
{'one': '1','two': '2', ..., 'hundred': '100'}
我的清单包含
[['for 30']['for thirty']]
我想将列表中的字符串拆分为:
[for],[30] and [for],[thirty]
然后我想使用'thirty'
与字典中的相应条目进行比较,并获得'30'
作为输出。
import csv
reader = csv.reader(open('dict.csv', 'r'))
d = {}
for row in reader:
k, v = row
d[k] = v
with open('test_term.csv', 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
答案 0 :(得分:0)
欢迎使用Python!您应该阅读一些基础知识,例如如何声明列表和字典。
我猜你要做的是:
# this is how you format a list
pair_list = ['for 30', 'for thirty', 'for 1', 'for two']
# this is how you format a dict
resolve_dict = {'one': '1', 'thirty': '30', 'hundred': '100'}
# iterate the list two strings at a time
for idx in xrange(len(pair_list)):
# example in comments
# pairs[idx] == 'for 30'
splitted = pair_list[idx].split(' ')
# splitted == ['for', '30']
number = splitted[1]
# number == '30'
# pairs[idx + 1] == 'for thirty'
splitted = pair_list[idx + 1].split(' ')
# splitted == ['for', 'thirty']
word = splitted[1]
# word == 'thirty'
# comparison
if resolve_dict[word] == number:
print 'hurray?'