在单个variab中引用非嵌套列表

2024-10-02 22:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由没有嵌套的列表组成的变量

 myvariable = [list1][list2][etc]

我只是不知道如何引用变量中的不同列表。myvariable(0)给出list object not callable的错误。显然,嵌套for循环不起作用,我完全没有主意了。你知道吗

当我打印变量时,它会按预期打印所有内容,但return只返回第一个列表。你知道吗

我很抱歉,如果这是真的很明显,但它已经让我疯狂了好几天。你知道吗

谢谢你的帮助。你知道吗

编辑

(抱歉,我不知道如何使代码显示在多行上)

[u'leicestershire', u'virginia', u'Lincs', u'tonyssoccerschool@yahoo', u'.', u'co', u'.', u'uk', u'cleethorpes', u'Grimsby', u'Usually', u'the', u'M25', u'?', u'Grimsby', u'Worldwide', u'Market', u'Rasen', u'Sheffield/Grimsby', u'United', u'States', u'Grimsby', u'Grimsby', u'Grantham', u',', u'Lincolnshire', u'south', u'london/grimsby', u'Grimsby', u'Cleethorpes', u'Lincoln', u',', u'England', u'Grimsby', u'UK', u'London', u',', u'UK', u'Preston', u'Grimsby', u'Lincolnshire', u',', u'UK', u'Swadlincote', u',', u'Derbyshire', u'Hexham', u',', u'Northumberland', u',', u'UK', u'Grimsby', u'Grimsby', u',', u'UK', u'Harrogate', u'Hexham', u',', u'Northumberland', u',', u'UK', u'356', u'Holderness', u'Road', u'-', u'Hull', u'Yorkshire', u',', u'via', u'the', u'M180', u'Grimsby', u'Eltham', u',', u'London', u'United', u'Kingdom', u'Leeds', u',', u'West', u'Yorkshire', u'.', u'London', u'Ross', u'Castle', u'UK', u'(based', u'in', u'Lincolnshire)', u'Wolverhampton', u'#wwfc', u'Little', u'Mountain', u',', u'United', u'States', u'Grimsby', u'Birmingham', u',', u'UK', u'usa', u'/', u'danville', u',', u'va', u'Free', u'Bets', u'-', u'cambridge', u'UK', u'Grimsby', u'Ireland', u'Grimsby', u'Some', u'club', u'..', u'Resides', u'Milton', u'Keynes', u'Dunstable', u'Beds', u'www', u'.', u'facebook', u'.', u'com/3StyleSports', u'London-Alicante', u'Grimsby', u'North', u'Lincolnshire', u'Grimsby', u',', u'England', u'Grimsby', u'Lincolnshire', u'Cleethorpes', u',', u'UK', u'Lincoln', u',', u'UK', u'.', u'England', u'Home', u'Counties', u'/', u'North', u'London', u'Gateshead', u'UK', u'Andalucia', u'Grimsby', u',', u'England', u'Hull', u'(Booo)', u'-', u'UK', u'United', u'Kingdom']
[u'Grimsby', u'Grimsby', u'Grimsby', u'cambridge', u'Grimsby', u'Lincoln', u',', u'UK', u'.', u'www', u'.', u'facebook', u'.', u'com/3StyleSports', u'North', u'Lincolnshire', u'Grimsby', u'Hull', u'(Booo)', u'-', u'UK', u'Wolverhampton', u'#wwfc', u'United', u'Kingdom', u'Grimsby', u'Lincolnshire', u',', u'UK', u'Grimsby', u',', u'England', u'London', u',', u'UK', u'Some', u'club', u'..', u'Birmingham', u',', u'UK', u'Grimsby', u'Worldwide', u'Sheffield/Grimsby', u'Grimsby', u',', u'England', u'UK', u'?', u'cleethorpes', u'Resides', u'Milton', u'Keynes', u'UK', u'Usually', u'the', u'M25', u'Cleethorpes', u'Grimsby', u'Swadlincote', u',', u'Derbyshire', u'Cleethorpes', u',', u'UK', u'England', u'Preston', u'Dunstable', u'Beds', u'Hexham', u',', u'Northumberland', u',', u'UK', u'Lincolnshire', u'Lincs', u'leicestershire', u'Little', u'Mountain', u',', u'United', u'States', u'Lincoln', u',', u'England', u'south', u'london/grimsby', u'tonyssoccerschool@yahoo', u'.', u'co', u'.', u'uk', u'Home', u'Counties', u'/', u'North', u'London', u'UK', u'(based', u'in', u'Lincolnshire)', u'virginia', u'Hexham', u',', u'Northumberland', u',', u'UK', u'Gateshead', u'Leeds', u',', u'West', u'Yorkshire', u'.', u'Grimsby', u',', u'UK', u'London-Alicante', u'Yorkshire', u',', u'via', u'the', u'M180', u'Grimsby', u'Eltham', u',', u'London', u'United', u'Kingdom', u'Ireland', u'usa', u'/', u'danville', u',', u'va', u'London', u'United', u'States', u'Harrogate', u'Grimsby', u'Grimsby', u'Grimsby', u'Ross', u'Castle', u'Market', u'Rasen', u'UK', u'Grimsby', u'356', u'Holderness', u'Road', u'-', u'Hull', u'Andalucia', u'Free', u'Bets', u'-', u'Grantham', u',', u'Lincolnshire', u'Grimsby'][u'London', u'East', u'London', u'&', u'Essex', u'Grimsby', u'bromley', u'uk', u'lincoln', u'Grimsby', u'Grimsby', u',', u'England', u'Great', u'Great', u'Grimsby', u'Liverpool', u'New', u'Waltham', u'Derby', u'Washington', u'DC', u'USA', u'London', u'UK', u'Grimsby', u'liverpool', u'Great', u'Grimsby', u'London', u'\xdcT:', u'19', u'.', u'113285', u',', u'73', u'.', u'0078425', u'east', u'anglia', u'Cleethorpes', u'Somewhere', u'and', u'Anywhere', u'...', u'Here', u'west', u'midlands', u'Glasgow', u'London', u',', u'Crouch', u'End', u'Cottingham', u'Prague', u'Grimsby', u'Norwich', u',', u'England', u'Yorkshire', u',', u'UK', u'Cleethorpes', u'Grimsby', u'Stavanger', u'Grimsby', u'Grimsby', u'cleeeeeeethorpes', u'!', u'Dublin', u',', u'Dundee', u',', u'Humberside', u'Leicester', u'Omnipresent', u'Grimsby', u'Goodison', u'Park', u',', u'Liverpool', u'.', u'GB', u'Lisbon', u'Lincolnshire', u'Norfolk', u'London', u'Cardiff', u',', u'Wales', u'Grimsby', u'Immingham', u',', u'England', u'Grimsby', u',', u'UK', u'.', u'Grimsby', u'Grimsby', u'Near', u'Yorkshire', u'Newcastle', u'North', u'East', u',', u'England', u'Lincoln', u'Cornwall', u',', u'UK', u'Grimsby', u'london', u'town', u'baby', u'London', u'London', u'Town', u'Cleethorpes', u',', u'Lincolnshire', u'Great', u'Grimsby', u'Rawtenstall', u',', u'Rossendale', u'Cleethorpes', u'NEC', u',', u'Birmingham', u'Facebook', u'Page', u'London', u'Grimsby', u'RIght', u'here'][u'Cleethorpes', u'Leeds', u'Grimsby', u'Grimsby', u'Caistor', u',', u'Lincolnshire', u'Surrey', u'Grimsby', u',', u'UK', u'Preston', u'Hong', u'Kong', u'Grimsby', u',', u'United', u'Kigdom', u'Stallingborough', u'Lincolnshire', u',', u"it's", u'ace', u'!', u'grimsby', u'Great', u'Coates', u'Ireland', u'iPhone:', u'50', u'.', u'695671', u',', u'-1', u'.', u'291476', u'grimsby', u'uk', u'Yorba', u'Linda', u'grimsby', u'Lincoln', u',', u'UK', u'Sheffield', u',', u'UK', u'.', u'Manchester', u',', u'England', u'Grimsby', u',', u'NE', u'Lincolnshire', u',', u'UK', u'skegness', u'N/E', u'Lincolnshire', u'Brisbane', u',', u'Australia', u'leafy', u'cheshire', u'Grimsby', u',', u'UK', u'South', u'Killingholme', u'North', u'Pole', u'(well', u',', u'I', u'was', u'...', u')', u'England', u'Immingham', u'.', u'London', u'Knutsford', u'Off', u'Spurn', u'Head', u'grimsby', u'Cleethorpes', u'Yorkshire', u'Bath', u',', u'UK', u'Grimsby', u'Derby', u',', u'UK', u'The', u'dirty', u'South', u'of', u'England', u'costa', u'del', u'grimsby', u'hull\n', u'At', u'Home', u'or', u'Work', u'Grimsby', u'Cleethorpes/Nottingham', u'grimsby', u',', u'uk', u'.', u'chelsea', u'fan', u'Immingham/Louth', u'via', u'Sunderland', u'Brigg', u'/', u'Grimsby', u'/', u'High', u'Wycombe', u'Grimsby', u'Immingham', u',', u'UK', u'Grimsby', u',', u'Lincolnshire', u'Louth', u',', u'Lincolnshire', u'habrough', u'grimsby', u'Reading', u'immingham', u'Grimsby', u'Liverpool', u'Stafford', u'Grimsby', u'Cleethorpes', u'Grimsby', u'Cleethorpes', u'\xdcT:', u'53', u'.', u'549088', u',', u'-0', u'.', u'087738', u'Grimsby', u'Grimsby', u'.', u'uk', u'Grimsby', u'Lincoln', u'Trapped', u'in', u'Doncaster', u'Grimsby', u'Grimsby', u',', u'Lincolnshire', u'Moomin', u'Valley', u'UK', u'Mablethorpe', u',', u'Lincolnshire', u'China', u'all', u'round', u'the', u'globe'][u'Grimsby', u',', u'UK', u'Derbyshire', u'Leeds', u',', u'West', u'Yorkshire', u'Manchester', u'Great', u'Grimsby', u'Grimsby', u'United', u'Kingdom', u'UK', u'Laceby', u'UK', u'Grimsby', u'Aspiring', u'author/poet']

