简体   繁体   English

Python 'for' 循环随机停止

[英]Python 'for' loop stopping randomly

I realize how poorly worded the title is, but I am beyond lost as to what the actual problem is to begin with as I'm not getting much predictable behavior.我意识到这个标题的措辞是多么糟糕,但我对真正的问题是什么一无所知,因为我没有得到太多可预测的行为。 So feel free to suggest changes or whatever.因此,请随时提出更改或其他建议。

I'm trying to iterate through a list of words followed by identifiers (ie. 'Bus', 'n.', 'C1') and classify the words themselves into one one of several lists of dictionary values based on the identifiers that follow each word.我正在尝试遍历后跟标识符(即'Bus'、'n.'、'C1')的单词列表,并根据后面的标识符将单词本身分类到几个字典值列表之一每个字。

My problem is that I cannot get Python to finish iterating through the list.我的问题是我无法让 Python 完成对列表的迭代。 Conditions are obviously met and then disregarded.条件显然得到满足,然后被忽略。

I've tried using 'elif' (it shouldn't matter if the conditions are exactly met?) and reodering the if/elif statements to no avail.我尝试过使用“elif”(如果条件完全满足,这无关紧要?)并重新编码 if/elif 语句无济于事。 The current script below iterates further than any other configuration I tried.下面的当前脚本比我尝试的任何其他配置迭代得更远。

dict = {'noun':[], 'verb':[], 'adj':[], 'adv':[], 'prep':[]}

oxfi = "adaptation n. C1 addiction n. B2 additionally adv. B2 adequate adj. B2 adequately adv. B2 adhere v. C1 adjacent adj. C1 adjust v. B2 adjustment n. C1 administer v. C1 administrative adj. C1 administrator n. C1 admission n. C1 adolescent n. C1 adoption n. C1 adverse adj. C1 advocate n., v. C1 aesthetic adj. C1 affection n. C1 affordable adj. B2"
oxfi = oxfi.split()

for word in oxfi:
    if oxfi[1] == 'n.,' and oxfi[2] == 'v.':
        dict['noun'].append(oxfi[0])
        dict['verb'].append(oxfi[0])
        del oxfi[0:4]
    if oxfi[1] == 'n.':
        dict['noun'].append(oxfi[0])
        del oxfi[0:3]
    if oxfi[1] == 'adj.':
        dict['adj'].append(oxfi[0])
        del oxfi[0:3]
    if oxfi[1] == 'v.':
        dict['verb'].append(oxfi[0])
        del oxfi[0:3]
    if oxfi[1] == 'adv.':
        dict['adv'].append(oxfi[0])
        del oxfi[0:3]
    if oxfi[1] == 'prep.':
        dict['prep'].append(oxfi[0])
        del oxfi[0:3]

print("oxfi = " + str(oxfi) + '\n')
print("dict = " + str(dict) + '\n')

I'm an intermediate Python hobbyist, and will admit this has me totally stumped.我是一名中级 Python 爱好者,我承认这让我很困惑。 Any help is greatly appreciated!!任何帮助是极大的赞赏!!

That is because you are deleting the elements from the list you are iterating in. I suggest doing something like that那是因为你正在从你正在迭代的列表中删除元素。我建议做类似的事情

oxfi = "adaptation n. C1 addiction n. B2 additionally adv. B2 adequate adj. B2 adequately adv. B2 adhere v. C1 adjacent adj. C1 adjust v. B2 adjustment n. C1 administer v. C1 administrative adj. C1 administrator n. C1 admission n. C1 adolescent n. C1 adoption n. C1 adverse adj. C1 advocate n., v. C1 aesthetic adj. C1 affection n. C1 affordable adj. B2"
oxfi = oxfi.split()
oxfier = oxfi.split()

for word in oxfi:
    if oxfi[1] == 'n.,' and oxfi[2] == 'v.':
        dict['noun'].append(oxfier[0])
        dict['verb'].append(oxfier[0])
        del oxfier[0:4]
    if oxfi[1] == 'n.':
        dict['noun'].append(oxfier[0])
        del oxfier[0:3]
    if oxfi[1] == 'adj.':
        dict['adj'].append(oxfier[0])
        del oxfier[0:3]
    if oxfi[1] == 'v.':
        dict['verb'].append(oxfier[0])
        del oxfier[0:3]
    if oxfi[1] == 'adv.':
        dict['adv'].append(oxfier[0])
        del oxfier[0:3]
    if oxfi[1] == 'prep.':
        dict['prep'].append(oxfier[0])
        del oxfier[0:3]

print("oxfi = " + str(oxfier) + '\n')
print("dict = " + str(dict) + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM