2D 列表。在 Python 中打印列表的前 5 个项目。

huangapple go评论66阅读模式
英文:

2d list. Print 5 items of a list in python

问题

I am reading data from a CSV file that has several rows and columns.

Objective: Print the first twenty rows and eight attributes of each row.

The following successfully prints all the entire 2d list:

import csv

rows = []
with open('googleplay.csv') as gps_file:
    csv_reader = csv.reader(gps_file)
    line_counter = 0
    for row in csv_reader:
        rows.append(row)
        print(row)

Output:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M', '2000000', 'Free', '0', 'Teen', 'Strategy', 'May 18 2018', '2.0081_206', '4.0.3 and up']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M', '2200000', 'Free', '0', 'Teen', 'Strategy', 'July 26 2018', '1.8.2', '4.4 and up']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M', '3000000', 'Paid', '4.99', 'Everyone', 'Puzzle', 'April 18 2018', '1.0.1', '4.4 and up']
...

The following prints out the first 20 rows of the 2d list:

for item in range(20): # Select the first 20 rows of the data
    print(rows[item]) # print out the first 20 rows

Output:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M', '2000000', 'Free', '0', 'Teen', 'Strategy', 'May 18 2018', '2.0081_206', '4.0.3 and up']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M', '2200000', 'Free', '0', 'Teen', 'Strategy', 'July 26 2018', '1.8.2', '4.4 and up']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M', '3000000', 'Paid', '4.99', 'Everyone', 'Puzzle', 'April 18 2018', '1.0.1', '4.4 and up']
...

(Note: I have replaced HTML entities with their corresponding characters in the code.)

英文:

I am reading data from a CSV file that has several rows and columns.

Objective: Print the first twenty rows and eight attributes of each row.

The following successfully prints all the entire 2d list:

import csv

rows = []
with open('googleplay.csv') as gps_file:
    csv_reader = csv.reader(gps_file)
    line_counter = 0
    for row in (csv_reader):
        rows.append(row)
        print(row)

