Python,更好的编码方式。使用循环数组?

时间:2016-07-26 11:41:08

标签: python

我刚刚开始学习python,并想知道是否有更好的方法来编写以下代码。

user1 = "username"
userkey1 = "userkey"
user2 = "username"
userkey2 = "userkey"
user3 = "username"
userkey3 = "userkey"
user4 = "username"
userkey4 = "userkey"
user5 = "username"
userkey5 = "userkey"
user6 = "username"
userkey6 = "userkey"
user7 = "username"
userkey7 = "userkey"

level = ["variable2", "variable2"]

connect = ipaddress
for article in connect.link("var1", "var2"):
    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user1,}
            )

            output = output.sign([userkey1])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user2,}
            )

            output = output.sign([userkey2])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

    if article["variable"] == '' and article["creator"] in level:
        try:
            action = dr.create(
                    **{"person": user3,}
            )

            output = output.sign([userkey3])

            output = tx.JsonObj(tx)

            # Broadcast to network
            broadcast

我还不熟悉while循环,循环或数组,但在我看来,上面的代码可以更高效一点?

代码应该抓取输入并且只是循环,当它找到它想要为每个用户及其各自的密钥创建一个动作时。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果您的代码按照您希望的方式运行良好。但长变量定义很多。查找(谷歌)python列表和字典。看看这篇文章:How to implement associative array in Python?