列表操作与索引

huangapple go评论87阅读模式
英文:

Lists manipulation with indexs

问题

我有两个列表。

列表 A:

  1. A = ["apple", "cherry", "pear", "mango", "banana", "grape", "kiwi", "orange", "pineapple"]

列表 B:

  1. B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]

思路是使用 B 中每个项目的偏移量作为结果数组中的 "xx" 值的索引偏移量。
例如,这将是预期结果:

  1. C = [
  2. {"fruit": "apple", "xx": 789},
  3. {"fruit": "cherry", "xx": 789},
  4. {"fruit": "pear", "xx": 789},
  5. {"fruit": "mango", "xx": 921},
  6. {"fruit": "banana", "xx": 921},
  7. {"fruit": "grape", "xx": 921},
  8. {"fruit": "kiwi", "xx": 89},
  9. {"fruit": "orange", "xx": 89},
  10. {"fruit": "pineapple", "xx": 89}
  11. ]

例如,B[0] 的 "offset" 为 0,这意味着 C 的索引 >= 0 将具有 "xx" 值等于 B[0]['xx']。然后,B[0]['offset'] 为 3,将为索引 >= 3 的 C 项目设置新的 "xx" 值,依此类推。

我可以使用数据框和 pandas 实现类似的结果。但由于 pandas 库相当庞大,我被要求在不使用 pandas 的情况下完成。

英文:

I have two lists.

List A :

  1. A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]

List B :

  1. B = [{"offset":0, "xx":789},{"offset":3, "xx":921},{"offset":6, "xx":89}]

The idea is to use the offset from each item in B as an index offset for setting the xx values in our results array.
For instance, this would be the expected result:

  1. C=[
  2. {"fruit":"apple","xx":789},
  3. {"fruit":"cherry","xx":789},
  4. {"fruit":"pear","xx":789},
  5. {"fruit":"mango","xx":921},
  6. {"fruit":"banana","xx":921},
  7. {"fruit":"grape","xx":921},
  8. {"fruit":"kiwi","xx":89},
  9. {"fruit":"orange","xx":89},
  10. {"fruit":"pineapple","xx":89},
  11. ]

For example, B[0] has "offset" of 0. this means that C of index >= 0 will have an "xx" value of B[0]['xx']. Then we have B[0]['offset'] of 3 that will set new "xx" values to the C items with index >= 3 and so on.

I am able to acheive a similar result using a dataframes and pandas. But since pandas library is quite heavy, I am requested to do it without using pandas.

答案1

得分: 6

使用一个简单的循环如何?

  1. # 将 B 重新格式化
  2. dic = {d['offset']: d['xx'] for d in B}
  3. # {0: 789, 3: 921, 6: 89}
  4. C = []
  5. v = None
  6. for i, a in enumerate(A):
  7. v = dic.get(i, v) # 如果达到阈值,更新值
  8. C.append({'fruit': a, 'xx': v})
  9. print(C)

输出:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]
英文:

What about using a simple loop?

  1. # rework B in a better format
  2. dic = {d['offset']:d['xx'] for d in B}
  3. # {0: 789, 3: 921, 6: 89}
  4. C = []
  5. v = None
  6. for i, a in enumerate(A):
  7. v = dic.get(i, v) # if we reached a threshold, update the value
  8. C.append({'fruit':a, 'xx': v})
  9. print(C)

Output:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]

答案2

得分: 1

如果需要使B的结构如此,你可以这样做:

  1. A = ["apple", "cherry", "pear", "mango", "banana", "grape", "kiwi", "orange", "pineapple"]
  2. B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
  3. C = []
  4. B_iter = 0
  5. for i, fruit in enumerate(A):
  6. # 检查是否不是最后一个元素并且下一个元素是新范围的开始
  7. if B[B_iter] != B[-1] and B[B_iter + 1]["offset"] == i:
  8. B_iter += 1
  9. C.append({"fruit": fruit, "xx": B[B_iter]["xx"]})
  10. print(C)

输出:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]
英文:

If the structure of B is required to be this way, you can do this:

  1. A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
  2. B = [{"offset":0, "xx":789},{"offset":3, "xx":921},{"offset":6, "xx":89}]
  3. C = []
  4. B_iter = 0
  5. for i, fruit in enumerate(A):
  6. # check if not the last element and next element is start of new range
  7. if B[B_iter] != B[-1] and B[B_iter+1]["offset"] == i:
  8. B_iter += 1
  9. C.append({"fruit": fruit, "xx": B[B_iter]["xx"]})
  10. print(C)

Output:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]

答案3

得分: 0

如果偏移量始终是3的倍数,您可以简单地进行整数除法来将实际索引映射到偏移量。

  1. A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
  2. B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
  3. C = [{"fruit": fruit, "xx": B[int(idx/3)]["xx"]} for idx, fruit in enumerate(A)]

输出:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]
英文:

If the offset is always multiple of 3 you can simply do integer division to map the actual index to offset.

  1. A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
  2. B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
  3. C = [{"fruit": fruit,"xx": B[int(idx/3)]["xx"]} for idx, fruit in enumerate(A)]

Output:

  1. [{'fruit': 'apple', 'xx': 789},
  2. {'fruit': 'cherry', 'xx': 789},
  3. {'fruit': 'pear', 'xx': 789},
  4. {'fruit': 'mango', 'xx': 921},
  5. {'fruit': 'banana', 'xx': 921},
  6. {'fruit': 'grape', 'xx': 921},
  7. {'fruit': 'kiwi', 'xx': 89},
  8. {'fruit': 'orange', 'xx': 89},
  9. {'fruit': 'pineapple', 'xx': 89}]

huangapple
  • 本文由 发表于 2023年2月16日 19:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定