Output:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M', '2000000', 'Free', '0', 'Teen', 'Strategy', 'May 18 2018', '2.0081_206', '4.0.3 and up']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M', '2200000', 'Free', '0', 'Teen', 'Strategy', 'July 26 2018', '1.8.2', '4.4 and up']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M', '3000000', 'Paid', '4.99', 'Everyone', 'Puzzle', 'April 18 2018', '1.0.1', '4.4 and up']
['Farming Simulator 18', 'GAME', '4.5', '18125', '15M', '5000000', 'Paid', '4.99', 'Everyone', 'Simulation;Education', 'July 9 2018', 'Varies with device', '4.4 and up']
['RollerCoaster Tycoon¸ Classic', 'GAME', '4.6', '10795', '69M', '5000000', 'Paid', '5.99', 'Everyone', 'Simulation', 'December 21 2017', '1.2.1.1712080', '4.0.3 and up']
['Star Wars ?: DIRTY', 'GAME', '4.5', '38207', '15M', '5000000', 'Paid', '9.99', 'Teen', 'Role Playing', 'October 19 2015', '1.0.6', '4.1 and up']
['Ice Crush 2018 - A new Puzzle Matching Adventure', 'GAME', '4.6', '15403', '25M', '1000000', 'Free', '0', 'Everyone', 'Casual', 'August 2 2018', '1.8.1', '4.1 and up']
['TerraGenesis - Space Colony', 'GAME', '4.3', '38957', 'Varies with device', '1000000', 'Free', '0', 'Everyone', 'Simulation', 'August 2 2018', '4.9.31', 'Varies with device']
['SHADOWGUN LEGENDS', 'GAME', '4.6', '100609', '52M', '1000000', 'Free', '0', 'Teen', 'Action', 'July 16 2018', '0.5.4', '6.0 and up']
['Dino War: Rise of Beasts', 'GAME', '4.1', '18996', '81M', '1000000', 'Free', '0', 'Teen', 'Strategy', 'August 1 2018', '1.5.2', '4.1 and up']
['Tokyo Ghoul: Dark War', 'GAME', '4.3', '25094', '82M', '1000000', 'Free', '0', 'Teen', 'Role Playing', 'July 6 2018', '1.2.0', '4.3 and up']
['Chapters: Interactive Stories', 'GAME', '4.5', '73539', '96M', '1000000', 'Free', '0', 'Mature 17+', 'Role Playing', 'August 2 2018', '1.3.3', '4.3 and up']
['Honkai Impact 3rd', 'GAME', '4.7', '59017', '82M', '1000000', 'Free', '0', 'Teen', 'Action', 'July 3 2018', '2.2.1', '4.3 and up']
['The Game of Life', 'GAME', '4.4', '18621', '63M', '1000000', 'Paid', '2.99', 'Everyone', 'Board', 'July 4 2018', '2.1.2', '4.4 and up']
['The Escapists', 'GAME', '4.4', '7412', '84M', '1000000', 'Paid', '4.99', 'Teen', 'Strategy', 'April 26 2018', '1.1.0', '2.3 and up']
['Hero Hunters', 'GAME', '4.4', '70747', '99M', '5000000', 'Free', '0', 'Teen', 'Action', 'July 31 2018', '1.7', '4.4 and up']
['Merge Dragons!', 'GAME', '4.5', '214777', '91M', '5000000', 'Free', '0', 'Everyone', 'Puzzle', 'July 30 2018', '3.6.0', '4.1 and up']
['Last Shelter: Survival', 'GAME', '4.4', '93033', '98M', '5000000', 'Free', '0', 'Teen', 'Strategy', 'August 4 2018', '1.250.078', '4.0.3 and up']
['Lineage 2: Revolution', 'GAME', '4.2', '187972', '99M', '5000000', 'Free', '0', 'Teen', 'Role Playing', 'July 11 2018', '1.05.20', '4.4 and up']
['DRAGON BALL LEGENDS', 'GAME', '4.6', '337913', '48M', '5000000', 'Free', '0', 'Teen', 'Action', 'August 3 2018', '1.11.0', '6.0 and up']
['MARVEL Strike Force', 'GAME', '4.3', '165928', '91M', '5000000', 'Free', '0', 'Teen', 'Role Playing', 'July 31 2018', '1.4.0', '4.4 and up']
['Jurassic World? Alive', 'GAME', '4.3', '309176', '70M', '5000000', 'Free', '0', 'Everyone 10+', 'Simulation', 'August 2 2018', '1.3.15', '4.4 and up']
['Merge Dragons!', 'GAME', '4.5', '214819', '91M', '5000000', 'Free', '0', 'Everyone', 'Puzzle', 'July 30 2018', '3.6.0', '4.1 and up']
['Monument Valley 2', 'GAME', '4.6', '9394', '33M', '9000000', 'Paid', '4.99', 'Everyone', 'Puzzle', 'November 30 2017', '1.2.9', '4.4 and up']
["Cooking Madness - A Chef's Restaurant Games", 'GAME', '4.7', '358817', '49M', '10000000', 'Free', '0', 'Everyone', 'Arcade', 'July 30 2018', '1.2.2', '4.1 and up']
['RULES OF SURVIVAL', 'GAME', '4.2', '1343866', '56M', '10000000', 'Free', '0', 'Teen', 'Action', 'August 1 2018', '1.180271.184729', '4.0 and up']
['Mafia City', 'GAME', '4.5', '168717', '56M', '10000000', 'Free', '0', 'Mature 17+', 'Strategy', 'August 3 2018', '1.3.205', '2.3 and up']
['Cash Inc. Money Clicker Game & Business Adventure', 'GAME', '4.8', '549720', '85M', '10000000', 'Free', '0', 'Everyone', 'Simulation', 'July 19 2018', '2.1.1.4.0', '4.4 and up']
['Guns of Glory', 'GAME', '4', '120592', '77M', '10000000', 'Free', '0', 'Everyone 10+', 'Strategy', 'July 31 2018', '2.0.2', '4.0.3 and up']
['Final Fantasy XV: A New Empire', 'GAME', '4', '484981', 'Varies with device', '10000000', 'Free', '0', 'Everyone 10+', 'Strategy', 'June 15 2018', '3.30.4.83', '4.1 and up']
['Call of Duty:Black Ops Zombies', 'GAME', '4.2', '13004', '46M', '10000000', 'Paid', '6.99', 'Teen', 'Action', 'December 20 2016', '1.0.11', '2.3.3 and up']
['Fallout Shelter', 'GAME', '4.6', '2719142', '25M', '10000000', 'Free', '0', 'Teen', 'Simulation', 'June 11 2018', '1.13.12', '4.1 and up']
['Harry Potter: Hogwarts Mystery', 'GAME', '4.4', '1107197', 'Varies with device', '10000000', 'Free', '0', 'Teen', 'Adventure', 'August 1 2018', '1.8.2', '4.4 and up']
['The Sims? FreePlay', 'GAME', '4.3', '931595', '31M', '10000000', 'Free', '0', 'Teen', 'Simulation', 'July 16 2018', '5.39.1', '4.0.3 and up']
['Best Fiends - Free Puzzle Game', 'GAME', '4.6', '1480189', 'Varies with device', '10000000', 'Free', '0', 'Everyone', 'Casual', 'August 2 2018', '5.8.1', 'Varies with device']
['Choices: Stories You Play', 'GAME', '4.6', '807338', '93M', '10000000', 'Free', '0', 'Teen', 'Simulation', 'July 30 2018', '2.3.5', '4.0.3 and up']
['Solitaire TriPeaks', 'GAME', '4.5', '446434', '52M', '10000000', 'Free', '0', 'Everyone', 'Card', 'August 1 2018', '4.6.2.46103', '4.0.3 and up']
['The Walking Dead: Road to Survival', 'GAME', '4.2', '522466', '37M', '10000000', 'Free', '0', 'Mature 17+', 'Role Playing', 'June 19 2018', '12.0.4.62276', '4.0.3 and up']
['PUBG MOBILE', 'GAME', '4.4', '3716278', '36M', '50000000', 'Free', '0', 'Teen', 'Action', 'July 24 2018', '0.7.0', '4.3 and up']
['MARVEL Contest of Champions', 'GAME', '4.3', '2468063', '92M', '50000000', 'Free', '0', 'Teen', 'Action', 'July 30 2018', '19.1.0', '4.0.3 and up']
['Clue', 'GAME', '4.6', '19922', '35M', '50000000', 'Paid', '1.99', 'Everyone 10+', 'Board', 'July 30 2018', '2.2.5', '5.0 and up']
['ROBLOX', 'GAME', '4.5', '4449882', '67M', '60000000', 'Free', '0', 'Everyone 10+', 'Adventure;Action & Adventure', 'July 31 2018', '2.347.225742', '4.1 and up']
['Angry Birds 2', 'GAME', '4.6', '3883589', '57M', '60000000', 'Free', '0', 'Everyone', 'Casual', 'July 26 2018', '2.21.1', '4.1 and up']
['8 Ball Pool', 'GAME', '4.5', '14200550', '52M', '60000000', 'Free', '0', 'Everyone', 'Sports', 'July 31 2018', '4.0.0', '4.0.3 and up']
['Hustle Castle: Fantasy Kingdom', 'GAME', '4.7', '584126', '64M', '30000000', 'Free', '0', 'Everyone 10+', 'Role Playing', 'July 27 2018', '1.5.4', '4.1 and up']
['Candy Crush Saga', 'GAME', '4.4', '22429716', '74M', '20000000', 'Free', '0', 'Everyone', 'Casual', 'July 5 2018', '1.129.0.2', '4.1 and up']