第二个代码块,是变量中包含的所有内容,我认为它是3个非嵌套列表。你知道吗

第一个代码块是由return返回的

编辑2

创建此的代码

   for l in locations:
            Lexer.input(l)
            for ll in Lexer:
                locations1.append(ll.value)

编辑3

当我在代码中使用嵌套for循环时,它不会循环遍历每个列表,然后遍历列表中的每个单词,而是循环遍历第一个列表中的每个单词,然后遍历每个单词中的每个字母。你知道吗

我诚恳地尽可能详细地解释问题,如果我解释得不好的话,我很抱歉。你知道吗

编辑4

return locations1

(返回第一个块)

self.response.out.write(locations1)

打印第二块的内容)


Tags: 代码列表unitedlondonuknorthgreatlincoln
3条回答

你声称你有这样的代码:

myvariable = [list1][list2][etc]

这不会定义三个新的“非嵌套”或“内联”列表并将它们存储在myvariable或其他任何地方。它将做的是:

  • 创建一个元素列表,其元素是list1中的任何值。你知道吗
  • 在单个列表中查找第list2个元素。你知道吗
  • 在结果中查找第etc个元素。你知道吗

如果list2恰好是数字0或-1,而etc恰好是一个整数,它是对list1中任何值的有效索引,这将返回该值。否则,它只会引发TypeErrorIndexError。(好吧,在某些情况下,slice对象也可以工作,但我们忽略它。)并且,根据名称,它可能是一个列表,它将提高TypeError。你知道吗


