且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基本的Python循环

更新时间:2023-12-05 14:17:52

哈哈:-)人们正在记下您的问题,但我知道这是每个人都必须想到的一个问题.特别是那些通过在线课程而不是亲自授课来学习Python的人.

Haha :-) People are marking down your question, but I know that is one question must have came in every person's mind. Specially those who learned Python through Online courses and not through a teacher in person.

让我用外行的话解释一下,

Well let me explain that in layman's term,

您使用的方法专门用于1)列表和2)列表中的列表. 例如,

The method that you used is specially used for 1) lists and 2) lists within lists. For eg,

example1= ['a','b','c'] # This is a simple list
example2 = [['a','b','c'],['a','b','c'],['a','b','c']] # This is list within lists.

现在,"a","b"和"c"是列表中的项目.

Now, 'a','b' & 'c' are items in list.

所以说,

for i in example1:
    print i

我们实际上是在说

for item in the list(example1):
    print item

-------------------------

人们使用"i"(可能是item的缩写)或其他名称. 我不知道历史.

-------------------------

People use 'i', probably taken as abbreviation to item, or something else. I don't know the history.

但是,事实是,我们可以使用任何东西代替或使用'i',Python仍将其视为列表中的一项.

But, the fact is that, we can use anything instead or 'i' and Python will still consider it as an item in list.

让我再次给您示例. example1 = ['a','b','c']#这是一个简单的列表 example2 = [['a','b','c'],['a','b','c'],['a','b','c']]#这是列表列表.

Let me give you examples again. example1= ['a','b','c'] # This is a simple list example2 = [['a','b','c'],['a','b','c'],['a','b','c']] # This is list within lists.

for i in example1:
    print i

[out]: a
       b
       c

现在在example2中,项目是列表中的列表. ---而且,现在我将使用单词"item"而不是"i" ---结果无论两者都相同.

now in example2, items are lists within lists. --- also, now i will use the word 'item' instead of 'i' --- the results regardless would be the same for both.

for item in example2:
    print item

[out]: ['a','b','c']
       ['a','b','c']
       ['a','b','c']

人们还使用单数和复数形式来记住事物, 因此,让我们有一个字母列表.

people also use singulars and plurals to remember things, so lets we have a list of alphabet.

letters=['a','b','c','d']
for letter in letters:
    print letter

[out]: a
       b
       c
       d

希望有帮助.还有很多要解释的. 继续研究并继续学习.

Hope that helps. There is much more to explain. Keep researching and keep learning.

关于, 莫欣山