[英]how get the nth string in a list of tuples through list-comprehension?
I am trying to get 1st (0) and 2nd (1) strings in a tuple at a df.我正在尝试在 df 的元组中获取第一个 (0) 和第二个 (1) 字符串。
df= {'col':[ "[('affect', 'the risks')]", "[('have', 'we'), ('breached', 'our systems'), ('cease', 'our computer'), ('suffer', ''), ('allow', ''), ('damage', 'misappropriation'), ('require', 'proprietary'), ('incur', 'us'), ('remediate', 'us'), ('resolve', 'us')]"]}
df = pd.DataFrame(df)
such that, the expected output for item0 and item1 should be:这样, item0 和 item1 的预期输出应该是:
df={'item0': [ "'affect'", "'have', 'breached','cease', 'suffer',' allow', 'damage' , 'require', 'incur', 'remediate', 'resolve'"]}
df={'item1': [ "'the risks'", "'we', 'our systems','our computer', '', 'misappropriation', 'proprietary', 'us', 'us', 'us'"]}
df = pd.DataFrame(df)
I think we should use zip() function but I couldnot figure it out because I have a dataframe here.我认为我们应该使用 zip() 函数,但我无法弄清楚,因为我在这里有一个数据框。
Resources I went through: 1) https://docs.python.org/3/tutorial/datastructures.html#nested-list-comprehensions 2) Python - List comprehension list of tuple list to tuple list我经历的资源:1) https://docs.python.org/3/tutorial/datastructures.html#nested-list-comprehensions 2) Python - 元组列表到元组列表的列表理解列表
If you actually have a list of tuples, like this:如果您实际上有一个元组列表,如下所示:
data = {
"col": [
("affect", "the risks"),
("have", "we"),
("breached", "our systems"),
("cease", "our computer"),
("suffer", ""),
("allow", ""),
("damage", "misappropriation"),
("require", "proprietary"),
("incur", "us"),
("remediate", "us"),
("resolve", "us"),
],
}
Then extracting things the way you want is relatively easy:然后以您想要的方式提取内容相对容易:
item0 = [x[0] for x in data["col"]]
item1 = [x[1] for x in data["col"]]
print("item0:", item0)
print("item1:", item1)
That gets us:这让我们:
item0: ['affect', 'have', 'breached', 'cease', 'suffer', 'allow', 'damage', 'require', 'incur', 'remediate', 'resolve']
item1: ['the risks', 'we', 'our systems', 'our computer', '', '', 'misappropriation', 'proprietary', 'us', 'us', 'us']
Unfortunately, you don't have a list of tuples, and it's not clear from your question if that's just a typo or if you have simply mis-described your data.不幸的是,您没有元组列表,从您的问题中不清楚这只是一个错字还是您只是错误地描述了您的数据。 When you write:
当你写:
df = {
"col": [
"[('affect', 'the risks')]",
"[('have', 'we'), ('breached', 'our systems'), ('cease', 'our computer'), ('suffer', ''), ('allow', ''), ('damage', 'misappropriation'), ('require', 'proprietary'), ('incur', 'us'), ('remediate', 'us'), ('resolve', 'us')]",
]
}
You have a list of two strings.您有一个包含两个字符串的列表。 The first is:
第一个是:
>>> df['col'][0]
"[('affect', 'the risks')]"
And the second is:第二个是:
>>> df['col'][1]
"[('have', 'we'), ('breached', 'our systems'), ('cease', 'our computer'), ('suffer', ''), ('allow', ''), ('damage', 'misappropriation'), ('require', 'proprietary'), ('incur', 'us'), ('remediate', 'us'), ('resolve', 'us')]"
Processing these is going to be a little tricky.处理这些会有点棘手。 Things will be much easier if you can arrange for your data to be formatted as a list of tuples instead.
如果您可以安排将数据格式化为元组列表,事情会容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.