最有可能的是,你真的没有list1list2etc任何地方,但像这样:

myvariable = ['a', 'b']['c', 'd']['e', 'f']

这肯定会引发一个TypeError,因为您试图使用元组'c', 'd'作为列表['a', 'b']的索引,而您不能这样做。你知道吗


你能写的一件事是:

myvariable = ['a', 'b'], ['c', 'd'], ['e', 'f']

所做的是定义一个由三个列表组成的元组。这是完全正确的。它的工作原理非常类似于一个由三个列表组成的列表。例如:

>>> myvariable[0]
['a', 'b']
>>> myvariable[0][1]
'b'

你可以写的另一件事是:

myvariable = ['a', 'b']
['c', 'd']
['e', 'f']

这将myvariable设置为['a', 'b'],然后定义另外两个列表以立即忘记它们。你知道吗

因此,您可以稍后使用myvariable来访问第一个列表的元素,因为您已经将它作为第一个列表的名称,但是无法访问其他两个列表的元素,因为您没有将它们存储在任何位置。你知道吗


同时,您声称“创建此的代码”是:

for l in locations:
    Lexer.input(l)
    for ll in Lexer:
        locations1.append(ll.value)

大概在这上面有个locations1 = []。你知道吗

如果是这样的话,就有一个列表locations1。根本没有其他列表可供访问。你知道吗


