-6

ネストされていないリストで構成される変数があります

 myvariable = [list1][list2][etc]

変数内のさまざまなリストを参照する方法がわかりません。myvariable(0)list object not callable のエラーが発生します。明らかにネストされた for ループは機能しません。私は完全にアイデアがありません。

変数を印刷すると、期待どおりにすべてが印刷されますが、最初のリストのみが返されます。

これが本当に明白な場合は申し訳ありませんが、何日も私を夢中にさせています。

ご協力いただきありがとうございます。

編集

(申し訳ありませんが、コードを複数行に表示する方法がわかりません)

[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']

2 番目のコード ブロックは、変数内に含まれるすべてのものです。ネストされていない 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)

2番目のブロックを出力するもの)

4

3 に答える 3

0

myvariable[0]角かっこで試してください。

>>> myvariable = [[1,2,3],[4,5,6],[7,8,9]]
>>> myvariable[0]
[1, 2, 3]
>>> 
于 2013-04-30T17:37:22.680 に答える
0

単一の変数にリストのコレクションがあるため、ネストのレイヤーが少なくとも 1 つある必要があります。外側の iterable は、それ自体がリストである必要はありません (タプルまたはその他の iterable である可能性があります) が、それでもリストのコンテナーです。あなたは添え字でアクセスしようとしています(括弧は実際には添え字には有効ではありませんが、整数を使用したという事実に基づいて、添え字を使用していると思います)、実際にはリストのリストを持っていると思われます。内部リスト自体はさらにネストされていません。

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

イテラブルが実際にジェネレーターである場合、添字を使用してそれらに実際にアクセスすることはできず、一度に 1 つずつアクセスする必要がnext()あります。

于 2013-04-30T17:37:50.373 に答える