变量赋值期间的Python函数打印输出

时间:2018-05-28 14:55:27

标签: python

我希望以下代码运行已定义的函数,并将已定义函数的输出保存在指定的变量" functionsOutput"中。然后,我想用空格替换变量中的新行。

但我的下面的代码并没有这样做。我究竟做错了什么?将函数输出分配给变量是打印出输出。我不想要那个。我希望输出存储在变量中。

#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        print objects

functionsOutput = printWithoutNewlines()
functionsOutput.replace('\n', ' ')

3 个答案:

答案 0 :(得分:0)

以下代码应该提供您想要的输出,但是您想以这种方式完成它吗?

mylist = [ "hello", "you", "are", "so", "cool" ]

def printWithoutNewlines():
    for objects in mylist:
        objects = objects.replace('hello', "hi")
        print objects,
    print

printWithoutNewlines()

答案 1 :(得分:0)

这里有几个错误:

  1. 您的功能不return。挥手告别之前做的最后一件事就是打印。
  2. 您正在为迭代器分配一个新值。这使得 循环中的陈述已过时。
  3. 如果你纠正了这些错误,你甚至不必删除\n字符。

    #!/usr/bin/python2.7
    mylist = [ "hello", "you", "are", "so", "cool" ]
    
    def printWithoutNewlines():
        output = ""
        for objects in mylist:
            output += objects.replace('hello', "hi")
        return output
    
    functionsOutput = printWithoutNewlines()
    print functionsOutput
    >>> hiyouaresocool
    

答案 2 :(得分:-2)

您的功能是打印结果,我认为您想要的是返回值。我可以是这样的:

function FeaturedPost(a) {
    (function(e) {
        var h = {
            blogURL: "",
            MaxPost: 5,
            idcontaint: "#featuredpost",
            ImageSize: 500,
            interval: 10000,
            autoplay: false,
            loadingClass: "loadingxx",
            pBlank: "https://3.bp.blogspot.com/-v45kaX-IHKo/VDgZxWv0xUI/AAAAAAAAHAo/QJQf8Dlh3xc/s1600/grey.gif",
            MonthNames: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
            tagName: false
        };
        h = e.extend({}, h, a);
        var g = e(h.idcontaint);
        var c = h.blogURL;
        var d = h.MaxPost * 200;
        if (h.blogURL === "") {
            c = window.location.protocol + "//" + window.location.host
        }
        g.html('<div id="slides"><ul class="randomnya"></ul></div><div id="buttons"><a href="#" id="prevx" title="prev"></a><a href="#" id="nextx" title="next"></a></div>').addClass(h.loadingClass);
        var l = "";
        var count = 0;
        var f = function(w) {
            var q, k, m, u, x, p, t, v, r, tt, ts = "",
                s = w.feed.entry;
            for (var o = 0; o < s.length; o++) {
                for (var n = 0; n < s[o].link.length; n++) {
                    if (s[o].link[n].rel == "alternate") {
                        q = s[o].link[n].href;
                        break
                    }
                }
                if ("media$thumbnail" in s[o]) {
                    u = s[o].media$thumbnail.url.replace(/\/s[0-9]+\-c/g, "/s" + h.ImageSize + "-c")
                } else {
                    u = h.pBlank.replace(/\/s[0-9]+(\-c|\/)/, "/s" + h.ImageSize + "$1")
                }
                k = s[o].title.$t;
                ts = s[o].published.$t;
                tt = Date.parse(ts);
                l += '<li data-date="' + tt + '"><a href="' + q + '" title="' + k + '"><div class="overlayx"></div><img class="random" src="' + u + '" title="' + k + '"><h4>' + k + '</h4>' + t + '</span> ' + m + "</span></div></li>"
            }
            count++;
            if (count === h.tagName.length) {
                var el = e( '<div></div>' ).html(l);
                e('li', el).sort(function (a, b) {
                   return e(b).attr('data-date') - e(a).attr('data-date');
                }).appendTo("ul", g);
            }
        };
        var b = function() {
            if (h.tagName === false) {
                i = "";
                e.ajax({
                    url: c + "/feeds/posts/default" + i + "?max-results=" + h.MaxPost + "&orderby=published&alt=json-in-script",
                    success: f,
                    dataType: "jsonp",
                    cache: true
                })
            } else {
                for (var q = 0; q < h.tagName.length; q++) {
                    e.ajax({
                        url: c + "/feeds/posts/default/-/" + h.tagName[q] + "?max-results=1&orderby=published&alt=json-in-script",
                        success: f,
                        dataType: "jsonp",
                        cache: true
                    })
                }
            }(function() {
                setTimeout(function() {
                    e("#prevx").click(function() {
                        e("#slides li:first").before(e("#slides li:last"));
                        return false
                    });
                    e("#nextx").click(function() {
                        e("#slides li:last").after(e("#slides li:first"));
                        return false
                    });
                    if (h.autoplay) {
                        var k = h.interval;
                        var l = setInterval("rotate()", k);
                        e("#slides li:first").before(e("#slides li:last"));
                        e("#slides").hover(function() {
                            clearInterval(l)
                        }, function() {
                            l = setInterval("rotate()", k)
                        });

                        function j() {
                            e("#nextx").click()
                        }
                    }
                    g.removeClass(h.loadingClass)
                }, d)
            })()
        };
        e(document).ready(b)
    })(jQuery)
};

function rotate() {
    $('#nextx').click();
};
document.write('')

//<![CDATA[
$(document).ready(function() {
    FeaturedPost({
        blogURL: "https://trit3k.blogspot.com",
        MaxPost: 5,
        idcontaint: "#featuredpost",
        ImageSize: 500,
        tagName: ["Android","Inovação","Gaming", "Oreo","SnapChat"]
    });
});
//]]>

事实上,它有点复杂,因为你需要添加空格等等。然后,您将不需要替换。