【问题标题】:generate list of tuples to import to pandas df生成要导入到 pandas df 的元组列表
【发布时间】:2020-04-12 01:51:35
【问题描述】:

我找到了一个带有以下代码的辅音簇列表:

list_2 = ['financial','disastrous','accuracy','important','numbers']
reg = r'[bdðfghjklmnprstvxþ]+'
d = []
largest = []
for w in list_2:
    d.append(re.findall(reg, str(w), re.IGNORECASE))
print(d)

[['f', 'n', 'n', 'l'], ['d', 's', 'str', 's'], ['r'], ['mp' , 'rt', 'nt'], ['n', 'mb', 'rs']]

我需要为每个单词获取最大的辅音计数,以作为(元组的)列表导入到 pandas 数据帧。我尝试了各种方法,但都没有成功。

【问题讨论】:

  • 元组应该是 (word, int(largest cluster in word))

标签: python pandas list tuples


【解决方案1】:

如果单词没有辅音,我会得到 ValueError: max() arg is an empty sequence。我该如何解决?

【讨论】:

    【解决方案2】:

    工作正常,现在将其导入带有列标签的熊猫。希望没问题。

    【讨论】:

      【解决方案3】:

      这应该给你你正在寻找的int:

      def largest_cluster(cons_list):
          return max(len(c) for c in cons_list)
      

      然后你可以通过以下方式获取元组:

      tuples = [(w, largest_cluster(cons)) for w, cons in zip(list2, d)]
      

      【讨论】:

        猜你喜欢
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 2022-11-16
        • 2017-03-28
        相关资源
        最近更新 更多