Python加入字符串和数组的内容

时间:2018-01-16 12:57:57

标签: python arrays string

我一直在尝试使用一些循环将字符串与数组内容连接在一起,如下例所示:

Array's content for each index:
array[0] = [bla,bla,bla,bla]
array[1] = [ble,ble,ble,ble]
array[2] = [bli,bli,bli,bli]

String i want to add:
new_string = ",ok"

我想获得以下输出:

bla,bla,bla,bla,ok

如果我尝试类似下面的代码,我会得到以下输出

print (str(array[0]) + new_string)

Output:
bla,bla,bla,bla
,OK

newer_string = str(array[0]) + new_string
print (newer_string)

Output:
[bla,bla,bla,bla\n],ok

所以我不确定如何得到我想要的结果。此外,数组的每个内容都有点大,上面的例子只是它们包含的简化版本,但我想添加的字符串只是一两个字。

编辑:

以下是我的一个索引数组的示例。它们中的每一个都有一个从csv文件中获得的分隔线。

Array[0] = [11111,22222,88888888888,2018-01-01 00:00:00,Document,Place,type1,type2,COMPLET SUCESS,,PostPaid,No Extra,,20,N,,,,,,,,,,,9876543321,No,R,P,november,proxy ,2018-01-01 16:01:01]

2 个答案:

答案 0 :(得分:1)

要从数组连接字符串元素,请使用 join 方法

示例:

public class BaseActivity : Activity{
        static bool isNotify=false;
        static string messageAct = "";
        protected override void OnCreate(Bundle savedInstanceState)
        {
            OneSignal.Current.StartInit("mykey")
                .InFocusDisplaying(OSInFocusDisplayOption.None)
                .HandleNotificationOpened(HandleNotificationOpened)
                .EndInit();
            OneSignal.Current.IdsAvailable(IdsAvailable);
            base.OnCreate(savedInstanceState);
        }
        protected override void OnResume()
        {
            base.OnResume();
            Intent notificationIntent;
            if(isNotify){
                if(messageAct="1"){
                    notificationIntent = new Intent(this, typeof(ProfileActivity));
                }
                if(messageAct="2"){
                    notificationIntent = new Intent(this, typeof(NotificationsActivity));
                }
                else{
                    notificationIntent = new Intent(this, typeof(MainActivity));
                }
            }
            else{
                notificationIntent = new Intent(this, typeof(MainActivity));
            }
            StartActivity(notificationIntent);
        }
        private static void HandleNotificationOpened(OSNotificationOpenedResult result)
        {
            OSNotificationPayload payload = result.notification.payload;
            Dictionary<string, object> additionalData = payload.additionalData;
            string message = payload.body;
            if (additionalData != null)
            {
                isNotify = true;
                if (additionalData.ContainsKey("act"))
                {
                    messageAct = additionalData["act"].ToString();
                }
            }
        }
    }

    public class ActivityOne : BaseActivity{
        // do ActivityOne stuff
    }


    public class ActivityTwo : BaseActivity{
        // do ActivityTwo stuff
    }

<强>结果

a = ['bla','bla','bla','bla']
print(",".join(a) + " ok")

答案 1 :(得分:1)

>>> array = [["bla","bla","bla","bla"],["ble","ble","ble","ble"],["bli","bli","bli","bli"]]
>>> for element in array:
...     element.append("ok")
...     ",".join(element)

或者如果您不想修改数组:

>>> array = [["bla","bla","bla","bla"],["ble","ble","ble","ble"],["bli","bli","bli","bli"]]
>>> for element in array:
...     ",".join(element) + ",ok"