如何将python字典转换d = {1:10, 2:20, 3:30, 4:30}为{10: [1], 20: [2], 30: [3, 4]}?
我需要反转一个字典,这些值应该成为另一个字典的键,并且这些值应该是列表中的键,即也在排序的内容中。
我有以下数据框。
ID LOC Alice Bob Karen
0 1 CH 9|5 6|3 4|4
1 2 ES 1|1 0|8 2|0
2 3 DE 2|4 6|6 3|1
3 4 ES 3|9 1|2 4|2
Run Code Online (Sandbox Code Playgroud)
Alice 和 Bob 列包含字符串值。我想以另一列的值为条件反转这些列中的字符串。例如,在 LOC==ES 处,反转相应列中的字符串将如下所示:
ID LOC Alice Bob Karen
0 1 CH 9|5 6|3 4|4
1 2 ES 1|1 8|0 0|2
2 3 DE 2|4 6|6 3|1
3 4 ES 9|3 2|1 2|4
Run Code Online (Sandbox Code Playgroud)
是否有一种快速方法可以对具有数千行的 csv 文件中的所有匹配行执行此操作?
谢谢你。
我发现Pandas groupby 累积总和并发现它非常有用。但是,我想确定如何计算反向累积和。
该链接建议如下。
df.groupby(by=['name','day']).sum().groupby(level=[0]).cumsum()
Run Code Online (Sandbox Code Playgroud)
为了反转总和,我尝试对数据进行切片,但失败了。
df.groupby(by=['name','day']).ix[::-1, 'no'].sum().groupby(level=[0]).cumsum()
Jack | Monday | 10 | 90
Jack | Tuesday | 30 | 80
Jack | Wednesday | 50 | 50
Jill | Monday | 40 | 80
Jill | Wednesday | 40 | 40
Run Code Online (Sandbox Code Playgroud)
编辑:根据反馈,我尝试实现代码并使数据框更大:
import pandas as pd
df = pd.DataFrame(
{'name': ['Jack', 'Jack', 'Jack', 'Jill', 'Jill'],
'surname' : ['Jones','Jones','Jones','Smith','Smith'],
'car' : ['VW','Mazda','VW','Merc','Merc'],
'country' : ['UK','US','UK','EU','EU'],
'year' : [1980,1980,1980,1980,1980],
'day': ['Monday', 'Tuesday','Wednesday','Monday','Wednesday'],
'date': ['2016-02-31','2016-01-31','2016-01-31','2016-01-31','2016-01-31'],
'no': [10,30,50,40,40],
'qty' : …Run Code Online (Sandbox Code Playgroud) 我希望能够编写一个递归函数来反转链表。想象一下,所有元素都已经附加到列表中。
我想分配head->next->next to head,所以node->next的下一个节点就是节点本身。然后,当递归完成时,将链表的头(this->head)分配给最终节点(即头)。
还缺少的是将最后一个节点分配到 NULL 旁边。
在任何世界上都会有这样的事情吗?它给出了运行时/分段错误。
struct node {
int data;
node *next;
};
class LinkedList{
node *head = nullptr;
public:
node *reverse(node *head){
if(head->next != nullptr){
reverse(head->next)->next = head;
}
else{
this->head = head;
}
return head;
}
};
Run Code Online (Sandbox Code Playgroud) 我在 apache 代理后面设置 Moodle 实例时遇到问题。
这是我的 apache 前端,它代理正在运行的服务器。
<VirtualHost *:80>
ServerName public.domain.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://10.10.10.10:81/moodle/
ProxyPassReverse / http://10.10.10.10:81/moodle/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)
和。
$CFG->wwwroot = 'http://public.domain.com';
Run Code Online (Sandbox Code Playgroud)
我安装没有问题,但是安装完成后我在浏览器中尝试:
此重定向到:http://public.domain.com/moodle/index.php? sessionstarted=1&lang=en ...
有谁知道会发生什么?
我试图将 CLLocationCoordinates2d 坐标转换为物理街道地址。
我试过 CLGeocoder 没有运气。这是我提供纬度和经度坐标的代码。
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
// let locationtext = "locations = \(locValue.latitude) \(locValue.longitude)"
locationTxt.text = "\(locValue.latitude) \(locValue.longitude)"
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
所以我想做的是输出字符串“33k22k11k”,它只是最后一个值,后跟反转的最后一个键,后跟倒数第二个值,后跟倒数第二个反转的键,依此类推。我不确定如何获取我所在的特定循环的反向键值。从我当前拥有的代码中,我得到输出:
dict = {"k1":1, "k2":2, "k3":3}
current=""
current_k=""
for k,v in dict.items():
for i in k:
current_k=i+current_k
current=str(v)+current_k+current
print(current)
print(current_k)
Run Code Online (Sandbox Code Playgroud)
33k2k1k22k1k11k
3k2k1k
因此,我尝试在反向代理后面设置一个 Spring Boot 应用程序,但是当我使用它时,HttpServletResponse.redirect("/")它总是重定向到内部 url。无论有没有 都会发生这种情况use-forward-headers: true。
我从反向代理获得的标头如下:
upgrade-insecure-requests: 1
forwarded: for=10.x.x.x;host=internal-domain.com;proto=http
x-forwarded-server: external-domain.com
host: internal-domain.com
x-forwarded-host: internal-domain.com
x-forwarded-port: 80
x-forwarded-proto: http
Run Code Online (Sandbox Code Playgroud)
我无法更改反向代理返回的内容。
如果有人可以提供帮助,那就太棒了:) 谢谢
我想解决一个倒数问题。
这是我的递归函数,为什么Python3抱怨我的函数未定义?有什么想法吗?
class Solution:
def reverse(self, x: int) -> int:
if x < 0:
return -1 * reverse(self, x)
if x // 10 == 0:
return x
if x % 10 == 0:
return reverse(self, x // 10)
else:
return (x % 10) * 10 ** (len(str(x//10))) + reverse(self, x // 10)
Run Code Online (Sandbox Code Playgroud)
我只是遵循传统的递归函数。
您能帮我解决这个问题吗,因为我进行了很多搜索但无法解决。我有一个用于电力消耗的多变量数据框,并且我正在使用时间序列的 VAR(向量自回归)模型进行预测。我做出了预测,但我需要反转时间序列(energy_log_diff),因为我应用了季节性对数差来使序列平稳,以获得真实的能量值:
df['energy_log'] = np.log(df['energy'])
df['energy_log_diff'] = df['energy_log'] - df['energy_log'].shift(1)
Run Code Online (Sandbox Code Playgroud)
为此,我首先做了:
df['energy'] = np.exp(df['energy_log_diff'])
Run Code Online (Sandbox Code Playgroud)
这应该给出滞后 365 天的 2 个值之间的能量差,但我也不确定这一点。
我怎样才能做到这一点?
reverse ×10
python ×5
pandas ×2
proxy ×2
python-3.x ×2
recursion ×2
c++ ×1
cllocation ×1
dictionary ×1
difference ×1
function ×1
geocoding ×1
group-by ×1
linked-list ×1
list ×1
moodle ×1
spring ×1
spring-boot ×1
string ×1
swift ×1
time-series ×1
tomcat ×1
xcode ×1