看看你的第一个例子,你称之为“内联列表”:

[u'leicestershire', u'virginia', u'Lincs', u'tonyssoccerschool@yahoo', u'.', u'co', u'.', u'uk', u'cleethorpes', u'Grimsby', u'Usually', u'the', u'M25', u'?', u'Grimsby', u'Worldwide', u'Market', u'Rasen', u'Sheffield/Grimsby', u'United', u'States', u'Grimsby', u'Grimsby', u'Grantham', u',', u'Lincolnshire', u'south', u'london/grimsby', u'Grimsby', u'Cleethorpes', u'Lincoln', u',', u'England', u'Grimsby', u'UK', u'London', u',', u'UK', u'Preston', u'Grimsby', u'Lincolnshire', u',', u'UK', u'Swadlincote', u',', u'Derbyshire', u'Hexham', u',', u'Northumberland', u',', u'UK', u'Grimsby', u'Grimsby', u',', u'UK', u'Harrogate', u'Hexham', u',', u'Northumberland', u',', u'UK', u'356', u'Holderness', u'Road', u'-', u'Hull', u'Yorkshire', u',', u'via', u'the', u'M180', u'Grimsby', u'Eltham', u',', u'London', u'United', u'Kingdom', u'Leeds', u',', u'West', u'Yorkshire', u'.', u'London', u'Ross', u'Castle', u'UK', u'(based', u'in', u'Lincolnshire)', u'Wolverhampton', u'#wwfc', u'Little', u'Mountain', u',', u'United', u'States', u'Grimsby', u'Birmingham', u',', u'UK', u'usa', u'/', u'danville', u',', u'va', u'Free', u'Bets', u'-', u'cambridge', u'UK', u'Grimsby', u'Ireland', u'Grimsby', u'Some', u'club', u'..', u'Resides', u'Milton', u'Keynes', u'Dunstable', u'Beds', u'www', u'.', u'facebook', u'.', u'com/3StyleSports', u'London-Alicante', u'Grimsby', u'North', u'Lincolnshire', u'Grimsby', u',', u'England', u'Grimsby', u'Lincolnshire', u'Cleethorpes', u',', u'UK', u'Lincoln', u',', u'UK', u'.', u'England', u'Home', u'Counties', u'/', u'North', u'London', u'Gateshead', u'UK', u'Andalucia', u'Grimsby', u',', u'England', u'Hull', u'(Booo)', u'-', u'UK', u'United', u'Kingdom']

没有[,只有开头的那一个,没有],只有结尾的那一个(连引号都没有)。这只是一个大名单。你知道吗

因此:

when i use a nested for loop on this code, it doesnt loop through each list and then each word in the list, it instead loops over every word in the first list and then every letter in each word.

这正是您对单个字符串列表的期望。(一个字符串本身就是一个由一个字符串组成的序列。)这是一个很好的证据,无论你认为你拥有什么,你实际上拥有的只是一个字符串列表。你知道吗

因为在一个变量中有一个列表集合,所以必须至少有一层嵌套。外部iterable本身不必是列表(它可以是元组或其他iterable),但它仍然是列表的容器。您试图用下标访问它(括号实际上对下标无效,但基于您使用整数的事实,我假设您使用的是下标),因此您可能确实有一个列表列表,即使内部列表本身没有进一步嵌套。你知道吗

>>> myvariable=[['a'],['list', 'of'],['Liszts']]
>>> print myvariable[1]
['list', 'of']

如果您的iterable实际上是一个生成器,那么您实际上无法通过订阅访问它们,必须通过调用next()一次访问一个,或者将整个hog转换为一个列表列表。你知道吗

尝试用方括号myvariable[0]。你知道吗

>>> myvariable = [[1,2,3],[4,5,6],[7,8,9]]
>>> myvariable[0]
[1, 2, 3]
>>> 

相关问题 更多 >