The Following prints out the first 20 rows of the 2d list:

for item in range(20): # Select the first 20 rows of the data
print(rows[item]) # print out the first 20 rows

Output:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M', '2000000', 'Free', '0', 'Teen', 'Strategy', 'May 18 2018', '2.0081_206', '4.0.3 and up']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M', '2200000', 'Free', '0', 'Teen', 'Strategy', 'July 26 2018', '1.8.2', '4.4 and up']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M', '3000000', 'Paid', '4.99', 'Everyone', 'Puzzle', 'April 18 2018', '1.0.1', '4.4 and up']
['Farming Simulator 18', 'GAME', '4.5', '18125', '15M', '5000000', 'Paid', '4.99', 'Everyone', 'Simulation;Education', 'July 9 2018', 'Varies with device', '4.4 and up']
['RollerCoaster Tycoon¸ Classic', 'GAME', '4.6', '10795', '69M', '5000000', 'Paid', '5.99', 'Everyone', 'Simulation', 'December 21 2017', '1.2.1.1712080', '4.0.3 and up']
['Star Wars ?: DIRTY', 'GAME', '4.5', '38207', '15M', '5000000', 'Paid', '9.99', 'Teen', 'Role Playing', 'October 19 2015', '1.0.6', '4.1 and up']
['Ice Crush 2018 - A new Puzzle Matching Adventure', 'GAME', '4.6', '15403', '25M', '1000000', 'Free', '0', 'Everyone', 'Casual', 'August 2 2018', '1.8.1', '4.1 and up']
['TerraGenesis - Space Colony', 'GAME', '4.3', '38957', 'Varies with device', '1000000', 'Free', '0', 'Everyone', 'Simulation', 'August 2 2018', '4.9.31', 'Varies with device']
['SHADOWGUN LEGENDS', 'GAME', '4.6', '100609', '52M', '1000000', 'Free', '0', 'Teen', 'Action', 'July 16 2018', '0.5.4', '6.0 and up']
['Dino War: Rise of Beasts', 'GAME', '4.1', '18996', '81M', '1000000', 'Free', '0', 'Teen', 'Strategy', 'August 1 2018', '1.5.2', '4.1 and up']
['Tokyo Ghoul: Dark War', 'GAME', '4.3', '25094', '82M', '1000000', 'Free', '0', 'Teen', 'Role Playing', 'July 6 2018', '1.2.0', '4.3 and up']
['Chapters: Interactive Stories', 'GAME', '4.5', '73539', '96M', '1000000', 'Free', '0', 'Mature 17+', 'Role Playing', 'August 2 2018', '1.3.3', '4.3 and up']
['Honkai Impact 3rd', 'GAME', '4.7', '59017', '82M', '1000000', 'Free', '0', 'Teen', 'Action', 'July 3 2018', '2.2.1', '4.3 and up']
['The Game of Life', 'GAME', '4.4', '18621', '63M', '1000000', 'Paid', '2.99', 'Everyone', 'Board', 'July 4 2018', '2.1.2', '4.4 and up']
['The Escapists', 'GAME', '4.4', '7412', '84M', '1000000', 'Paid', '4.99', 'Teen', 'Strategy', 'April 26 2018', '1.1.0', '2.3 and up']
['Hero Hunters', 'GAME', '4.4', '70747', '99M', '5000000', 'Free', '0', 'Teen', 'Action', 'July 31 2018', '1.7', '4.4 and up']
['Merge Dragons!', 'GAME', '4.5', '214777', '91M', '5000000', 'Free', '0', 'Everyone', 'Puzzle', 'July 30 2018', '3.6.0', '4.1 and up']
['Last Shelter: Survival', 'GAME', '4.4', '93033', '98M', '5000000', 'Free', '0', 'Teen', 'Strategy', 'August 4 2018', '1.250.078', '4.0.3 and up']
['Lineage 2: Revolution', 'GAME', '4.2', '187972', '99M', '5000000', 'Free', '0', 'Teen', 'Role Playing', 'July 11 2018', '1.05.20', '4.4 and up']
['DRAGON BALL LEGENDS', 'GAME', '4.6', '337913', '48M', '5000000', 'Free', '0', 'Teen', 'Action', 'August 3 2018', '1.11.0', '6.0 and up']

