【问题标题】:Splitting a tuple in to a list without splitting it in to individual characters将元组拆分为列表而不将其拆分为单个字符
【发布时间】:2019-06-23 12:40:50
【问题描述】:

我的 Python 代码有问题。我想从元组中获取值并将它们放入列表中。在下面的示例中,我想让艺术家进入一个列表,而收入进入另一个列表。然后将它们放入一个元组中。

def sort_artists(x):
    artist = []
    earnings = []
    z = (artist, earnings)
    for inner in x:
        artist += inner[0]
        earnings += inner[1]
    return z

artists = [("The Beatles", 270.8), ("Elvis Presley", 211.5), ("Michael Jackson", 183.9)]
print(sort_artists(artists))

我可以打印 `inner[0] ,这会给我 'The Beatles',但是当我尝试将它附加到空列表时,它会将其拆分为单个字母。怎么了?

错误(尽管我也尝试过没有“收益”位,以及使用append 和其他东西:

Traceback (most recent call last):
  File "Artists.py", line 43, in <module>
    print(sort_artists(artists))
  File "Artists.py", line 31, in sort_artists
    earnings += inner[1]
TypeError: 'float' object is not iterable
Command exited with non-zero status 1

想要的输出:

(['Elvis Presley', 'Michael Jackson', 'The Beatles'], [270.8, 211.5, 183.9]) 

这是目前正在发生的事情(没有收入位):

(['T', 'h', 'e', ' ', 'B', 'e', 'a', 't', 'l', 'e', 's', 'E', 'l', 'v', 'i', 's', ' ', 'P', 'r', 'e', 's', 'l', 'e', 'y', 'M', 'i', 'c', 'h', 'a', 'e', 'l', ' ', 'J', 'a', 'c', 'k', 's', 'o', 'n'], []) 

【问题讨论】:

  • “出了什么问题?” 在您的上下文中,+ 是序列的串联,因此[] 是一个序列,@987654328 @ 是一个字符串,它是一个字符序列,所以你有 []+['a','b'] 的等价物(在解释器中尝试)。您必须使用列表的 .append(object) 方法,它不会解包它的参数,而只是将它插入到列表的末尾。

标签: python string list tuples


【解决方案1】:

您可以使用内置函数zip 将列表拆分为一个简短的表达式:

def sort_artists(x):
    return tuple(list(t) for t in zip(*x))

artists = [("The Beatles", 270.8), ("Elvis Presley", 211.5), ("Michael Jackson", 183.9)]
print(sort_artists(artists))

输出:

(['The Beatles', 'Elvis Presley', 'Michael Jackson'], [270.8, 211.5, 183.9])

【讨论】:

  • 这是一个元组的元组,OP要求一个列表的元组(可能他们甚至不在乎,但我在这里是为了吹毛求疵:-) +1
  • @gboffi 如果您在 2 年后到达这里,值得修复:P
【解决方案2】:

试试这个代码:

def sort_artists(x):
    artist = []
    earnings = []
    z = (artist, earnings)
    for inner in x:
        artist.append(inner[0])
        earnings.append(inner[1])
    return z

artists = [("The Beatles", 270.8), ("Elvis Presley", 211.5), ("Michael Jackson", 183.9)]
print(sort_artists(artists))

输出:

(['The Beatles', 'Elvis Presley', 'Michael Jackson'], [270.8, 211.5, 183.9])

【讨论】:

  • 谢谢。 += 用错了吗?这仅适用于单个字符吗?
  • @william3031 使用+= 尝试连接列表。例如[1,2] + [3,4] == [1,2,3,4]...你遇到的问题是一个字符串可以被认为是一个字符列表,这就是你单独得到它们的原因
【解决方案3】:
def sort_artists(x):
    artist = []
    earnings = [] 
    for i in range(len(x)):
        artist.append(x[i][0])
        earnings.append(x[i][1])
    return (artist,earnings)

我们可以通过它的位置访问列表的元素

print(artist[0]) 
#o/p
('The Beatles', 270.8)

现在我们也可以使用索引解包元组

#for artist
print(artist[0][0])
o/p
'The Beatles'

#for earnings
print(artist[0][1])
o/p
270.8

【讨论】:

    【解决方案4】:
    def sort_artists(x):
        artist = []
        earnings = []
        z = (artist, earnings)
        for inner in x:
            artist.append(inner[0])
            earnings.append(inner[1])
        artist.sort()
        earnings.sort()
        earnings.reverse()
        return z
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 2017-07-30
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多