挣扎于一系列变量 [英] Struggling with a series of variables

查看:110
本文介绍了挣扎于一系列变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为避免在Twitter垃圾邮件过滤器中捕获推文,我有一些代码会转到tinyurl,并在每次为每个原始url运行代码时创建一个新的短url.我想要的是每次打印'u'时,它的值应该传递给变量'linkvar1', 'linkvar2', 'linkvar3'等.这是稍后在代码中传递给tweet提交的信息:

To avoid tweets becoming caught in the twitter spam filter I have some code that goes to tinyurl and creates a new short url each time the code is run for each of the original urls. What i want is each time 'u' is printed, it's value should be passed to a variable 'linkvar1', 'linkvar2', 'linkvar3' etc. This is the passed to the tweet submission later in the code:

import simplejson
import httplib2
import twitter
import tinyurl

print("Python will now attempt to submit tweets to twitter...")

try:

    api = twitter.Api(consumer_key='',
                      consumer_secret='',
                      access_token_key='',
                      access_token_secret='')

    for u in tinyurl.create('http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html',
                        'http://audiotechracy.blogspot.co.uk/2014/03/free-guitar-patches-for-propellerhead.html',
                        'http://audiotechracy.blogspot.co.uk/2014/02/get-free-propellerhead-rock-and-metal.html',
                        ):
        print u
        linkvar1 = u
        linkvar2 = u
        linkvar3 = u

    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvar1 + " #propellerhead #synapse")
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvar2 + " #propellerhead #reason #guitar")
status = api.PostUpdate("Free Metal and Rock drum samples!" + linkvar3 + " #propellerhead #reason)


print("Tweets submitted successfully!")

例外,e: 打印str(e)
打印("Twitter提交失败!!")

except Exception,e: print str(e)
print("Twitter submissions have failed!!!")

然而,目前所有这些操作仅使用最后生成的tinyurl用于所有提交的tweet.我敢肯定,这只是个愚蠢的简单解决方案,但是有人知道如何做我想做的事吗?

However at the minute all this does is use the tinyurl generated last for all tweets submitted. I'm sure this is an easy fix that I'm just being stupid about, but does anyone know how to do what I want?

谢谢

推荐答案

您的问题是您在每个循环中都没有对linkvar变量进行任何操作.因此,每次循环运行时,它们都会被覆盖.

Your issue is that you aren't doing anything with your linkvar variables through each loop. Thus, each time the loop runs, they are getting overwritten.

您有一些选择

选项1:将linkvar s列表添加到每个循环中

Option 1: Make the linkvars a list that you append to each loop

linkvar1 = []
for u in ...
   ...
   linkvar1.append(u)

# Post to twitter
for p in linkvar1:
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + p + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + p + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + p + " #propellerhead #reason)

在第一个for循环的结尾,您将在linkvar变量中具有值.我不确定为什么要使用三个实例,我是否将其切成单个实例.然后,您可以使用另一个for循环来循环浏览,或者将批发价传递给自己的函数来适当地处理它们.无论哪种情况,您的所有网址现在都在每个这些变量的列表中

At the end of your first for loop, you will have values in the linkvar variable. I am not sure why you are using three, do I chopped it down to just the single instance. You can then loop through using another for loop, or passed whole-sale to your own function that will handle them appropriately. In either case, all of your urls are now in a list in each of these variables

选项2:调用一个函数在每个循环上执行

Option 2: Call a function to perform on each loop

for u in ...
   ...
   MyTwitterFunction(u)

def MyTwitterFunction(url):
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + url + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + url + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + url + " #propellerhead #reason)

每次循环循环时,将使用值u调用MyTwitterFunction.

Each time the loop is iterated over, the MyTwitterFunction will be called with the value of u.

选项3:将您的发布代码直接拉入for循环

Option 3: Pull your posting code directly into your for loop

for u in ...
    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + u + " #propellerhead #synapse")
    status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + u + " #propellerhead #reason #guitar")
    status = api.PostUpdate("Free Metal and Rock drum samples!" + u + " #propellerhead #reason)

这消除了对linkvar变量和多余的for循环的需要.您可以直接从创建URL的循环中发布.

This eliminates the need for the linkvar variables and the extra for loop. You can post directly from the loop in which the URLs are created.

这篇关于挣扎于一系列变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