Question: How do I print only 5 of the attributes of each list within the 2d list?

[print(rows[item]) for item, singlerow in enumerate(rows)]

this didn't work.

Please Help. I've tried this thing too. didn't work.

data = []
for list2d_index, singlerow in enumerate(rows):
if list2d_index < 20:
[print(singlerow) for index, item in enumerate(singlerow) if item[index] < 8]

答案1

得分: 2

以下是已经翻译好的部分:

IIUC, You can try this:
for l in lst:
    print(l[:5])

输出:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M']
['Farming Simulator 18', 'GAME', '4.5', '18125', '15M']
['RollerCoaster Tycoon™ Classic', 'GAME', '4.6', '10795', '69M']
['Star Wars : DIRTY', 'GAME', '4.5', '38207', '15M']
['Ice Crush 2018 - A new Puzzle Matching Adventure', 'GAME', '4.6', '15403', '25M']
['TerraGenesis - Space Colony', 'GAME', '4.3', '38957', 'Varies with device']
['SHADOWGUN LEGENDS', 'GAME', '4.6', '100609', '52M']
['Dino War: Rise of Beasts', 'GAME', '4.1', '18996', '81M']
['Tokyo Ghoul: Dark War', 'GAME', '4.3', '25094', '82M']
['Chapters: Interactive Stories', 'GAME', '4.5', '73539', '96M']
['Honkai Impact 3rd', 'GAME', '4.7', '59017', '82M']
['The Game of Life', 'GAME', '4.4', '18621', '63M']
['The Escapists', 'GAME', '4.4', '7412', '84M']
['Hero Hunters', 'GAME', '4.4', '70747', '99M']
['Merge Dragons!', 'GAME', '4.5', '214777', '91M']
['Last Shelter: Survival', 'GAME', '4.4', '93033', '98M']
['Lineage 2: Revolution', 'GAME', '4.2', '187972', '99M']
['DRAGON BALL LEGENDS', 'GAME', '4.6', '337913', '48M']
英文:

