【问题标题】:How do I remove commas and quotations from my list output如何从列表输出中删除逗号和引号
【发布时间】:2016-10-09 05:47:16
【问题描述】:

很难从我的列表中删除这些引号和逗号,

**SHOWN BELOW IN THE SECOND PART OF MY CODE OUTPUT**我需要从输出中删除所有的 (' ',),我一直在我的team variable 上尝试rstrip(),但它给了我这个错误。

TypeError: Can't convert 'int' object to str implicitly

我想我的代码可能是错误的......但这是阻止自己丢弃它并重新开始的最后努力

#list of teams
Champs = ['Boston Americans', 'New York Giants', 'Chicago White Sox', 'Chicago Cubs', 'Chicago Cubs', 'Pittsburgh Pirates', 'Philadelphia Athletics', 'Philadelphia Athletics', 'Boston Red Sox', 'Philadelphia Athletics', 'Boston Braves', 'Boston Red Sox', 'Boston Red Sox', 'Chicago White Sox', 'Boston Red Sox', 'Cincinnati Reds', 'Cleveland Indians', 'New York Giants', 'New York Giants', 'New York Yankees', 'Washington Senators', 'Pittsburgh Pirates', 'St. Louis Cardinals', 'New York Yankees', 'New York Yankees', 'Philadelphia Athletics', 'Philadelphia Athletics', 'St. Louis Cardinals', 'New York Yankees', 'New York Giants', 'St. Louis Cardinals', 'Detroit Tigers', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'Cincinnati Reds', 'New York Yankees', 'St. Louis Cardinals', 'New York Yankees', 'St. Louis Cardinals', 'Detroit Tigers', 'St. Louis Cardinals', 'New York Yankees', 'Cleveland Indians', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'New York Giants', 'Brooklyn Dodgers', 'New York Yankees', 'Milwaukee Braves', 'New York Yankees', 'Los Angeles Dodgers', 'Pittsburgh Pirates', 'New York Yankees', 'New York Yankees', 'Los Angeles Dodgers', 'St. Louis Cardinals', 'Los Angeles Dodgers', 'Baltimore Orioles', 'St. Louis Cardinals', 'Detroit Tigers', 'New York Mets', 'Baltimore Orioles', 'Pittsburgh Pirates', 'Oakland Athletics', 'Oakland Athletics', 'Oakland Athletics', 'Cincinnati Reds', 'Cincinnati Reds', 'New York Yankees', 'New York Yankees', 'Pittsburgh Pirates', 'Philadelphia Phillies', 'Los Angeles Dodgers', 'St. Louis Cardinals', 'Baltimore Orioles', 'Detroit Tigers', 'Kansas City Royals', 'New York Mets', 'Minnesota Twins', 'Los Angeles Dodgers', 'Oakland Athletics', 'Cincinnati Reds', 'Minnesota Twins', 'Toronto Blue Jays', 'Toronto Blue Jays', 'Atlanta Braves', 'New York Yankees', 'Florida Marlins', 'New York Yankees', 'New York Yankees', 'New York Yankees', 'Arizona Diamondbacks', 'Anaheim Angels', 'Florida Marlins', 'Boston Red Sox', 'Chicago White Sox', 'St. Louis Cardinals', 'Boston Red Sox', 'Philadelphia Phillies', 'New York Yankees', 'San Francisco Giants', 'St. Louis Cardinals', 'San Francisco Giants', 'Boston Red Sox']    

#sort list alphabetically
Champs.sort()

for team in [ele for ind, ele in enumerate(Champs,1) if ele not in Champs[ind:]]:
    count = 0
    for ele in Champs:
        if team == ele:
            count += 1
    print((team.strip(),count.strip()))
    count = 0

--------我的输出如下------------

Welcome to the world series team count report generator.

Choose an option from the following list.
1: Find out the count of wins for a single team.
2: Run a report showing all teams with their count of wins.
3: Exit the program.

Enter your choice: 2
Team Count of Wins 
------------------------------

Team Name       Wins
('Anaheim Angels', 1)
('Arizona Diamondbacks', 1)
('Atlanta Braves', 1)
('Baltimore Orioles', 3)
('Boston Americans', 1)
('Boston Braves', 1)
('Boston Red Sox', 7)
('Brooklyn Dodgers', 1)
('Chicago Cubs', 2)
('Chicago White Sox', 3)
('Cincinnati Reds', 5)
('Cleveland Indians', 2)
('Detroit Tigers', 4)
('Florida Marlins', 2)
('Kansas City Royals', 1)
('Los Angeles Dodgers', 5)
('Milwaukee Braves', 1)
('Minnesota Twins', 2)
('New York Giants', 5)
('New York Mets', 2)
('New York Yankees', 27)
('Oakland Athletics', 4)
('Philadelphia Athletics', 5)
('Philadelphia Phillies', 2)
('Pittsburgh Pirates', 5)
('San Francisco Giants', 2)
('St. Louis Cardinals', 11)
('Toronto Blue Jays', 2)
('Washington Senators', 1)

【问题讨论】:

  • 额外说明 - 您不需要在代码末尾添加最后一个 count = 0。您已经在第一个循环的每次迭代中重置了计数。

标签: python list output strip


【解决方案1】:

( ) 包装项目使其成为一个元组;您应该删除调用print 中的额外括号。你不需要strip 来输入int 类型。你也不要strip这个字符串(在这种情况下):

print(team, count)

更重要的是,collections.Counter 已经做了你想做的事情:

from collections import Counter

for k, v in Counter(Champs).items():
    print(k, v)

【讨论】:

  • 去掉print中多余的括号( )
  • 知道了......哇,这都是愚蠢的错误造成的麻烦啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
【解决方案2】:

它不是你元素的一部分。 print() 在显示列表、元组、字典时添加此内容。你看到它是因为你使用了太多的() 并创建了元组。

你有

print(  (team.strip(), count.strip())   )

(team.strip(), count.strip()) 表示元组。

你需要

print( team.strip(), count )

【讨论】:

    【解决方案3】:

    从计数中删除strip()

    print(team.strip(), count)
    

    【讨论】:

    • 什么也没做
    【解决方案4】:

    去掉括号和strip

    print(team.strip(), count)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多