【问题标题】:I keep getting Nonetype but I dont think I should be?我不断收到 Nonetype 但我认为我不应该是?
【发布时间】:2012-09-14 16:58:55
【问题描述】:

我正在为我的项目以及 json 模块使用 sqlalchemy-flask。

我有两个要从中提取数据的课程。

这两种数据类型是 Ints 和 List(我通过使用 type 确定了这一点)。当我尝试将 int 附加到列表时,我得到 None。怎么了?

def update_retweet_count(TWEET, TWEET_has_retweet):

    if type(json.loads(TWEET_has_retweet.js_rt)) != list:
        list_of_retweets = list([0])
    else:
        list_of_retweets = list(json.loads(TWEET_has_retweet.js_rt))

    new_rtc = int(TWEET.tmp_rt_count)

    x = list_of_retweets.append(new_rtc)
    print x 

当我在 X 上方运行时,没有。

4 小时后,我在下面尝试了这个,它成功了!

def update_retweet_count(TWEET, TWEET_has_retweet):
    if type(json.loads(TWEET_has_retweet.js_rt)) != list:
        list_of_retweets = list([0])
    else:
        list_of_retweets = list(json.loads(TWEET_has_retweet.js_rt))

    lst =[]

    new_rtc = int(TWEET.tmp_rt_count)

    [lst.append(y) for y in list_of_retweets]

    lst.append(new_rtc)

    print lst

为什么第一个代码不起作用?

谢谢!

费尔南多

【问题讨论】:

  • 几个 cmets:1) list([0])[0] 相同。 2)你可能只做lst = list_of_retweets,或者最坏的情况是lst = list(list_of_retweets)
  • 注明。感谢您的反馈

标签: python flask-sqlalchemy


【解决方案1】:

list.append 总是返回None:它就地改变了列表。你这样做:

x = list_of_retweets.append(new_rtc)
print x

在第一个例子中:

lst.append(new_rtc)
print lst

在第二个(正确的)示例中。

【讨论】:

  • @mgilson:FGITW,但下次你会超过我。 (顺便说一句,程序员在沮丧时会说“Argv”吗?)
  • 是的,我在哪里看到过,觉得很有趣,所以我开始说...
猜你喜欢
  • 2019-06-25
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2019-08-10
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多