IIUC, You can try this:

for l in lst:
print(l[:5])

Output:

["Girls' Frontline", 'GAME', '4.2', '2055', '96M']
['Master of Eternity(MOE)', 'GAME', '4.2', '5829', '70M']
['The Room: Old Sins', 'GAME', '4.9', '21119', '48M']
['Farming Simulator 18', 'GAME', '4.5', '18125', '15M']
['RollerCoaster Tycoon¸ Classic', 'GAME', '4.6', '10795', '69M']
['Star Wars ?: DIRTY', 'GAME', '4.5', '38207', '15M']
['Ice Crush 2018 - A new Puzzle Matching Adventure', 'GAME', '4.6', '15403', '25M']
['TerraGenesis - Space Colony', 'GAME', '4.3', '38957', 'Varies with device']
['SHADOWGUN LEGENDS', 'GAME', '4.6', '100609', '52M']
['Dino War: Rise of Beasts', 'GAME', '4.1', '18996', '81M']
['Tokyo Ghoul: Dark War', 'GAME', '4.3', '25094', '82M']
['Chapters: Interactive Stories', 'GAME', '4.5', '73539', '96M']
['Honkai Impact 3rd', 'GAME', '4.7', '59017', '82M']
['The Game of Life', 'GAME', '4.4', '18621', '63M']
['The Escapists', 'GAME', '4.4', '7412', '84M']
['Hero Hunters', 'GAME', '4.4', '70747', '99M']
['Merge Dragons!', 'GAME', '4.5', '214777', '91M']
['Last Shelter: Survival', 'GAME', '4.4', '93033', '98M']
['Lineage 2: Revolution', 'GAME', '4.2', '187972', '99M']
['DRAGON BALL LEGENDS', 'GAME', '4.6', '337913', '48M']

huangapple
  • 本文由 发表于 2023年2月14日 03:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定